本帖最後由 劉漢文 於 2012-3-13 20:23 編輯
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int main()
- {
- float rate[4];
- int run=1;
- int test;
- int a, b, c;
- float one=0 ,two=0 ,three=0 ,four=0;
- cout<<"$$$ 吃角子老虎機中獎機率評估程式 $$$"<<endl;
- cout<<"中獎規則說明:"<<endl;
- cout<<"任一欄出現7 --> 獎金變兩倍"<<endl;
- cout<<"任兩欄出現7 --> 獎金變十倍"<<endl;
- cout<<"三欄都出現7 --> 獎金變一百倍"<<endl;
- cout<<"本次測試方式: 讓使用者自行輸入欲測試的次數, 並以百分比的方式分別表示出中第一特獎, 二獎與三獎的機率"<<endl;
- cout<<"請輸入欲測試的次數: ";
- cin>>test;
- srand(time(NULL));
- while(run<=test)
- {
- a=rand()%10;
- b=rand()%10;
- c=rand()%10;
- cout<<a<<" "<<b<<" "<<c<<endl;
- if(a==7 && b==7 && c==7)
- {
- one++;
- }
- if((a==7 && b==7 && c!=7)||(a==7 && b!=7 && c==7)||(a!=7 && b==7 && c==7))
- {
- two++;
- }
- if((a==7 && b!=7 && c!=7)||(a!=7 && b==7 && c!=7)||(a!=7 && b!=7 && c==7))
- {
- three++;
- }
- if(a!=7 && b!=7 && c!=7)
- {
- four++;
- }
- run++;
- }
- cout<<"在這"<<test<<"次的測試中,三獎中了"<<three<<"次,二獎中了"<<two<<"次,第一特獎中了"<<one<<"次"<<endl;
- rate[0]=three/test*100;
- rate[1]=two/test*100;
- rate[2]=one/test*100;
- rate[3]=four/test*100;
- cout<<"中三獎任一欄出現7的機率約為"<<rate[0]<<"%"<<endl;
- cout<<"中二獎任兩欄出現7的機率約為"<<rate[1]<<"%"<<endl;
- cout<<"中第一特獎三欄都出現7的機率約為"<<rate[2]<<"%"<<endl;
- cout<<"都沒中獎的機率為"<<rate[3]<<"%"<<endl;
- system("pause");
- return 0;
- }
複製代碼 |