返回列表 發帖
本帖最後由 吳孟書 於 2019-6-15 13:55 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {  
  6.     int n;
  7.     cout<<"*** 超級金頭腦 v1.0***"<<endl<<endl;
  8.     cout<<"遊戲規則:電腦隨機出題,比賽誰能在最短時間內答對三題!"<<endl;
  9.     system("pause");
  10.     system("cls");
  11.     cout<<"有幾位挑戰者?";
  12.     cin>>n;
  13.     system("cls");
  14.     string name[n];
  15.     double sum[n];
  16.     for(int i=0;i<n;i++)
  17.     {
  18.         cout<<"第"<<i+1<<"位挑戰者您好,請輸入你的大名:";
  19.         cin>>name[i];
  20.         system("cls");
  21.         cout<<name[i]<<"同學請就位!";
  22.         system("pause");
  23.         system("cls");
  24.         int c=0;
  25.         while(c<3)
  26.         {
  27.             srand(time(NULL));
  28.             int num1,num2,ans,pass;
  29.             double s,e;
  30.             num1 = rand()%889+111;
  31.             num2 = rand()%889+111;
  32.             cout<<num1<<" + "<<num2<<" = "<<endl;
  33.             s=clock();
  34.             cin>>ans;
  35.             e=clock();
  36.             pass=e-s;
  37.             if(ans == (num1+num2))
  38.            {
  39.                 cout<<"答對了!本題你花了"<<e-s<<"毫秒思考!"<<endl;
  40.                 sum[i] = sum[i]+pass;
  41.                 c++;
  42.            }else
  43.            {
  44.                 cout<<"答錯了!正確答案是"<<(num1+num2)<<".本題你花了"<<e-s<<"毫秒思考!"<<endl;
  45.                 sum[i] = sum[i]+pass;
  46.            }
  47.        }
  48.        cout<<name[i]<<"同學總共花了"<<sum[i]<<"毫秒!!"<<endl;
  49.        system("pause");
  50.        system("cls");  
  51.    }
  52.    for(int i=0;i<n-1;i++)
  53.    {
  54.          for(int j=i+1;j<n;j++)
  55.          {
  56.                   if(sum[i]>sum[j])
  57.                   {
  58.                          string tmpName;
  59.                          double tmpSum;
  60.                          tmpName = name[i];
  61.                          name[i] = name[j];
  62.                          name[j] = tmpName;
  63.                          tmpSum = sum[i];
  64.                          sum[i] = sum[j];
  65.                          sum[j] = tmpSum;
  66.                   }
  67.                  
  68.          }
  69.    }
  70.    cout<<"排名\t姓名\t成績"<<endl;
  71.    cout<<"---------------------"<<endl;
  72.    for(int i=0;i<n;i++)
  73.    {
  74.          cout<<(i+1)<<"\t"<<name[i]<<"\t"<<sum[i]<<endl;
  75.    }
  76.    system("pause");
  77.    return 0;   
  78. }
複製代碼

TOP

返回列表