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

xiaoxiao2021-02-27  1.2K+

题目分析:具有这种性质的四位数没有分布规律,我们可以用穷举法,对所以四位数进行筛选,找出符合条件的四位数。

具体算法实现:任取一个四位数,将它分为前后两部分,前两位为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

转载请注明原文地址: https://www.6miu.com/read-265.html

最新回复(0)