本帖最後由 tonyh 於 2013-10-26 14:25 編輯
使在比賽結束後, 程式能判斷出哪位選手勝出.
提示: 判斷變數 a, b, c, 還是 d, 誰比較大- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int winner;
- int a=0,b=0,c=0,d=0,r;
- cout<<"$ 財源滾滾賽馬場 $"<<endl;
- cout<<"---------------------------------------------------------------------|終點|"<<endl;
- cout<<"◥1◣"<<endl;
- cout<<"◥2◣"<<endl;
- cout<<"◥3◣"<<endl;
- cout<<"◥4◣"<<endl;
- system("pause");
- system("cls");
- srand(time(NULL));
- while((a!=70)&&(b!=70)&&(c!=70)&&(d!=70))
- {
- r=rand()%4; //產生0~3之隨機亂數
- if(r==0)
- a++;
- if(r==1)
- b++;
- if(r==2)
- c++;
- if(r==3)
- d++;
- cout<<"比賽進行中"<<endl;
- cout<<"---------------------------------------------------------------------|終點|"<<endl;
- for(int i=1;i<=a;i++)
- cout<<" ";
- cout<<"◥1◣"<<endl;
- for(int i=1;i<=b;i++)
- cout<<" ";
- cout<<"◥2◣"<<endl;
- for(int i=1;i<=c;i++)
- cout<<" ";
- cout<<"◥3◣"<<endl;
- for(int i=1;i<=d;i++)
- cout<<" ";
- cout<<"◥4◣"<<endl;
- system("cls");
- }
- if(a>b && a>c && a>d)
- winner=1;
- else if(b>a && b>c && b>d)
- winner=2;
- else if(c>a && c>b && c>d)
- winner=3;
- else
- winner=4;
- cout<<"比賽結束! 由 "<<winner<<" 號選手先馳得點!"<<endl;
- cout<<"---------------------------------------------------------------------|終點|"<<endl;
- for(int i=1;i<=a;i++)
- cout<<" ";
- cout<<"◥1◣"<<endl;
- for(int i=1;i<=b;i++)
- cout<<" ";
- cout<<"◥2◣"<<endl;
- for(int i=1;i<=c;i++)
- cout<<" ";
- cout<<"◥3◣"<<endl;
- for(int i=1;i<=d;i++)
- cout<<" ";
- cout<<"◥4◣"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |