ABCDE x 4 = EDCBA

问题描述:一个五位数字满足 ABCDE x 4 = EDCBA,并且A、B、C、D、E不重复,求解这五个数字。

解决方案1:

#include <stdio.h>

int calc ()
{
for (int i=10001; i<100000; i++)
{
int right =0; /*右边表达式的值*/
int left = i;
while ( left !=0 ) /*求右边的值*/
{
right
= right *10+ left %10;
left
/=10;
}

if ( (i <<2) == right ) /*判断左边是否等于右边*/
{
return i;
}
}

return-1;
}

void main(void)
{
printf(
"the result is : %d\n", calc());
}

解决方案2:

include <stdio.h>

int calc()
{
for(int a=1; a<10; a++)
{
for(int b=0; b<10; b++)
{
for(int c=0; c<10; c++)
{
for(int d=0; d<10; d++)
{
for(int e=0; e<10; e++)
{
int left=a*10000+b*1000+c*100+d*10+e;
int right=e*10000+d*1000+c*100+b*10+a;
if( left==right )
{
return left;
}
}
}
}
}
}
}

void main(void)
{
printf(
"the result is: %d\n", calc());
}
posted @ 2011-05-13 16:03  Gage-Xu  阅读(1233)  评论(0编辑  收藏  举报