本帖最後由 吳孟書 於 2019-6-15 13:55 編輯
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int n;
- cout<<"*** 超級金頭腦 v1.0***"<<endl<<endl;
- cout<<"遊戲規則:電腦隨機出題,比賽誰能在最短時間內答對三題!"<<endl;
- system("pause");
- system("cls");
- cout<<"有幾位挑戰者?";
- cin>>n;
- system("cls");
- string name[n];
- double sum[n];
- for(int i=0;i<n;i++)
- {
- cout<<"第"<<i+1<<"位挑戰者您好,請輸入你的大名:";
- cin>>name[i];
- system("cls");
- cout<<name[i]<<"同學請就位!";
- system("pause");
- system("cls");
- int c=0;
- while(c<3)
- {
- srand(time(NULL));
- int num1,num2,ans,pass;
- double s,e;
- num1 = rand()%889+111;
- num2 = rand()%889+111;
- cout<<num1<<" + "<<num2<<" = "<<endl;
- s=clock();
- cin>>ans;
- e=clock();
- pass=e-s;
- if(ans == (num1+num2))
- {
- cout<<"答對了!本題你花了"<<e-s<<"毫秒思考!"<<endl;
- sum[i] = sum[i]+pass;
- c++;
- }else
- {
- cout<<"答錯了!正確答案是"<<(num1+num2)<<".本題你花了"<<e-s<<"毫秒思考!"<<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;
- }
複製代碼 |