|
|
发表于 2013-5-27 19:28:37
|
显示全部楼层
#include <stdio.h>
#include <stdarg.h>
// 定义BOOL类型
typedef enum
{
FALSE = 0,
TRUE = 1
}BOOL;
// 定义64位整数类型
#ifdef _WIN32
typedef __int64 INT64;
#else
typedef long long INT64;
#endif
/**************************************************************************
* - 名称: isAllDiff
* - 功能: 判断数字x是否与其他数都不相同
* - 入参: x - 待判断的数字
* count - 需要与x比较的数字的个数
* - 出参: 无
* - 返回: BOOL
* - 备注: 无
**************************************************************************/
BOOL isAllDiff(int x, int count, ...)
{
va_list args;
int i = 0;
va_start(args, count);
for (i = 0; i < count; ++i)
{
if (x == va_arg(args, int)) // 发现有相同直接返回
{
va_end(args);
return FALSE;
}
}
va_end(args);
return TRUE; // x与其他数字均不相同
}
/**************************************************************************
* - 名称: checkSquare
* - 功能: 检查平方数是否满足排他性
* - 入参: square - 平方数
* abcdef - 原数的各位
* - 出参: 无
* - 返回: BOOL
* - 备注: 无
**************************************************************************/
BOOL checkSquare(INT64 square, int a, int b, int c, int d, int e, int f)
{
int digit = 0;
while (0 < square)
{
digit = (int)(square % 10);
if (!isAllDiff(digit, 6, a, b, c, d, e, f))
{
return FALSE;
}
square /= 10;
}
return TRUE;
}
int main()
{
int a, b, c, d, e, f; // 代表6位数abcdef
int number = 0; // 由abcdef组成的6位数
INT64 square = 0; // 6位数number的平方
for (a = 9; a > 0; a--)
{
for (b = 9; b >= 0; b--)
{
if (!isAllDiff(b, 1, a))
{
continue;
}
for (c = 9; c >= 0; c--)
{
if (!isAllDiff(c, 2, a, b))
{
continue;
}
for (d = 9; d >= 0; d--)
{
if (!isAllDiff(d, 3, a, b, c))
{
continue;
}
for (e = 9; e >= 0; e--)
{
if (!isAllDiff(e, 4, a, b, c, d))
{
continue;
}
for (f = 9; f > 0; f--)
{
if (!isAllDiff(f, 5, a, b, c, d, e))
{
continue;
}
// 个位为1或5或6的数不满足排他性
if (1 == f || 5 == f || 6 == f)
{
continue;
}
number = f + e * 10 + d * 100 + c * 1000 + b * 10000 + a * 100000;
square = (INT64)number * (INT64)number;
if (checkSquare(square, a, b, c, d, e, f) && 203879 != number)
{
printf("%d\n", number);
return 0;
}
}
}
}
}
}
}
printf("can not find the number.\n"); // 没有找到满足条件的数字
return 0;
}
|
|