题目
描述
变种水仙花数 - Lily Number:把任意的数字,从中间拆分成两个数字,比如1461 可以拆分成(1和461),(14和61),(146和1),如果所有拆分后的乘积之和等于自身,则是一个Lily Number。
例如:
655 = 6 * 55 + 65 * 5
1461 = 1461 + 1461 + 146*1
求出 5位数中的所有 Lily Number。
输入描述:
无
输出描述:
一行,5位数中的所有 Lily Number,每两个数之间间隔一个空格。
题解思路
1,需要函数判断是否为 Lily Number 。
- 观察可知一个数是被分为两个部分,可以用 / 和 % 来取得
- 再根据相乘相加在判断即可
2,主函数打印五位数的 Lily Number。
代码实现
#include <stdbool.h>
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
bool islilynum(int n)
{
int bit = 10;
int front = 0, end = 0;
int res = 0;
while (bit <= 10000)
{
front = n / bit;
end = n % bit;
res += front * end;
bit *= 10;
}
if (res == n)
{
return true;
}
return false;
}
int main()
{
int a = 10000, b = 99999;
while (a <= b)
{
if (islilynum(a))
{
printf("%d ",a);
}
a++;
}
return 0;
}