您的位置:首页 > 编程语言

编程求具有abcd=(ab+cd)²性质的4位数

2017-05-03 22:49 591 查看
题目分析:具有这种性质的四位数没有分布规律,我们可以用穷举法,对所以四位数进行筛选,找出符合条件的四位数。
具体算法实现:任取一个四位数,将它分为前后两部分,前两位为a,后两位为b,然后套用公式进行计算并判断。
#include <stdio.h>

int main()
{
int n, a, b;

printf("There are following numbers with 4 digits satisfied condition:\n");
for (n = 1000; n < 10000; n++)    /*四位数取值为1000-9999*/
{
a = n / 100;                 /*取n的前两位存于a中*/
b = n % 100;                 /*取n的后两位存于b中*/

if ((a + b)*(a + b) == n)   /*n为符合条件的四位数时,打印出来*/
{
printf("%d\t",n);
}
}
printf("\n");

return 0;
}

运行结果:

There are following numbers with 4 digits satisfied condition:
2025              3035               9801
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 算法 编程