100c之23:两个平方数

100c之23:两个平方数

100c之23:两个平方数

1 问题

已知两个平方三位数abc和xyz,其中a,b,c,x,y,z未必是不同的;而 ax,by,cy是三个平方二位数,求三位数abc和xyz。

2 分析

穷举。 通过限制穷举的范围可以减少计算次数。

3 解决方案

 1:  /**
 2:   * @file   023c.c
 3:   * @author Chaolong Zhang <emacsun@163.com>
 4:   * @date   Thu May 23 22:15:08 2013
 5:   * 
 6:   * @brief  已知两个平方三位数abc和xyz,其中a,b,c,x,y,z未必是不同的;而 ax,by,cy是三个平方二位数,求三位数abc和xyz。
 7:   * 
 8:   * 
 9:   */
10:  
11:  #include <stdio.h> 
12:  
13:  int is_square (int x, int y);
14:  
15:  int main(int argc, char *argv[])
16:  {
17:    int abc,xyz;
18:    int m,n;
19:    for (m=11; m <= 31; ++m)
20:    {
21:      for (n=10; n <=31; ++n)
22:      {
23:        abc=m*m;
24:        xyz=n*n;
25:        if ( is_square(abc%10, xyz%10) && is_square(abc/10%10, xyz/10%10 ) && is_square(abc/100, xyz/100))
26:        {
27:          printf ("abc=%d, xyz=%d\n",abc, xyz);
28:        }
29:      }
30:    }
31:  
32:    return 0;
33:  }
34:  
35:  int is_square (int x, int y)
36:  {
37:    int i;
38:    for (i=4;i<10 ; i++ )
39:    {
40:      if (i*i == x*10 + y)
41:      {
42:        return 1;
43:      }
44:      else continue;
45:    }
46:    return 0;
47:  }

4 结果

abc = 841;xyz = 196;

Date: 2013-05-23 22:29

Author: emacsun

Org version 7.8.02 with Emacs version 23

Validate XHTML 1.0
原文地址:https://www.cnblogs.com/chaolong/p/3095903.html