Chinaunix首页 | 论坛 | 博客
  • 博客访问: 225234
  • 博文数量: 39
  • 博客积分: 1130
  • 博客等级: 少尉
  • 技术积分: 453
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-26 15:54
文章分类

全部博文(39)

文章存档

2012年(1)

2011年(31)

2010年(7)

分类: C/C++

2011-03-06 20:46:09

/*1.编程之前就应该考虑测试;2.问题中的常数通常应写为符号常量是一个编程常识;3.将太长的string literal分段;4.const 省心;5.函数应尽量具有通用性.*/

/*
第一题:
描述:
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.
*/

#include
#include

#define TEST 1           //编程之前就应该考虑测试(运行时将1改为0)

#if TEST
  #define END_NUM       10       //问题给出的常数  
  #define SUM               23
#else
  #define END_NUM       1000       
#endif

  #define BEGIN_NUM     1       
  #define FAC_1             3
  #define FAC_2             5

int sum_of_mul (const int ,const int ,const int ,const int ) ;

int main( void )
{
   printf ( "the sum of all the multiples"   //太长的string literal
            " of %d or %d below %d " ,      //可以用这种方法“分割”
            FAC_1 , FAC_2 , END_NUM       
           ); 
   printf ( "is  %d.\n",
            sum_of_mul ( BEGIN_NUM , END_NUM , FAC_1 , FAC_2  )
           );

              
 #if TEST
   printf ( "= %d\n", SUM ) ;             
 #else
 #endif
 
   system("PAUSE"); 
   return 0;
}

int sum_of_mul (const int begin ,const int end ,
                const int fac1  ,const int fac2 )
{
    int sum = 0 ;
    int i ;
    for( i = begin ; i < end ; i ++ )
      {
         if ( i % fac1 == 0 || i % fac2 == 0 )
           {
             sum += i ;  
           } 
      }
    return sum ;
}

参考博文
http://www.cnblogs.com/zhouyinhui/archive/2011/01/05/1926769.html

阅读(1945) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

KBTiller2011-05-22 23:07:48

liupingforarm: 学习了!.....
谢谢!尚请多多指教

liupingforarm2011-05-06 13:05:18

学习了!