返回列表 發帖

[作業] 賽馬程式 (三)

本帖最後由 tonyh 於 2013-10-26 14:25 編輯

使在比賽結束後, 程式能判斷出哪位選手勝出.
提示: 判斷變數 a, b, c, 還是 d, 誰比較大
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int winner;
  7.     int a=0,b=0,c=0,d=0,r;
  8.     cout<<"$ 財源滾滾賽馬場 $"<<endl;
  9.     cout<<"---------------------------------------------------------------------|終點|"<<endl;
  10.     cout<<"◥1◣"<<endl;
  11.     cout<<"◥2◣"<<endl;
  12.     cout<<"◥3◣"<<endl;
  13.     cout<<"◥4◣"<<endl;
  14.     system("pause");
  15.     system("cls");
  16.     srand(time(NULL));
  17.     while((a!=70)&&(b!=70)&&(c!=70)&&(d!=70))
  18.     {
  19.         r=rand()%4;    //產生0~3之隨機亂數
  20.         if(r==0)
  21.             a++;
  22.         if(r==1)
  23.             b++;
  24.         if(r==2)
  25.             c++;
  26.         if(r==3)
  27.             d++;
  28.         cout<<"比賽進行中"<<endl;
  29.         cout<<"---------------------------------------------------------------------|終點|"<<endl;
  30.         for(int i=1;i<=a;i++)
  31.             cout<<" ";
  32.         cout<<"◥1◣"<<endl;
  33.         for(int i=1;i<=b;i++)
  34.             cout<<" ";
  35.         cout<<"◥2◣"<<endl;
  36.         for(int i=1;i<=c;i++)
  37.             cout<<" ";
  38.         cout<<"◥3◣"<<endl;
  39.         for(int i=1;i<=d;i++)
  40.             cout<<" ";
  41.         cout<<"◥4◣"<<endl;
  42.         system("cls");
  43.     }
  44.         if(a>b && a>c && a>d)
  45.             winner=1;
  46.         else if(b>a && b>c && b>d)
  47.              winner=2;
  48.         else if(c>a && c>b && c>d)
  49.              winner=3;
  50.         else
  51.             winner=4;
  52.         cout<<"比賽結束! 由 "<<winner<<" 號選手先馳得點!"<<endl;
  53.         cout<<"---------------------------------------------------------------------|終點|"<<endl;
  54.         for(int i=1;i<=a;i++)
  55.             cout<<" ";
  56.         cout<<"◥1◣"<<endl;
  57.         for(int i=1;i<=b;i++)
  58.             cout<<" ";
  59.         cout<<"◥2◣"<<endl;
  60.         for(int i=1;i<=c;i++)
  61.             cout<<" ";
  62.         cout<<"◥3◣"<<endl;
  63.         for(int i=1;i<=d;i++)
  64.             cout<<" ";
  65.         cout<<"◥4◣"<<endl;  
  66.     system("pause");   
  67.     return 0;
  68. }
複製代碼

返回列表