2012/08/26

[C++]亂數機率

好久沒coding了
趁著空檔簡單寫個測試亂數機率的程式
來個不傷腦力的這樣...

------------------------------------CODE------------------------------------

#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

int main(int argc, char *argv[])
{
 
    int Cnt;
    int Get;
    int Box[9][2];
    float Temp = 0;
    srand(time(NULL));
 
    for(int i=0; i<9; i++)
    {
        Box[i][0] = i+1;
        Box[i][1] = 0;
    }
 
 
    cout << "實驗次數:";
    cin >> Cnt;
 
    for(int i=Cnt; i>0; i--)
    {
        Get = rand()%9+1;
        Box[Get-1][1] = Box[Get-1][1]+1;
    }
 
    cout << endl;
    cout << "數字\t次數\t機率" << endl;
 
    for(int i=0; i<9; i++)
    {
        for(int j=0; j<2; j++)
        {
            cout << Box[i][j] << "\t" ;
        }
        Temp = Box[i][1];
        cout << Temp / Cnt << endl;
    }
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

------------------------------------CODE------------------------------------

完畢,繼續唸書去...

沒有留言:

張貼留言