#3167: 幫 C++ 隨機抽數的程式問題


massgekevin (瘋狂屎蛋尼)


我使用的是 DEV C + +

 我要寫一個抽籤的程式

需求:

 從50人(號)中抽出10人(號)

 每次抽的人都不一樣

 標記以選取過的人,避免重複抽取。

 好像要使用到

1.rand()%50

2.srand(time(NULL))

3.利用true 和 false 檢查是否有重複抽取

請問要怎麼製作呢?

我覺得難的地方就是檢查重複抽取!

 一開始宣告:bool people[50]

我也想順便問上面這宣告有何意思?

謝謝

#3175: Re:幫 C++ 隨機抽數的程式問題


example (學姊)


我使用的是 DEV C + +

 我要寫一個抽籤的程式

需求:

 從50人(號)中抽出10人(號)

 每次抽的人都不一樣

 標記以選取過的人,避免重複抽取。

 好像要使用到

1.rand()%50

2.srand(time(NULL))

3.利用true 和 false 檢查是否有重複抽取

請問要怎麼製作呢?

我覺得難的地方就是檢查重複抽取!

 一開始宣告:bool people[50]

我也想順便問上面這宣告有何意思?

謝謝

 在宣告時先把 people[50] 初始化為 false

 接下來如果抽到 10

 就把 people[10] 設為 true 

 下次在抽到就能避免囉

 ( 題外話 這是學校作業嗎? )

#3179: Re:幫 C++ 隨機抽數的程式問題


massgekevin (瘋狂屎蛋尼)


學姊!謝謝!我知道bool的意思了!
回你所問~~~!
嗯哼!
不好意思!! 
該不會有特別規定嗎?
#3180: Re:幫 C++ 隨機抽數的程式問題


example (學姊)


學姊!謝謝!我知道bool的意思了!
回你所問~~~!
嗯哼!
不好意思!! 
該不會有特別規定嗎?

 特別規定什麼@@
#3181: Re:幫 C++ 隨機抽數的程式問題


massgekevin (瘋狂屎蛋尼)



 特別規定什麼@@



哈哈!沒有啦!我還以為說不能問功課!

有點想破頭了 = =|||

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
bool temp[50];
int x,i;
srand(time(NULL));

 

do
{
x=rand()%50;  
}
while(temp[x]==true);
temp[x]=true;

 

for (i=0;i<50;i++)
{
   
    if(temp[i]==true)
     {
     cout<<i<<endl;
     }
}

system ("pause");
return 0;
}

 

好像還要再補一行  temp[i]=false;

可是我加進去都輸出一片空白。

上面的輸出都只有1個人(數字)

請問我要怎麼輸出10個人(數字)?

#3182: Re:幫 C++ 隨機抽數的程式問題


example (學姊)


好像還要再補一行  temp[i]=false;

可是我加進去都輸出一片空白。

上面的輸出都只有1個人(數字)

請問我要怎麼輸出10個人(數字)?

 幫你修改過囉

#include <iostream>
#include <ctime>

using namespace std;

int main() {
    int i, x, count;
    bool temp[50];

    srand(time(NULL));

    count = 0; // 計數器歸零
    while( count < 10 ) { // 要抓到十個數字
        x = rand()%50;
        if( temp[x] == false ) { // 看有沒有出現過,沒有則進入
            temp[x] = true;
            count++; // 計數器加一
        } else continue; // 這行其實可以省略,加上來是為了邏輯要完整
    }
   
    for( i = 0 ; i < 50 ; i++ )
        if( temp[i] == true ) printf("%d ", i ); // 如果有抽到就輸出囉

    system("pause");
    return 0;

 輸出一個人是因為你只抽一個數字

 用一個計數器才能知道目前抽了幾個

#3183: Re:幫 C++ 隨機抽數的程式問題


massgekevin (瘋狂屎蛋尼)


 幫你修改過囉

#include
#include

using namespace std;

int main() {
    int i, x, count;
    bool temp[50];

    srand(time(NULL));

    count = 0; // 計數器歸零
    while( count < 10 ) { // 要抓到十個數字
        x = rand()%50;
        if( temp[x] == false ) { // 看有沒有出現過,沒有則進入
            temp[x] = true;
            count++; // 計數器加一
        } else continue; // 這行其實可以省略,加上來是為了邏輯要完整
    }
   
    for( i = 0 ; i < 50 ; i++ )
        if( temp[i] == true ) printf("%d ", i ); // 如果有抽到就輸出囉

    system("pause");
    return 0;

 輸出一個人是因為你只抽一個數字

 用一個計數器才能知道目前抽了幾個

謝謝!有點頭緒了

阿....我想問一下 printf = cout ?

不好意思 基本功有點不好!

還有!我compiler後輸出的視窗為空白...

是~~~~?  @@

 



 

#3184: Re:幫 C++ 隨機抽數的程式問題


example (學姊)


 幫你修改過囉

#include
#include

using namespace std;

int main() {
    int i, x, count;
    bool temp[50];

    srand(time(NULL));

    count = 0; // 計數器歸零
    while( count < 10 ) { // 要抓到十個數字
        x = rand()%50;
        if( temp[x] == false ) { // 看有沒有出現過,沒有則進入
            temp[x] = true;
            count++; // 計數器加一
        } else continue; // 這行其實可以省略,加上來是為了邏輯要完整
    }
   
    for( i = 0 ; i < 50 ; i++ )
        if( temp[i] == true ) printf("%d ", i ); // 如果有抽到就輸出囉

    system("pause");
    return 0;

 輸出一個人是因為你只抽一個數字

 用一個計數器才能知道目前抽了幾個

謝謝!有點頭緒了

阿....我想問一下 printf = cout ?

不好意思 基本功有點不好!

還有!我compiler後輸出的視窗為空白...

是~~~~?  @@

 哈哈

 是的 printf 就等於 cout ( 我忘記你用的是 C++ 了 )

 這只是 C 與 C++ 輸出的不同而已

 那行應該改成 cout << i << " ";

 為空白是指?

 貼的是我的程式碼嗎?

#3185: Re:幫 C++ 隨機抽數的程式問題


massgekevin (瘋狂屎蛋尼)


 幫你修改過囉

#include
#include

using namespace std;

int main() {
    int i, x, count;
    bool temp[50];

    srand(time(NULL));

    count = 0; // 計數器歸零
    while( count < 10 ) { // 要抓到十個數字
        x = rand()%50;
        if( temp[x] == false ) { // 看有沒有出現過,沒有則進入
            temp[x] = true;
            count++; // 計數器加一
        } else continue; // 這行其實可以省略,加上來是為了邏輯要完整
    }
   
    for( i = 0 ; i < 50 ; i++ )
        if( temp[i] == true ) printf("%d ", i ); // 如果有抽到就輸出囉

    system("pause");
    return 0;

 輸出一個人是因為你只抽一個數字

 用一個計數器才能知道目前抽了幾個

謝謝!有點頭緒了

阿....我想問一下 printf = cout ?

不好意思 基本功有點不好!

還有!我compiler後輸出的視窗為空白...

是~~~~?  @@

 哈哈

 是的 printf 就等於 cout ( 我忘記你用的是 C++ 了 )

 這只是 C 與 C++ 輸出的不同而已

 那行應該改成 cout << i << " ";

 為空白是指?

 貼的是我的程式碼嗎?

恩  is yours

我是有先把printf改cout了

也是一樣空白...連原本system("pause")

都會顯示 "請按任意鍵繼續.."的字幕都沒有!哈哈

我總覺得都是 在 " false "那裡的問題 ...

#3187: Re:幫 C++ 隨機抽數的程式問題


example (學姊)


#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main() {
    int i, x, count;
    bool temp[50];

      for( i = 0 ; i < 50 ; i++ ) temp[i] = false;

    srand(time(NULL));

    count = 0; // 計數器歸零
    while( count < 10 ) { // 要抓到十個數字
        x = rand()%50;
        if( temp[x] == false ) { // 看有沒有出現過,沒有則進入
            temp[x] = true;
            count++; // 計數器加一
        } else continue; // 這行其實可以省略,加上來是為了邏輯要完整
    }
   
    for( i = 0 ; i < 50 ; i++ )
        if( temp[i] == true ) printf("%d ", i ); // 如果有抽到就輸出囉

    system("pause");
    return 0;

 

恩  is yours

我是有先把printf改cout了

也是一樣空白...連原本system("pause")

都會顯示 "請按任意鍵繼續.."的字幕都沒有!哈哈

我總覺得都是 在 " false "那裡的問題 ...

 紅色那行初始化了

 試試看吧

#3196: Re:幫 C++ 隨機抽數的程式問題


massgekevin (瘋狂屎蛋尼)


#include
#include

using namespace std;

int main() {
    int i, x, count;
    bool temp[50];

      for( i = 0 ; i < 50 ; i++ ) temp[i] = false;

    srand(time(NULL));

    count = 0; // 計數器歸零
    while( count < 10 ) { // 要抓到十個數字
        x = rand()%50;
        if( temp[x] == false ) { // 看有沒有出現過,沒有則進入
            temp[x] = true;
            count++; // 計數器加一
        } else continue; // 這行其實可以省略,加上來是為了邏輯要完整
    }
   
    for( i = 0 ; i < 50 ; i++ )
        if( temp[i] == true ) printf("%d ", i ); // 如果有抽到就輸出囉

    system("pause");
    return 0;

 

恩  is yours

我是有先把printf改cout了

也是一樣空白...連原本system("pause")

都會顯示 "請按任意鍵繼續.."的字幕都沒有!哈哈

我總覺得都是 在 " false "那裡的問題 ...

 紅色那行初始化了

 試試看吧


嗯哼 我試過了!謝謝學姊 ^o^

It's perfectly ~~~~~

抱歉這麼晚回!

今天和家人出去玩!

那...所以是真的false那裡的問題囉?

#3197: Re:幫 C++ 隨機抽數的程式問題


example (學姊)


#include
#include

using namespace std;

int main() {
    int i, x, count;
    bool temp[50];

      for( i = 0 ; i < 50 ; i++ ) temp[i] = false;

    srand(time(NULL));

    count = 0; // 計數器歸零
    while( count < 10 ) { // 要抓到十個數字
        x = rand()%50;
        if( temp[x] == false ) { // 看有沒有出現過,沒有則進入
            temp[x] = true;
            count++; // 計數器加一
        } else continue; // 這行其實可以省略,加上來是為了邏輯要完整
    }
   
    for( i = 0 ; i < 50 ; i++ )
        if( temp[i] == true ) printf("%d ", i ); // 如果有抽到就輸出囉

    system("pause");
    return 0;

 

恩  is yours

我是有先把printf改cout了

也是一樣空白...連原本system("pause")

都會顯示 "請按任意鍵繼續.."的字幕都沒有!哈哈

我總覺得都是 在 " false "那裡的問題 ...

 紅色那行初始化了

 試試看吧


嗯哼 我試過了!謝謝學姊 ^o^

It's perfectly ~~~~~

抱歉這麼晚回!

今天和家人出去玩!

那...所以是真的false那裡的問題囉?

 就是以後一定要記得初始化囉