標題:
[專題實作] 超級金頭腦 (二)
[打印本頁]
作者:
李知易
時間:
2025-2-21 02:25
標題:
[專題實作] 超級金頭腦 (二)
本帖最後由 李知易 於 2025-2-21 21:02 編輯
設計遊戲 "超級金頭腦",
讓使用者計算兩個範圍介於51~99之隨機亂數的和.
參考作法如下:
1. 要有一個起始畫面, 顯示標題與遊戲規則
2. 參賽人數
3. 參賽者姓名
4. 請就位的訊息
5. 測驗中畫面
6. 排名
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
int x;
long t1,t2;
srand(time(NULL));
cout<<"*** 超級金頭腦 v1.0 ***"<<endl<<endl;
cout<<"遊戲規則: 電腦隨機出題, 比賽誰能在最短的時間內算對三題!"<<endl<<endl;
cout<<"有幾位挑戰者? ";
cin>>x;
string name[x];
long score[x];
for(int i=0; i<x; i++)
{
system("cls");
score[i]=0;
cout<<"第"<<i+1<<"位挑戰者你好, 請輸入你的大名: ";
cin>>name[i];
cout<<name[i]<<"同學請就位!"<<endl<<endl;
system("pause");
system("cls");
int n=0; //答對的次數
int ans;
while(n<3)
{
int a=rand()%9+1;
int b=rand()%9+1;
cout<<a<<" + "<<b<<" = ";
t1=clock();
cin>>ans;
t2=clock();
if(ans==a+b)
{
cout<<"答對了! ";
n++;
}
else
cout<<"答錯了! 正確答案是"<<a+b<<". ";
cout<<"本題花了"<<t2-t1<<"毫秒思考!"<<endl;
score[i]+=(t2-t1);
}
cout<<endl<<name[i]<<"同學總共花了"<<score[i]<<"毫秒!"<<endl;
system("pause");
}
system("cls");
cout<<"*** 金頭腦風雲榜 ***"<<endl<<endl;
string tmp1;
long tmp2;
for(int i=0; i<x-1; i++)
{
for(int j=i+1; j<x; j++)
{
if(score[j]<score[i])
{
tmp1=name[i];
name[i]=name[j];
name[j]=tmp1;
tmp2=score[i];
score[i]=score[j];
score[j]=tmp2;
}
}
}
for(int i=0; i<x; i++)
cout<<"第"<<i+1<<"名\t"<<name[i]<<"\t"<<score[i]<<"毫秒"<<endl;
cout<<endl;
system("pause");
return 0;
}
複製代碼
作者:
洪睿輿
時間:
2025-2-21 21:03
此帖僅作者可見
作者:
鄭子宸
時間:
2025-2-22 09:17
此帖僅作者可見
作者:
吳孟樵
時間:
2025-2-23 08:09
此帖僅作者可見
作者:
江立辰
時間:
2025-2-23 12:08
此帖僅作者可見
作者:
楊昀瑾
時間:
2025-2-27 21:29
此帖僅作者可見
作者:
張詠博
時間:
2025-2-28 10:59
此帖僅作者可見
作者:
黃品錡
時間:
2025-3-6 21:29
此帖僅作者可見
作者:
朱立人
時間:
2025-3-7 19:13
此帖僅作者可見
作者:
陳頎諠
時間:
2025-3-13 21:57
此帖僅作者可見
作者:
彭郁程
時間:
2025-3-27 21:47
此帖僅作者可見
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2