返回列表 發帖
本帖最後由 李唯銘 於 2024-8-17 15:34 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     srand(time(NULL));
  8.     int n[2], ans, player, start, startin, tmp1, tmp2;
  9.     int name[99], score[99];
  10.     cout<<"***超級金頭腦***"<<endl;
  11.     cout<<"由電腦隨機出題,比誰能最快答完三題"<<endl;
  12.     cout<<"有幾位挑戰者"<<endl;
  13.     cin>>player;
  14.     for(int i=1; i<=player; i++){
  15.     cout<<"第"<<i<<"位挑戰者請輸入姓名"<<endl;
  16.     cin>>name[i-1];
  17.     cout<<"準備就緒"<<endl;
  18.     system("pause");
  19.     system("cls");
  20.     start=clock();
  21.     for(int a=1; a<=3; a++){
  22.     startin=clock();
  23.     for(int j=0; j<2; j++)
  24.     {
  25.         n[j]=rand()%899+101;
  26.         for(int k=0; k<j; k++)
  27.         {
  28.             if(n[j]==n[k])
  29.             {
  30.                j--;
  31.                break;
  32.             }
  33.         }
  34.     }
  35.     cout<<n[0]<<"+";
  36.     cout<<n[1]<<"=";
  37.     cin>>ans;
  38.     if(ans==n[0]+n[1]){
  39.         cout<<"答對了,用了"<<clock()-startin<<"毫秒"<<endl;
  40.     }
  41.     else{
  42.         cout<<"答錯了,用了"<<clock()-startin<<"毫秒"<<endl;
  43.         a--;
  44.     }
  45.     }
  46.     score[i-1]=clock()-start;
  47.     }
  48.     for(int i=0; i<player-1; i++)
  49.     {
  50.          for(int j=i+1; j<player; j++)
  51.          {
  52.              if(score[j]<score[i])
  53.              {
  54.                   tmp1=name[j];
  55.                   name[j]=name[i];
  56.                   name[i]=tmp1;
  57.                   tmp2=score[j];
  58.                   score[j]=score[i];
  59.                   score[i]=tmp2;
  60.              }
  61.          }
  62.     }
  63.     system("cls");
  64.     cout<<"***金頭腦最終排名***"<<endl;
  65.     for(int i=0; i<player; i++)
  66.         cout<<"第"<<i+1<<"名"<<name[i]<<"\t"<<"花了"<<score[i]<<"毫秒"<<endl;
  67.     cout<<endl;
  68.     system("pause");
  69.     return 0;

  70. }
複製代碼

TOP

返回列表