Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2508857
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2012-07-06 17:19:25

    具有这种性质的四位数没有分布规律,可以采用穷举法,对所有四位数进行判断,从而筛选出符合这种性质的四位数。具体算法实现,可任取一个四位数,将其截为两部分,前两位为a,后两位为b,然后套用公式计算并判断。这里不需要像前面的题目那样,求出四位数每一位数是多少,再来组合数字。
    代码如下:

点击(此处)折叠或打开

  1. #include <stdio.h>

  2. int main(int argc, char *argv[])
  3. {
  4.   int a, b, n;
  5.   printf("there are following number with 4 digits satisfied condition\n");
  6.   for(n=1000; n<10000; n++){
  7.     a = n / 100;
  8.     b = n % 100;
  9.     if(n == (a+b) * (a+b))
  10.       printf("%d\n", n);
  11.   }

  12.   return 0;
  13. }
程序执行结果如下:
there are following number with 4 digits satisfied condition
2025
3025
9801
阅读(4315) | 评论(0) | 转发(0) |
0

上一篇:回文数

下一篇:求素数

给主人留下些什么吧!~~