- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- int main(){
- string name,tmp2;
- float t1,t2,t3,t4,pass,tmp1;
- int x,y,answer,answer_c,people;
- srand(time(NULL));
- cout<<"***超級金頭腦 1.0***"<<endl<<endl;
- cout<<"遊戲規則:電腦隨機出題,比賽誰能在最短的時間算對3題"<<endl<<endl;
- cout<<"有幾位參賽者:"<<endl;
- cin>>people;
- string n[people];
- int score[people];
- for(int i=1;i<=people;i++){
- cout<<"第"<<i<<"位挑戰者你好,請輸入你的大名";
- cin>>name;
- n[i-1]=name;
- system("cls");
- }
- for(int i=0;i<people;i++){
- cout<<n[i]<<"同學請就位!"<<endl;
- system("pause");
- t3=clock();
- for(int j=1;j<=3;j++){
- while(true){
- x=rand()%10;
- y=rand()%10;
- answer_c=x+y;
- cout<<x<<"+"<<y<<"=";
- t1=clock();
- cin>>answer;
- t2=clock();
- pass=t2-t1;
- if(answer==answer_c){
- cout<<"答對了!本題花"<<pass<<"毫秒思考"<<endl;
- break;
- }
- else {
- cout<<"答錯了!本題花"<<pass<<"毫秒思考"<<endl;
- j--;
- break;
- }
- }
- }
- t4=clock();
- cout<<"總共花"<<t4-t3<<"毫秒"<<endl;
- score[i]=t4-t3;
- system("pause");
- system("cls");
- }
- cout<<"***金頭腦風雲榜***"<<endl<<endl;
- for(int k=0;k<people-1;k++){
- for(int p=k+1;p<people;p++){
- if(score[k]>score[p]){
- tmp1=score[k];
- score[k]=score[p];
- score[p]=tmp1;
- tmp2=n[k];
- n[k]=n[p];
- n[p]=tmp2;
- }
- }
- }
- for(int i=0;i<people;i++){
- cout<<"第"<<i+1<<"名"<<" "<<n[i]<<"\t"<<score[i]<<"毫秒"<<endl;
- }
- system ("pause");
- return 0;
- }
複製代碼 |