HDU1012 计算e~
其实主要部分很简单,就是阶乘和累加,关键是输出的格式不好弄,一开始怎么都和标准输出不一致,后来发现输出的格式可以人为控制一下下~~~
贴代码~
1 #include <stdio.h> 2 3 int jiechen(int x){ 4 int i, sum = 1; 5 for(i = 1;i <= x;i++){ 6 sum *= i; 7 } 8 return sum; 9 } 10 11 int main(){ 12 int i,n; 13 double ans=1.0; 14 15 printf("n e\n- -----------\n"); 16 printf("0 1\n"); 17 for(n = 1; n < 10; n++){ 18 for(i = 1; i <= n; i++){ 19 ans += 1/(double)jiechen(i); 20 } 21 if(n == 1){ 22 printf("%d %.0f\n",n,ans); 23 } 24 else if(n == 2){ 25 printf("%d %.1f\n",n,ans); 26 } 27 else{ 28 printf("%d %.9f\n",n,ans); 29 } 30 31 ans=1.0; 32 } 33 getchar(); 34 return 0; 35 }
Everything will be ok in the end. If it is not ok then it is not the end.