Chinaunix首页 | 论坛 | 博客
  • 博客访问: 239185
  • 博文数量: 47
  • 博客积分: 1229
  • 博客等级: 中尉
  • 技术积分: 568
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-20 10:06
文章分类

全部博文(47)

文章存档

2014年(1)

2013年(7)

2012年(1)

2011年(38)

分类: C/C++

2011-03-02 23:06:44


Problem

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.



  1. #include <stdio.h>


  2. #define NUM    999

  3. int main(int argc, const char *argv[])
  4. {
  5.     int i, sum = 0;
  6. #if 0

  7.     for (i = 0; i <= NUM; i++) {
  8.         if (!(i%3 && i%5)) {
  9.             sum += i;
  10.         }

  11.     }
  12.     printf("sum = %d\n", sum);

  13. #else
  14.     sum = 0;
  15.     sum += (3+(NUM - (NUM%3)))* (NUM/3)/2 ;
  16.     sum += (5+(NUM - (NUM%5)))* (NUM/5)/2 ;
  17.     sum -= (15+(NUM - (NUM%15)))* (NUM/15)/2 ;
  18.     printf("sum = %d\n", sum);
  19. #endif


  20.     return 0;
  21. }
阅读(1471) | 评论(0) | 转发(0) |
0

上一篇:Dismayed

下一篇:euler2

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