返回列表 發帖
本帖最後由 王瑞喻 於 2019-6-15 15:24 編輯
  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<<endl;
  9.     cout<<"遊戲規則:電腦隨機出題,比賽誰能在最短的時間內算對三題!"<<endl;
  10.     system("pause");
  11.     system("cls");   //畫面清空
  12.    
  13.    
  14.     cout<<"有幾位挑戰者?";
  15.     cin>>n;
  16.     string name[n];   //姓名       不要放在n的前面
  17.     double sum[n];    //個人總秒數 不要放在n的前面
  18.     system("pause");
  19.     system("cls");
  20.     for(int i=0;i<n;i++)
  21.     {
  22.         int count=0; //count紀錄答對幾題 每個人開始時都要歸零
  23.         cout<<"第"<<i+1<<"位挑戰者您好,請輸入您的大名:";
  24.         cin>>name[i];
  25.         system("pause");
  26.         system("cls");
  27.         
  28.         
  29.         cout<<name[i]<<"同學請就位!"<<endl;
  30.         system("pause");
  31.         system("cls");
  32.    
  33.         while(count<3)
  34.         {
  35.             srand(time(NULL));
  36.             int a,b,ans;
  37.             double start,end,pass;
  38.             a=rand()%889+111;//889+111 111-999
  39.             b=rand()%889+111;
  40.             cout<<a<<" + "<<b<<" = ";
  41.             start=clock();
  42.             cin>>ans;
  43.             end=clock();
  44.             pass=end-start;
  45.             if(ans==(a+b))
  46.             {
  47.                     cout<<"答對了!本題花了"<<pass<<"毫秒思考"<<endl;
  48.                     sum[i]=sum[i]+pass;
  49.                     count++;
  50.             }else
  51.             {
  52.                     cout<<"答錯了!正確答案是"<<(a+b)<<".本題花了"<<pass<<"毫秒思考"<<endl;
  53.                     sum[i]=sum[i]+pass;
  54.             }
  55.         }        
  56.         cout<<endl<<name[i]<<"同學總共花了"<<sum[i]<<"毫秒"<<endl;
  57.         system("pause");
  58.         system("cls");
  59.     }
  60.     //排序
  61.     for(int i=0;i<n-1;i++)
  62.     {
  63.         for(int j=i+1;j<n;j++)
  64.         {
  65.             if(sum[i]>sum[j])
  66.             {
  67.                 string tmpName;
  68.                 double tmpSum;//暫存總秒數交換
  69.                 tmpName = name[i];
  70.                 name[i] = name[j] ;
  71.                 name[j] = tmpName;
  72.                 //成績
  73.                 tmpSum = sum[i];
  74.                 sum[i] = sum[j];
  75.                 sum[j] = tmpSum ;
  76.             }
  77.         }
  78.     }
  79.     cout<<"排名\t姓名\t成績"<<endl;
  80.     cout<<"---------------------"<<endl;
  81.     for(int i=0;i<n;i++)
  82.     {
  83.         cout<<(i+1)<<"\t"<<name[i]<<"\t"<<sum[i]<<endl;
  84.     }
  85.     system("pause");
  86.     return 0;
  87. }
複製代碼

TOP

返回列表