c語言使用亂數隨意抽出五張牌,檢查這五張牌是否為同花色? |
答題得分者是:jow
|
n890377
一般會員 發表:17 回覆:16 積分:21 註冊:2006-07-13 發送簡訊給我 |
請教各位大大:我的題目是使用亂數隨意抽出五張牌,檢查這五張牌是否為同花色?
以下是我的演算法流程 但是我不知道應該如何去寫code 請大家幫幫我一下 謝謝!!! 先用一個二微陣列,陣列大小為[4,13],做兩個亂數種子,第一個0~3第二個0~12去抽, 只抽五張牌所以不會有用完的情況,第一個亂數總子就讓他隨意去跑吧, 至於第二個亂數種子就不能出現重複問題,所以有兩種方式,第一種就是抽完後跟前面幾張比對, 第二種是抽到後直接把放這張牌的位置設為空的, 最後就剩下判斷,你只是要看是不是同花,那就看每張牌的第一個亂數總子是否都相同,就可以知道了。
------
. |
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
以Set來Keep已經發出的牌..
[code cpp] //--------------------------------------------------------------------------- #ifndef fMainH #define fMainH //--------------------------------------------------------------------------- #include #include #include #include <Forms.hpp><br />//--------------------------------------------------------------------------- //typedef Set //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: TButton *Button1; TLabel *Label1; TLabel *Label2; void __fastcall Button1Click(TObject *Sender); public: __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif [/code] [code cpp] //--------------------------------------------------------------------------- #include #pragma hdrstop #include "fMain.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { randomize; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { typedef Set //隨機抽牌 Card card; byte hand[5]; int count=0; do{ byte n = random(52); if(!card.Contains(n)){ card< } }while(count<5); //顯示 String T, S="{"; for(int i=0;i<5;i ) S =T.sprintf("(%d,%d),",hand[i]/13,hand[i]); S[S.Length()]='}'; Label1->Caption=S; //檢查是否同花色並顯示結果 int k=hand[0]/13; //0:spade, 1:heart, 2:dimond, 3:club bool b=true; for(int i=1;i<5;i ){ if(hand[i]/13!=k){ b=false; break; } } if(b)Label2->Caption="同花色"; else Label2->Caption="不同花色"; } //--------------------------------------------------------------------------- [/code]
編輯記錄
jow 重新編輯於 2007-11-09 08:22:15, 註解 無‧
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |