求∑k(1~100)+∑k*k(1~50)+∑1/k(1~10).
这道题,我们可以其中的三项,然后进行加法运算即可,因此,可以将其写成三个函数,一次调用。代码如下:
#include <stdio.h>
int sumnum(int,int); int pingfang(int,int); float fenshu(int); int main(int argc,int *argv[]) { double result = 0; int temp; float temp_float;
temp = sumnum(1,100); printf("1+2+3+...+100 = %d\n",temp); result += temp; temp = pingfang(1,50); printf("1*1 + 2*2 + 3*3 +...+50*50 = %d\n",temp); result += temp;
temp_float = fenshu(10); printf("1/1 + 1/2 +1/3 +...+1/10=%f\n",temp_float); result += temp_float; printf("the result is :%f\n",result); system("pause"); return 0; }
int sumnum(int begin,int end) { int i,result = 0; for (i = begin; i <= end; i++) { result += i; } return result; }
int pingfang(int begin,int end) { int i,result = 0; for(i = begin; i <= end; i++) { result += i * i; } return result; }
float fenshu(int end) { int i; float result = 0; for (i = 1; i <= end; i++) { result += 1.0 / i; } return result; }
|
阅读(534) | 评论(0) | 转发(0) |