本帖最後由 吳孟修 於 2019-6-15 13:54 編輯
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int n;
- cout<<"***超腦金頭腦 v1.0 ***"<<endl;
- cout<<endl;
- cout<<"遊戲規則: 電腦隨機出題,比賽誰能在最短時間內算對三題!"<<endl;
- system("pause");
- system("cls");
- cout<<"有幾位挑戰者:";
- cin>>n;
- system("pause");
- system("cls");
- string name[n];
- double sum[n];
- srand(time(NULL));
- for(int i=0;i<n;i++)
- {
- cout<<"第"<<i+1<<"位挑戰者您好,請輸入你的大名:";
- cin>>name[i];
- system("pause");
- system("cls");
- cout<<name[i]<<"同學請就位!"<<endl;
- system("pause");
- system("cls");
- int count =0;
- while(count <3)
- {
- int a=rand()%889+ 111;
- int b=rand()%889+ 111;
- double start,end,pass;
- int ans;
- start = clock();
- cout<<a<<" + "<<b<<" = ";
- 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<<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;
- }
複製代碼 |