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

  82.    system("pause");
  83.    return 0;

  84. }
複製代碼

TOP

返回列表