請問c++ 五顆骰子two pair

2009-03-26 9:21 am
請問
要怎麼用IF敘述判斷五顆骰子的two pair出來呢?
暴力解也無妨ˊˋ

PS.假設5顆骰子已經random儲存在一個陣列裡面了
a[0],a[1],a[2],a[3],a[4]


THX
更新1:

TO bean_bottom_2 Sure thanks!

更新2:

thanks.. another question is if there is "33314" ,how to judge it as "one pair"?

回答 (1)

2009-03-26 1:30 pm
✔ 最佳答案
Will you take an answer for C (not C++) but you can still compile it under C++ environment?

2009-03-26 05:30:59 補充:
#include <stdio.h>
#include <stdlib.h>

main()
{
int i,pairs;
int b[6];
int a[] = { 5, 3, 4, 3, 5 };

for (i = 0; i < 6; i++) {
b[i] = 0;
}

for (i = 0; i < 6; i++) {
b[a[i]] ++;
}

pairs = 0;
for (i = 0; i < 6; i++) {
if (b[i] == 2) {
pairs ++;
}
}

if (pairs == 2) {
printf("we have 2 pairs\n");
}

printf("================\n");
for (i = 0; i < 5; i++) {
printf("%d\n", a[i]);
}
}

/* fast and quick, you may need some clean up but the idea is there
you may want to compile and print out info out to see how this
works
*/

2009-03-26 05:37:02 補充:
Note that 2,2,2,2,4 is considered 4 of a kind and 3,3,3,2,2 is full house.


收錄日期: 2021-04-30 13:06:02
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090326000015KK00721

檢視 Wayback Machine 備份