返回列表 發帖
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main(){
  6.     string name,tmp2;
  7.     float t1,t2,t3,t4,pass,tmp1;
  8.     int x,y,answer,answer_c,people;
  9.     srand(time(NULL));
  10.     cout<<"***超級金頭腦 1.0***"<<endl<<endl;
  11.     cout<<"遊戲規則:電腦隨機出題,比賽誰能在最短的時間算對3題"<<endl<<endl;
  12.     cout<<"有幾位參賽者:"<<endl;
  13.     cin>>people;
  14.     string n[people];
  15.     int score[people];
  16.     for(int i=1;i<=people;i++){
  17.         cout<<"第"<<i<<"位挑戰者你好,請輸入你的大名";
  18.         cin>>name;
  19.         n[i-1]=name;
  20.         system("cls");
  21.     }
  22.     for(int i=0;i<people;i++){
  23.         cout<<n[i]<<"同學請就位!"<<endl;
  24.         system("pause");
  25.         t3=clock();
  26.         for(int j=1;j<=3;j++){
  27.         while(true){

  28.         x=rand()%10;
  29.         y=rand()%10;
  30.         answer_c=x+y;
  31.         cout<<x<<"+"<<y<<"=";
  32.         t1=clock();
  33.         cin>>answer;
  34.         t2=clock();
  35.         pass=t2-t1;
  36.             if(answer==answer_c){
  37.                 cout<<"答對了!本題花"<<pass<<"毫秒思考"<<endl;
  38.                 break;
  39.             }
  40.             else {
  41.                 cout<<"答錯了!本題花"<<pass<<"毫秒思考"<<endl;
  42.                j--;
  43.                break;

  44.             }

  45.         }
  46.         }
  47.         t4=clock();
  48.         cout<<"總共花"<<t4-t3<<"毫秒"<<endl;
  49.         score[i]=t4-t3;
  50.         system("pause");
  51.         system("cls");
  52.   }
  53.   cout<<"***金頭腦風雲榜***"<<endl<<endl;
  54.   for(int k=0;k<people-1;k++){
  55.     for(int p=k+1;p<people;p++){
  56.         if(score[k]>score[p]){
  57.             tmp1=score[k];
  58.             score[k]=score[p];
  59.             score[p]=tmp1;

  60.             tmp2=n[k];
  61.             n[k]=n[p];
  62.             n[p]=tmp2;
  63.         }
  64.     }
  65.   }
  66.   for(int i=0;i<people;i++){
  67.   cout<<"第"<<i+1<<"名"<<"  "<<n[i]<<"\t"<<score[i]<<"毫秒"<<endl;
  68.   }
  69.     system ("pause");
  70.     return 0;

  71. }
複製代碼

TOP

返回列表