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

TOP

返回列表