本帖最後由 李唯銘 於 2024-8-17 15:34 編輯
- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- int main()
- {
- srand(time(NULL));
- int n[2], ans, player, start, startin, tmp1, tmp2;
- int name[99], score[99];
- cout<<"***超級金頭腦***"<<endl;
- cout<<"由電腦隨機出題,比誰能最快答完三題"<<endl;
- cout<<"有幾位挑戰者"<<endl;
- cin>>player;
- for(int i=1; i<=player; i++){
- cout<<"第"<<i<<"位挑戰者請輸入姓名"<<endl;
- cin>>name[i-1];
- cout<<"準備就緒"<<endl;
- system("pause");
- system("cls");
- start=clock();
- for(int a=1; a<=3; a++){
- startin=clock();
- for(int j=0; j<2; j++)
- {
- n[j]=rand()%899+101;
- for(int k=0; k<j; k++)
- {
- if(n[j]==n[k])
- {
- j--;
- break;
- }
- }
- }
- cout<<n[0]<<"+";
- cout<<n[1]<<"=";
- cin>>ans;
- if(ans==n[0]+n[1]){
- cout<<"答對了,用了"<<clock()-startin<<"毫秒"<<endl;
- }
- else{
- cout<<"答錯了,用了"<<clock()-startin<<"毫秒"<<endl;
- a--;
- }
- }
- score[i-1]=clock()-start;
- }
- for(int i=0; i<player-1; i++)
- {
- for(int j=i+1; j<player; j++)
- {
- if(score[j]<score[i])
- {
- tmp1=name[j];
- name[j]=name[i];
- name[i]=tmp1;
- tmp2=score[j];
- score[j]=score[i];
- score[i]=tmp2;
- }
- }
- }
- system("cls");
- cout<<"***金頭腦最終排名***"<<endl;
- for(int i=0; i<player; i++)
- cout<<"第"<<i+1<<"名"<<name[i]<<"\t"<<"花了"<<score[i]<<"毫秒"<<endl;
- cout<<endl;
- system("pause");
- return 0;
- }
複製代碼 |