代碼給妳拷上來,壹些不必要的部分妳自己改壹下就可以了。
當時隨便做的,還可以再進壹步優化的。
另:回樓上的,這題主要是三次排序的問題。第壹次選擇壹個行數,然後把那行的元素分成三份到每壹行去,第二次再選壹行,那麽把那壹行的第壹次就有的三個元素再分布到每行中去,這樣,最後每行只剩下壹個兩次都選擇了的元素。那麽最後再選壹行答案就是唯壹的了。
#include<iostream>
#include<string>
#include<time.h>
using namespace std;
namespace variable
{
int used;
static int i;
}
namespace array
{
int used[27]={0};
}
/*自定義函數部分*/
void drawline(int n)//畫線函數
{
if(n==1)
cout<<"----------------------------";
else
cout<<"-------------------------------------------------------------------------------";
}
//打印版權信息~~嘿嘿~~~~~自己寫的程序~~~標個版權~~
void printcopy(){
int short_line(1);
drawline(short_line);
cout<<"Copyright(c) SehDan.Lee";
drawline(short_line);
cout<<endl;
}
//函數:為名為poker的string類型數組賦初值,作用:生成壹副撲克牌
void push_poker(string *str,int len)
{
string list[15];
for(int i=3;i<18;i++)
switch(i)
{
case 10:list[i-3]="10";break;
case 11:list[i-3]='J';break;
case 12:list[i-3]='Q';break;
case 13:list[i-3]='K';break;
case 14:list[i-3]='A';break;
case 15:list[i-3]='2';break;
case 16:list[i-3]="KING2";break;
case 17:list[i-3]="KING1";break;
default:list[i-3]=i+48;
}
string type[4]={"a","b","c","d"};
i=0;
for(int j=0;j<13;j++)
{
for(int k=0;k<4;k++)
{
*(str+i)=type[k]+"-"+list[j];
i++;
}
}
for(i=0;i<2;i++)
*(str+len+i-2)=list[i+13];
}
//函數:檢測生成的撲克牌是否已被使用
int chk_used(int *p,int i)
{
for(int j=0;j<27;j++)
{
if(*(p+j)==i)
{
return 1;
break;
}
}
return 0;
}
//函數:標記已使用的撲克牌
void used(int *p,int i)
{
*(p+variable::i)=i;
variable::i++;
}
//數組置零
void set_int_array(int *p,int len)
{
for(int i=0;i<len;i++)
*(p+i)=0;
}
//抽取撲克
void set_poker(string *str,string *p_poker,int n)
{
for(int i=0;i<n;i++)//隨機抽取n張牌
{
srand((unsigned)time(NULL));//以系統時間為種子,產生不同的隨機值
variable::used=rand()%54;
while(chk_used(array::used,variable::used)==1){variable::used=rand()%54;}//確保產生不重復的隨機值
used(array::used,variable::used);//標記產生的隨機值,確保下次不再出現
*(p_poker+i)=*(str+variable::used);
}
variable::i=0;
set_int_array(array::used,27);
}
//出題函數
void print_area(string *str,int times,int row,int row_2,int *r)
{
int p_row,i,n_list[27]={0};
for(i=0;i<27;i++)
n_list[i]=i;
if(times!=1)
{
//接下來處理第壹次收集的row信息
int t;
for(i=0;i<3;i++)
{
t=n_list[i];
n_list[i]=n_list[(row-1)*9+i];
n_list[(row-1)*9+i]=t;
}
for(i=0;i<3;i++)
{
t=n_list[i+12];
n_list[i+12]=n_list[(row-1)*9+i+3];
n_list[(row-1)*9+i+3]=t;
}
for(i=0;i<3;i++)
{
t=n_list[i+24];
n_list[i+24]=n_list[(row-1)*9+i+6];
n_list[(row-1)*9+i+6]=t;
}
//第二次收集到的row信息(row_2)
if(times==3)
{
for(i=0;i<3;i++)
{
t=n_list[(row_2-1)*12+i];
n_list[(row_2-1)*12+i]=n_list[i*9];
n_list[i*9]=t;
}
for(i=0;i<27;i++)
*(r+i)=n_list[i];
}
}
for(i=0;i<27;i++)
{
if(i%9==0)
cout<<endl<<"Line "<<i/9+1<<":";
cout.width(8);
cout<<*(str+n_list[i]);
}
cout<<endl;
}
/*主函數*/
void main()
{
int again=1;
int times,row,row_2,row_last,r[27],long_line=0;
string poker[54],p_poker[27];
push_poker(poker,54);
while(again)
{
times=1,row=0,row_2=0,row_last=0;
printcopy();
set_poker(poker,p_poker,27);
while(times!=4)
{
drawline(long_line);
print_area(p_poker,times,row,row_2,r);
drawline(long_line);
cout<<endl;
if(!row)
cout<<"Remember a card,and tell me what line it reside in(1/2/3):";
else
cout<<"What line the card you remembered reside in now(1/2/3):";
if(!row)
cin>>row;
else if(!row_2)
cin>>row_2;
times++;
}
int final;
cin>>final;
drawline(long_line);
cout<<"\nYour remembered card is: "<<p_poker[r[(final-1)*9]]<<endl;
cout<<"Donot believe it? Try again?(1 for yes,0 for no)";
cin>>again;
cout<<"\n\n\n";
if(again!=1&&again!=0)
{
cout<<"error!";
break;
}
}
}