| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1408 人关注过本帖, 1 人收藏
标题:[公告]一道有趣的题
只看楼主 加入收藏
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
结帖率:100%
收藏(1)
 问题点数:0 回复次数:7 
[公告]一道有趣的题

去掉一付扑克牌中的王和K,将剩余的48张牌随机分成几份,依次标记为1--12列。游戏的过程如下。首先翻开第一列的最上面一张牌。假定该牌的数值为n(A,2,3,4,5,6,7,8,9,10,J,Q)则将该张牌放置在第n列的上面,且有数字的一面向上,使之成为第n列的最上面的那张牌。同时取出第n列的最下面一张牌,重复上面的过程,直到4个A全部被翻开为止。试写程序模拟该过程。

要求:

不做任何要求,能够实现上述过程的实现就行。可以 C, 也可以 C++ 风格,鼓励以 C++ 风格写此程序

此程序最佳创作者将予以一定奖励!!!!!!

搜索更多相关主题的帖子: color 风格 
2004-06-18 21:21
wonderfulday
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2004-7-28
收藏
得分:0 

#include<iostream.h>

#define paino 48

class pai{ int no; int back_or_front; public: void get_no(int i){no=i;} int show_no(){return no;} void get_back_or_front(int i){ if(i)back_or_front=1; else back_or_front=0; } int show_back_or_front(){return back_or_front;} };

class action{ pai a[48]; pai form[4][12]; public: void set_pai(){ for(int i=0;i<48;i++){ (i+1)%12==0?a[i].get_no(12):a[i].get_no((i+1)%12); } }

void witsh_pai(){ int temp,i,j; for(i=0;i<6;i++){ temp=a[i].show_no(); a[i].get_no(a[i+6].show_no()); a[i+6].get_no(temp); } for(i=17,j=18;j<24;i--,j++){ temp=a[i].show_no(); a[i].get_no(a[j].show_no()); a[j].get_no(temp); } temp=a[24].show_no(); a[24].get_no(a[25].show_no()); a[25].get_no(temp); temp=a[36].show_no(); a[36].get_no(a[45].show_no()); a[45].get_no(temp); }

void pai_pai(){ int no48=0; for(int i=0;i<4;i++){ for(int j=0;j<12;j++){ form[i][j].get_no(a[no48].show_no()); form[i][j].get_back_or_front(0); no48++; } } }

int pai_firstpick(){ //pick up the first pai,return the pai no return form[0][0].show_no(); }

int pai_put_and_pick(int i){ int temp=form[3][i-1].show_no(); for(int no3=3;no3>0;no3--){ form[no3][i-1].get_no(form[no3-1][i-1].show_no()); form[no3][i-1].get_back_or_front(form[no3-1][i-1].show_back_or_front()); } form[0][i-1].get_no(i); form[0][i-1].get_back_or_front(1); return temp; }

void find_a(){ int i=0; i=pai_firstpick(); while(form[3][0].show_back_or_front()==0){ switch(i){ case 1: i=pai_put_and_pick(i);break; case 2: i=pai_put_and_pick(i);break; case 3: i=pai_put_and_pick(i);break; case 4: i=pai_put_and_pick(i);break; case 5: i=pai_put_and_pick(i);break; case 6: i=pai_put_and_pick(i);break; case 7: i=pai_put_and_pick(i);break; case 8: i=pai_put_and_pick(i);break; case 9: i=pai_put_and_pick(i);break; case 10: i=pai_put_and_pick(i);break; case 11: i=pai_put_and_pick(i);break; case 12: i=pai_put_and_pick(i);break; } } }

void show_pai(){ for(int i=0;i<4;i++){ for(int j=0;j<12;j++){ cout<<form[i][j].show_no()<<" "; } cout<<endl; } }

};

main() { action abc; abc.set_pai(); abc.witsh_pai(); abc.pai_pai(); cout<<"before fine pai"<<endl; abc.show_pai(); abc.find_a(); cout<<endl; cout<<"after find pai"<<endl; abc.show_pai(); }


2004-07-31 18:04
wonderfulday
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2004-7-28
收藏
得分:0 
请教: 洗牌用c++的随机函数写应该怎样写??

2004-07-31 18:08
denglei_318
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-4-11
收藏
得分:0 
有难度!!
2006-04-14 14:55
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 

图片附件: 游客没有浏览图片的权限,请 登录注册

把DEBUG设成1就可以分步显示了。
程序代码:

#include \"stdafx.h\"
#include <deque>
#include <sstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

#define DEBUG 0

typedef enum CardType
{
CardTypeHearts = 3,
CardTypeDiamond,
CardTypeClubs,
CardTypeSpades,
END_PLUS_ONE_CardType
} cardtype;
const char *chararCardFace[] = {\"A \", \"2 \", \"3 \", \"4 \", \"5 \", \"6 \", \"7 \", \"8 \", \"9 \", \"10\", \"J \", \"Q \"};
const cardtype intCardTypeStart = CardTypeHearts;
const int intCardTypeNum = END_PLUS_ONE_CardType - intCardTypeStart;
const int intCardIDNum = sizeof(chararCardFace)/sizeof(char *);

int random(int intUpper)
{
static bInitialized = false;
if (! bInitialized)
{
srand( (unsigned)time( NULL ) );
bInitialized = true;
}
return rand() % intUpper;
}

class CCard
{
public:
CCard(cardtype type, int intID, bool bUp = false) : m_cType(type), m_intID(intID), m_bUp(bUp) {}
void TurnUp() {m_bUp = true;}
int GetID() {return m_intID;}
bool IsUp() {return m_bUp;}
string ToString() const;
private:
cardtype m_cType;
int m_intID;
bool m_bUp;
};

string CCard::ToString() const
{
stringstream ssOutput;
char chrType = m_cType;

if (chrType < intCardTypeStart || chrType > END_PLUS_ONE_CardType
|| m_intID < 0 || m_intID >= intCardIDNum)
return \"UNKNOWN\";

ssOutput << chrType << chararCardFace[m_intID];
if (m_bUp)
ssOutput << \"(Up)\";
else
ssOutput << \"(Dn)\";

return ssOutput.str();
}

ostream& operator << (ostream &o, const CCard &card)
{
return o << card.ToString();
}

class CCardDeck
{
public:
CCardDeck();
string ToString() const;
void Serve();
void Shuffle();
void Play();
private:
deque<CCard *> m_deqpCardColumns[intCardIDNum];
CCard *m_pSEQs[intCardTypeNum * intCardIDNum];
int m_intPlayedColumn;
};

CCardDeck::CCardDeck() : m_intPlayedColumn(0)
{
for (int intTypeCount = 0; intTypeCount < intCardTypeNum; intTypeCount++)
{
for (int intIDCount = 0; intIDCount < intCardIDNum; intIDCount++)
m_pSEQs[intTypeCount * intCardIDNum + intIDCount] = new CCard((cardtype)(intCardTypeStart+intTypeCount), intIDCount);
}
}

string CCardDeck::ToString() const
{
stringstream ssOutput;

for (int intColumnCount = 0; intColumnCount < intCardIDNum; intColumnCount++)
{
ssOutput << \"Column \" << intColumnCount;
for (int intCardCount = 0; intCardCount < m_deqpCardColumns[intColumnCount].size(); intCardCount++)
{
if (m_deqpCardColumns[intColumnCount][intCardCount])
ssOutput << \" \" << *m_deqpCardColumns[intColumnCount][intCardCount];
}
ssOutput << endl;
}

return ssOutput.str();
}

void CCardDeck::Serve()
{
for (int intCardCount = 0; intCardCount < intCardTypeNum; intCardCount++)
{
for (int intColumnCount = 0; intColumnCount < intCardIDNum; intColumnCount++)
m_deqpCardColumns[intColumnCount].push_back(m_pSEQs[intCardCount * intCardIDNum + intColumnCount]);
}
}

void CCardDeck::Shuffle()
{
for (int intCardCount = 0; intCardCount < intCardIDNum*intCardTypeNum; intCardCount++)
swap(m_pSEQs[intCardCount], m_pSEQs[random(intCardIDNum*intCardTypeNum)]);
}

void CCardDeck::Play()
{
CCard *pCard;
int intUpAs = 0;

while (intUpAs < intCardTypeNum)
{
pCard= m_deqpCardColumns[m_intPlayedColumn][m_deqpCardColumns[m_intPlayedColumn].size()-1];
m_deqpCardColumns[m_intPlayedColumn].pop_back();
pCard->TurnUp();
m_intPlayedColumn = pCard->GetID();
m_deqpCardColumns[m_intPlayedColumn].push_front(pCard);
if (!strcmp(chararCardFace[pCard->GetID()], \"A \"))
intUpAs++;
if (DEBUG)
cout << ToString() << endl;
}
}

ostream& operator << (ostream &o, const CCardDeck &deck)
{
return o << deck.ToString();
}

int main(int argc, char* argv[])
{
CCardDeck deck;
deck.Shuffle();
deck.Serve();
cout << deck << endl;
deck.Play();
cout << deck << endl;
return 0;
}

[此贴子已经被作者于2006-4-15 23:40:45编辑过]


http://myajax95./
2006-04-15 05:55
woodhead
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:1124
专家分:0
注 册:2005-7-18
收藏
得分:0 

我也顶一下

[CODE]#include<iostream>
#include<vector>
#include<algorithm>
#include<deque>
#include<ctime>
using namespace std;

struct Piece
{
int number;
int mark;
Piece(int n, int m){ number = n; mark = m; }
};
//-------------------------------------
class Poker48
{
private:
vector<Piece> poker;
const static int max = 48;
public:
Poker48();
void shuffle();
Piece operator[](int i) { return poker[i]; }
};

Poker48::Poker48()
{
for(int m=1; m<5; m++)
for(int n=1; n<13; n++)
poker.push_back( Piece(n,m) );
srand(time(0));
}

void Poker48::shuffle()
{
int j;
for(int i=0; i<max; i++)
{
j = rand()%max;
swap(poker[i], poker[j]);
}
}
//----------------------------------
class PokerTable
{
private:
deque<pair<Piece,bool> > table[12];
public:
void distribute(Poker48&);
void play();
void display();
void displayAll();
};

void PokerTable::distribute(Poker48 &pk)
{
for(int i=0; i<4; i++)
for(int j=0; j<12; j++)
table[j].push_back( pair<Piece,bool>(pk[i*12+j], false) );
}

void PokerTable::play()
{
int condition = 0;
int i = 0;
pair<Piece,bool> tmp = table[i][0];
int index;
while(condition != 4)
{
if(tmp.first.number == 1) condition++;
tmp.second = true;
index = tmp.first.number - 1;
table[index].push_front(tmp);
tmp = table[index][4];
table[index].pop_back();
}
}

void PokerTable::displayAll()
{
for(int i=0; i<12; i++)
{
cout<<i+1<<' ';
for(int j=0; j<table[i].size(); j++)
cout<<'('<<table[i][j].first.number<<','
<<table[i][j].first.mark<<')'<<' ';
cout<<endl;
}
}

void PokerTable::display()
{
for(int i=0; i<12; i++)
{
cout<<i+1<<' ';
for(int j=0; j<table[i].size(); j++)
if(table[i][j].second)
cout<<'('<<table[i][j].first.number<<','
<<table[i][j].first.mark<<')'<<' ';
else
cout<<"( * ) ";
cout<<endl;
}
}
//-------------------------------

int main()
{
char ch = 'y';
while(ch=='y')
{
Poker48 poker;
poker.shuffle();

PokerTable pt;
pt.distribute(poker);

cout<<"\n--original--\n"<<endl;
pt.displayAll();

pt.play();

cout<<"\n--result--\n"<<endl;
pt.display();

cout<<"are you continue ? ( enter 'y' ) ";
cin>>ch;
}
system("pause");
return 0;
}

[/CODE]

弄反了

[此贴子已经被作者于2006-4-15 14:39:31编辑过]


2006-04-15 14:15
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
好强
多学习啊
都没学过..

嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-04-16 14:04
提灯寻影
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2005-4-7
收藏
得分:0 
学习了

2006-04-18 00:02
快速回复:[公告]一道有趣的题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037945 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved