返回列表 發帖

超級金頭腦(二)

本帖最後由 陳品肇 於 2019-6-15 13:37 編輯

設計遊戲 "超級金頭腦",
讓使用者計算兩個範圍介於111~999之隨機亂數的和.
參考作法如下:
1. 要有一個起始畫面, 顯示標題與遊戲規則

2. 參賽人數

3. 參賽者姓名

4. 請就位的訊息

5. 測驗中畫面
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.         int n; //n幾位
  8.         cout<<"***超腦金頭腦 v1.0 ***"<<endl;
  9.         cout<<endl;
  10.         cout<<"遊戲規則: 電腦隨機出題,比賽誰能在最短時間內算對三題!"<<endl;
  11.         system("pause");
  12.         system("cls"); //畫面清空
  13.         
  14.         cout<<"有幾位挑戰者:";
  15.         cin>>n;
  16.         system("pause");
  17.         system("cls"); //畫面清空
  18.         
  19.         string name[n];  //宣告姓名陣列
  20.         double sum[n];   //宣告個別總秒數陣列
  21.         srand(time(NULL));
  22.         
  23.         for(int i=0;i<n;i++)   //幾位要挑戰
  24.         {
  25.            cout<<"第"<<i+1<<"位挑戰者您好,請輸入你的大名:";
  26.            cin>>name[i];  //姓名存進陣列裡
  27.            system("pause");
  28.            system("cls"); //畫面清空
  29.            
  30.            cout<<name[i]<<"同學請就位!"<<endl;
  31.            system("pause");
  32.            system("cls"); //畫面清空
  33.            
  34.            int count =0;  //儲存對的題數
  35.            while(count <3) //還沒答對三題前,持續產生題目
  36.            {
  37.                int a=rand()%889+ 111; //產生第一個亂數
  38.                int b=rand()%889+ 111; //產生第二個亂數
  39.                double start,end,pass;  
  40.                
  41.                int ans; //回答的答案
  42.                start = clock();  //產生題目的時間
  43.                cout<<a<<" + "<<b<<" = ";
  44.                cin>>ans;  //回答的時間+題目的時間
  45.                end = clock();
  46.                pass = end - start; //實際思考的時間
  47.                
  48.                if(ans ==(a+b))  //答對的話!
  49.                {
  50.                    cout<<"答對了!. 本題花了"<<pass<<"毫秒思考!"<<endl;  
  51.                    sum[i] = sum[i]+ pass;   //時間累計
  52.                    count++;  
  53.                }else   //答錯執行下面的結果
  54.                {
  55.                    cout<<"答錯了!正確答案是"<<a+b<<". 本題花了"<<pass<<"毫秒思考!"<<endl;
  56.                    sum[i] = sum[i]+ pass;    //時間累計  
  57.                }
  58.                
  59.            }
  60.            cout<<name[i]<<"同學總共花了"<<sum[i]<<"毫秒!" <<endl;
  61.            
  62.            system("pause");
  63.            system("cls"); //畫面清空
  64.             
  65.         }
  66.          //排序
  67.         for(int i=0;i<n-1;i++)
  68.         {
  69.            for(int j=i+1;j<n;j++)
  70.            {
  71.                 if(sum[i]>sum[j])
  72.                 {
  73.                       string tmpName; //暫存姓名交換
  74.                       double tmpSum;  //暫存總秒數交換
  75.                       //姓名
  76.                       tmpName = name[i];
  77.                       name[i] = name[j];
  78.                       name[j] = tmpName;
  79.                       //成績
  80.                       tmpSum = sum[i];
  81.                       sum[i] = sum[j];
  82.                       sum[j] = tmpSum;
  83.                 }
  84.            }
  85.         }
  86.         cout<<"排名\t姓名\t成績"<<endl;
  87.         cout<<"---------------------"<<endl;
  88.         for(int i =0;i<n;i++)
  89.         {
  90.             cout<<(i+1)<<"\t"<<name[i]<<"\t"<<sum[i]<<endl;
  91.         }
  92.         
  93.         
  94.         system("pause");
  95.         return 0;
  96. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

返回列表