本帖最後由 王瑞喻 於 2019-6-15 15:24 編輯
- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- int main()
- {
- int n; //n人數
- cout<<"***超級金頭腦 v1.0***"<<endl<<endl;
- cout<<"遊戲規則:電腦隨機出題,比賽誰能在最短的時間內算對三題!"<<endl;
- system("pause");
- system("cls"); //畫面清空
-
-
- cout<<"有幾位挑戰者?";
- cin>>n;
- string name[n]; //姓名 不要放在n的前面
- double sum[n]; //個人總秒數 不要放在n的前面
- system("pause");
- system("cls");
- for(int i=0;i<n;i++)
- {
- int count=0; //count紀錄答對幾題 每個人開始時都要歸零
- cout<<"第"<<i+1<<"位挑戰者您好,請輸入您的大名:";
- cin>>name[i];
- system("pause");
- system("cls");
-
-
- cout<<name[i]<<"同學請就位!"<<endl;
- system("pause");
- system("cls");
-
- while(count<3)
- {
- srand(time(NULL));
- int a,b,ans;
- double start,end,pass;
- a=rand()%889+111;//889+111 111-999
- b=rand()%889+111;
- cout<<a<<" + "<<b<<" = ";
- start=clock();
- cin>>ans;
- end=clock();
- pass=end-start;
- if(ans==(a+b))
- {
- cout<<"答對了!本題花了"<<pass<<"毫秒思考"<<endl;
- sum[i]=sum[i]+pass;
- count++;
- }else
- {
- cout<<"答錯了!正確答案是"<<(a+b)<<".本題花了"<<pass<<"毫秒思考"<<endl;
- sum[i]=sum[i]+pass;
- }
- }
- cout<<endl<<name[i]<<"同學總共花了"<<sum[i]<<"毫秒"<<endl;
- system("pause");
- system("cls");
- }
- //排序
- for(int i=0;i<n-1;i++)
- {
- for(int j=i+1;j<n;j++)
- {
- if(sum[i]>sum[j])
- {
- string tmpName;
- double tmpSum;//暫存總秒數交換
- tmpName = name[i];
- name[i] = name[j] ;
- name[j] = tmpName;
- //成績
- tmpSum = sum[i];
- sum[i] = sum[j];
- sum[j] = tmpSum ;
- }
- }
- }
- cout<<"排名\t姓名\t成績"<<endl;
- cout<<"---------------------"<<endl;
- for(int i=0;i<n;i++)
- {
- cout<<(i+1)<<"\t"<<name[i]<<"\t"<<sum[i]<<endl;
- }
- system("pause");
- return 0;
- }
複製代碼 |