想过二级C的朋友进来!(编程题)
偶以后每天都会发个二级C的编程题上来,
希望各位学C的新手支持偶啦,愿与大家互相学习,共同进步!
今天的题:
输入n输出s,n的范围是大于1小于100,公式s=1+(1+2^0.5)+(1+2^0.5+3^0.5)+(1+2^0.5+3^0.5+……+n^0.5)
偶做的:
#include<math.h>
#include<stdio.h>
double fun(int n){
int i,j;
double sum0=0,sum1=0;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++)
sum0+=sqrt(j);
sum1+=sum0;sum0=0;
}
return sum1;
}
main()
{int n;
printf("enter n:\n");
scanf("%d",&n);
if((n<=1)||(n>=100)){printf("data error!retry!");scanf("%d",&n);}
printf("the s=%lf",fun(n));
}