導航:首頁 > 源碼編譯 > 隨機猜數演算法

隨機猜數演算法

發布時間:2022-08-26 11:06:47

1. 有沒有大神給我講解一下這個猜數字小程序的演算法(代碼在裡面)

這里哪有演算法,就是循環,讓人猜數字大小。電腦生成隨機一個[1,100]中的數,人猜,輸入的如果比隨機值大就顯示too big,小就too small,直到猜對為止。

2. c語言怎麼使用隨機函數rand編寫一個猜數字的游戲程序

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
main()
{
int i,a,b;
srand(time(0)); /*設置種子,並生成偽隨機序列*/

while(~scanf("%d",&b))
{
for(i=0;i<N;++i) rand()%10;
a = rand()%100;
while(1)
{
if(a == b) {puts(" 恭喜你 猜對了 !\n***************\n"); break;}
else if(a > b) puts("你輸入的數 小了!");
else puts("你輸入的數 大了!");
scanf("%d",&b);
}
}
system("pause");

}

3. 猜數字游戲: 讓系統隨機生成一個1-100之間的隨機數,循環錄入猜數直到猜中為止

4. 猜數程序.要完整的程序!(用rand產生一個隨機數)

#include<iostream.h>
#include<stdlib.h>
#include<time.h>
void
main()
{
int
m,s,t,flag;
char
ch;
for(;;)
{
flag=0;
s=0;
srand(time(0));
do
m=rand();
while(m<0||m>100);
cout<<"我已經想到了一個0-100的整數,請你猜猜看。"<<endl;
for(;;)
{
if(flag)break;
cin>>t;
s++;
if(t>m)
cout<<"你猜的數太大了。"<<endl;
else
if(t<m)
cout<<"你猜的數太小了。"<<endl;
else
{
cout<<"恭喜!你猜對了!你猜了"<<s<<"次"<<endl;
cout<<"還想玩嗎?(Y/N)";
cin>>ch;
if(!(ch=='Y'||ch=='y'))
return;
else
flag=1;
}
}
}
}

5. 急求c語言 猜數字演算法

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 4/*隨機抽N個數*/
#define NUM 8/*NUM次猜數的機會*/

void detect(char s[])
{
int i,j,num;
int a,b;
char ch[N];
for(num=0;num<NUM;num++)
{
a=b=0;
printf("第%d次機會:",num+1);
for(i=0;i<N;i++)
{
ch[i]=getch();
if(ch[i]>='0'&&ch[i]<='9')
{
for(j=0;j<i;j++)
if(ch[i]==ch[j]) break;
if(j<i) i--;
else
{
putchar(ch[i]);
for(j=0;j<N;j++)
{
if(ch[i]==s[j])
if(i==j) a++;
else b++;
}
}
}
else
i--;
}
printf(" %dA%dB\n",a,b);
if(a==N)
{
printf("恭喜你答對了!\n");
break;
}
}
if(num==NUM)
printf("很遺憾,正確答案為:%s\n",s);
}

main()
{
int i,j;
char s[N+1];
srand(time(0));
for(i=0;i<N;i++)
{
s[i]=rand()%10;
for(j=0;j<i;j++)
if(s[i]==s[j]) break;
if(j<i) i--;
else
{
s[i]+='0';
putchar('*');
}
}
s[i]='\0';
printf("\n總共%d次機會\n",NUM);
detect(s);
}

6. C語言編程:編寫一個猜數的游戲,系統自動產生一個隨機數,你來猜,程序給出提示,直到猜對為止。

import java.util.*;

class Assignment8{

public static void main(String[]args){

Scanner sc=new Scanner(System.in);

int x=(int)(Math.random()*100);//生成一個0~100的隨機數

int y=-1;

System.out.println("已生成0~100的隨機整數,請輸入您所猜的數:");

while(x!=y)

{

y=sc.nextInt();

if(y&gt;x)

{

System.out.println("輸入的數過大");

}

else if(y&lt;x)

{

System.out.println("輸入的數過小");

}

}

System.out.println("正確!該隨機數是"+x);

sc.close();

}

}

(6)隨機猜數演算法擴展閱讀:

while循環的格式:while(表達式){語句;}

while循環的執行順序:當表達式為真,則執行下面的語句,語句執行完之後再判斷表達式是否為真,如果為真,再次執行下面的語句,然後再判斷表達式是否為真……就這樣一直循環下去,直到表達式為假,跳出循環。

例:

int a=NULL;

while(a&lt;10){

a++;//自加

if(a&gt;5)//不等while退出循環,直接判斷循環

{break;//跳出循環}

}

結果:結束後a的值為6。

閱讀全文

與隨機猜數演算法相關的資料

熱點內容
java添加圖標 瀏覽:883
命令與征服4任務攻略 瀏覽:97
演算法和邏輯程序驗證 瀏覽:764
用戶管理系統php模板 瀏覽:416
中國銀行app賬號怎麼登錄 瀏覽:864
安卓手機icloud雲伺服器地址 瀏覽:798
加密門禁如何寫到小米手機 瀏覽:166
java靜態方法的多線程 瀏覽:262
CAXAXp下載打不開要加密 瀏覽:241
開鎖pdf 瀏覽:86
python可視化奶茶 瀏覽:221
命令方字幕 瀏覽:592
蘋果手機如何連接ipad伺服器 瀏覽:978
列印矩陣java 瀏覽:396
如何雙開一個不能雙開的app 瀏覽:531
抖音喜歡加密了怎麼打開 瀏覽:388
mysql命令行顯示表 瀏覽:365
國防離我們源碼200字 瀏覽:782
vmwaretoolslinux 瀏覽:97
命令方塊生存放不了嗎 瀏覽:700