題目是
一...有三個骰子,擲600,000次,請計算每個骰子的1~6點各出現幾次?????
二...猜數字1~10,000由電腦選出,使用者末猜最多10次?????
請幫我用C++打程式出來...亂數(Random)
程式打出來是可以跑的...謝謝阿...
2007-08-17 13:31:31 · 2 個解答 · 發問者 ㄚ嘴 4 in 電腦與網際網路 ➔ 程式設計
1.
#include
#include
using namespace std;
int main()
{
srand(static_cast < unsigned int >(time(NULL)));
int i, count[6]={0,0,0,0,0,0};
for(int times=1; times<=600000;times++) {
i=rand()%7;
count[i-1]++;
}
for(i=0;i<6;i++) {
cout << i+1 << " 出現 " << count[i] << " 次" << endl;
}
system("pause");
}
2.
#include
#include
using namespace std;
int main()
{
srand(static_cast < unsigned int >(time(NULL)));
int guess, ans=rand()%10001;
for(int times=1; times<=10;times++) {
cout << "第" << times << "次 : ";
cin >> guess;
if(ans==guess) {
cout << "答對了!!" << endl;
break;
}
else if(ans
}
else
cout << "大一點" << endl;
}
cout << "答案是 : " << ans << endl;
system("pause");
}
2007-08-18 14:06:48 · answer #1 · answered by zonyen 3 · 0⤊ 0⤋
//第一題
#include
#include
using namespace std;
int main()
{
srand(time(NULL));
int i,j,k,temp;
int sum[7]={0};
int n[4]={0,10000,10000,10000};
int Die[4][7]={0};
for(k=1;k<=3;k++){
for(i=1;i<=n[k];i++){
temp = (rand()%6+1) ;
for(j=1;j<=6;j++)
if(temp==j) Die[1][j]++;
}
}
for(i=1;i<=6;i++){
for(j=1;j<=3;j++){
sum[i]+=Die[j][i];
}
cout<
}
system("pause");
return 0;
}
//第二題
#include
#include
using namespace std;
int main()
{
srand(time(NULL));
int ans,player,high=10000,low=1,count=1;
ans = rand()%10000+1;
do{
cout<
if(ans==player) cout<< "GOOD!!";
else
{
if(player>ans) high = player;
if(player
count++;
}while(ans!=player && count<=10);
if(count>10 && ans!=player) cout<<"FAILD!!";
system("pause");
return 0;
}
2007-08-18 02:47:23 · answer #2 · answered by 小哈 2 · 0⤊ 0⤋