C语言:编程输入正整数m和n(m<=n),求S=1/m+1/(m+1)+1/(m+2)+…+1/(n-1)+1/n。结果保留10位小数,咋写?
代码文本:
#include "stdio.h"
int main(int argc,char *argv[]){
double s;
int m,n;
printf("Enter m & n(int 0<m<=n)...\n");
if(scanf("%d%d",&m,&n)==2 && m>0 && n>=m){
if(n-m>3)
printf("1/%d+1/%d+1/%d+...+1/%d",m,m+1,m+2,n);
else if(m+3==n)
printf("1/%d+1/%d+1/%d+1/%d",m,m+1,m+2,m+3);
else if(m+2==n)
printf("1/%d+1/%d+1/%d",m,m+1,m+2);
else if(m+1==n)
printf("1/%d+1/%d",m,m+1);
else if(m==n)
printf("1/%d",m);
for(s=0.0;m<=n;s+=1.0/m++);
printf(" = %.10f\n",s);
}
else
printf("Input error, exit...\n");
return 0;
}
main( void )
{
int m,n;
double t=0;
scanf("%d%d",&m,&n);
if( m <=0 || m > n )
{
printf("input error!" ) ;
return ;
}
while( m<=n)
t+=1.0/m++;
printf("%.10lf\n",t);
return ;
}