由两个平方三位数获得三个平方二位数

来源:互联网 发布:珍宝岛战役 知乎 编辑:程序博客网 时间:2024/05/18 00:00

已知两个平方三位数abc和xyz,其中a、b、c、x、y、z未必是不同的;而ax、by、cz是三个平方二位数。请编程求三位数abc和xyz.
题目分析与算法设计
任取两个平方三位数n和n1,将n从高到低分解为a、b、c,将n1从高到低分解为x、y、z。判断ax、by、cz是否均为完全平方数。

下面为程序说明:

#include <stdio.h>#include <math.h>void fun(int n, float *s);int main(){    int i, t;    float a[3], b[3];    printf("The possible perfect squares combinations are:\n");    for (i = 11; i <= 31; i++)    {        for (t = 11; t <= 31; t++)        {            fun(i*i,a);            fun(t*t,b);           if (sqrt(a[0]*10 + b[0]) == (int)sqrt(a[0]*10 + b[0])            && sqrt(a[1]*10 + b[1]) == (int)sqrt(a[1]*10 + b[1])            && sqrt(a[2]*10 + b[2]) == (int)sqrt(a[2]*10 + b[2]))           {              printf("%d and %d\n",i*i,t*t);           }         }    }    return 0;}void fun(int n, float *s){    int k;    for (k = 1000; k >= 10; s++)    {        *s = (n % k)/(k / 10);        k /= 10;    }}

运行结果:
The possible perfect squares combinations are:
400 and 900
841 and 196

0 0
原创粉丝点击