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

TOP

返回列表