Re: [情报]2014年年度NBA推特人物-Manu Ginobili

楼主: simsin5566 (我就是歐洲精靈=))   2014-04-11 18:24:44
真的是语意问题而已
※ 引述《pponywong (pony)》之铭言:
: #include <stdio.h>
: #include <stdlib.h>
: #include <time.h>
: #define BLACK 0
: #define SILVER 1
: typedef struct card_s
: {
: int faces[2];
: } card_t;
: card_t cards[] =
: {
: { SILVER, BLACK },
: { SILVER, SILVER },
: };
: int main(int argc, char** argv)
: {
: int i, idx, face_idx;
: int count, total_count;
: srand(time(NULL));
: count = 0;
: total_count = 0;
: while(total_count < 100000)
: {
: idx = rand() & 0x1;
: face_idx = rand() & 0x1;
face_idx 的出现就表示 抽牌者只看的到一抽出来上方那一面
如果是银色的 就说抽出来的这张牌 "其中一面" 是银色的
如果抽牌者两面都看的到 那face_idx就应该被删掉
跑出来的结果是1/2
: if(cards[idx].faces[face_idx] == SILVER)
: {
: // check wether other side of face is SILVER or BLACK
: if(cards[idx].faces[(face_idx + 1) & 0x1] == SILVER)
: {
: count++;
: }
: total_count++;
: }
: }
: printf("P = %f\n", (float)count / (float)total_count);
: return 0;
: }
附上C++ code
#include <iostream>
#include <ctime>
using namespace std;
#define BLACK 0
#define SILVER 1
class mycard
{
public:
mycard(bool c1, bool c2){
color1 = c1;
color2 = c2;
}
bool color1;
bool color2;
};
int main()
{
srand(time(0));
mycard card[3] = {
mycard(BLACK, BLACK),
mycard(BLACK, SILVER),
mycard(SILVER, SILVER)
};
int counter = 0;
double silver_silver = 0, unknown_silver = 0;
while (counter < 100000)
{
int n_card = rand()%3;
if( card[n_card].color1 == SILVER || card[n_card].color2 == SILVER)
{
unknown_silver++;
if(card[n_card].color1 == SILVER && card[n_card].color2 == SILVER)
silver_silver++;
}
counter++;
}
cout <<"The probability is " << silver_silver/unknown_silver;
system("pause");
return 0;
}
作者: derricklight (derricklight)   2014-04-11 18:47:00
不推别人还以为我不懂

Links booklink

Contact Us: admin [ a t ] ucptt.com