早期我们用来入门学习的方法:


[cpp] view plain copy
  1. <span style="font-size:14px;"><strong style="background-color: rgb(255, 255, 102);">#include <stdio.h>  
  2. int main ()  
  3. {  
  4.     int sign = 1;  
  5.     double deno = 2.0,sum = 1.0,term;  
  6.     while(deno <= 100)  
  7.     {  
  8.         sign = -sign;  
  9.         term = sign/deno;  
  10.         sum = sum + term;  
  11.         deno = deno+1;  
  12.     }  
  13.     printf("the sum is %f\n",sum);  
  14.     return 0;  
  15. }</strong></span>  


现在我们可以在变量名和操作符上做一些改变,使程序看起来简便一点【C语言】计算1-1/2+1/3-.......+1/99-100的值


[cpp] view plain copy
  1. <span style="font-size:14px;"><strong style="background-color: rgb(255, 255, 102);">#include <stdio.h>  
  2. int main ()  
  3. {  
  4.     int i = 1;  
  5.     double j;  
  6.     double sum = 1;  
  7.     for(j=2; j<=100; j++)  
  8.     {  
  9.         i = -i;  
  10.         sum += i/j;  
  11.     }  
  12.     printf("the sum is %f\n",sum);  
  13.     return 0;  
  14. }</strong></span>  


【C语言】计算1-1/2+1/3-.......+1/99-100的值

相关文章: