您的位置:首页 > 其它

HDU 1716(排列2)解题纠错

2011-04-03 08:31 441 查看
以下来自http://zhidao.baidu.com/question/246928784.html

#include <stdio.h>
#include <string.h>
int main()
{ int a[4];
int che_c[24][4];
bool change=false;
while(scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3])!=EOF)
{
int x,y,z,m;
int hang=0;
for(x=0;x<4;x++)
{ //printf("%d",a[x]);
for(y=0;y<4;y++)
{ if(y!=x)
for(z=0;z<4;z++)
{ if(z!=x&&z!=y)
for(m=0;m<4;m++)
{ if(m!=x&&m!=y&&m!=z)
{//printf("%c%c%c%c",a[x],a[y],a[z],a[m]);
che_c[hang][0]=a[x];
che_c[hang][1]=a[y];
che_c[hang][2]=a[z];
che_c[hang][3]=a[m];
hang++;
}
}
}
}
}
int check=0;
int move=0;
int repeat=0;
while(check<24)
{
while(move<24&&che_c[check][3]!=-25670)
{
if(che_c[move][0]==che_c[check][0]&&che_c[move][1]==che_c[check][1]&&che_c[move][2]==che_c[check][2]&&che_c[move][3]==che_c[check][3])
{
repeat++;
if(repeat>=2)
che_c[move][3]=-25670;
//break;
}
move++;
}
check++;
move=check;
//move=0;
repeat=0;
}
int output=0;
int hang_t=0;
while(output<24)
{
if(che_c[output][3]!=-25670)
{//if(che_c[output][0]!=che_c[output-1][0]&&output>0)
//printf("/n");
printf("%d%d%d%d",che_c[output][0],che_c[output][1],che_c[output][2],che_c[output][3]);
printf(" ");

}
output++;
}
printf("/n");
}
}


(1)最后一组数据后面没有空行;
(2)千位数字相同的在同一行,即如果第1位不同,则应换行,而且行尾不能有空格;
(3)输出4位数,即第1位是0不要输出;
(4)没有实现"如果四张卡片都是0,则输入结束"的要求.

以下修改AC:
#include <stdio.h>
#include <string.h>
int main()
{ int a[4];

int che_c[24][4];
int case_number = 0; //added
//bool change=false;
//while(scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3])!=EOF)
while(scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3])!=EOF && (a[0]||a[1]||a[2]||a[3]))
{
if (case_number > 0) printf("/n"); //added
case_number++; //added
int x,y,z,m;
int hang=0;
for(x=0;x<4;x++)
{ //printf("%d",a[x]);
for(y=0;y<4;y++)
{ if(y!=x)
for(z=0;z<4;z++)
{ if(z!=x&&z!=y)
for(m=0;m<4;m++)
{ if(m!=x&&m!=y&&m!=z)
{//printf("%c%c%c%c",a[x],a[y],a[z],a[m]);
che_c[hang][0]=a[x];
che_c[hang][1]=a[y];
che_c[hang][2]=a[z];
che_c[hang][3]=a[m];
hang++;
}
}
}
}
}
int check=0;
int move=0;
int repeat=0;
while(check<24)
{
while(move<24&&che_c[check][3]!=-25670)
{
if(che_c[move][0]==che_c[check][0]&&che_c[move][1]==che_c[check][1]&&che_c[move][2]==che_c[check][2]&&che_c[move][3]==che_c[check][3])
{
repeat++;
if(repeat>=2)
che_c[move][3]=-25670;
//break;
}
move++;
}
check++;
move=check;
//move=0;
repeat=0;
}
int output=0;
//int hang_t=0;
int out_count = 0; //added
int last_number; //added
while(output<24)
{
if(che_c[output][3]!=-25670 && che_c[output][0])//if(che_c[output][3]!=-25670)
{//if(che_c[output][0]!=che_c[output-1][0]&&output>0)
//printf("/n");
if (out_count > 0) //added
{
printf("%c", last_number == che_c[output][0] ? ' ' : '/n');//printf(" ");
}
last_number = che_c[output][0]; //added
out_count++; //added
printf("%d%d%d%d",che_c[output][0],che_c[output][1],che_c[output][2],che_c[output][3]);
}
output++;
}
printf("/n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: