3569 - 7.cpp

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

编程实现:

看不到图的,公式是 :S=1+(1+根号2)+(1+根号2+根号3)+.....+(1+根号2+根号3+......+根号n);<br />

<b><span style="font-size:12.0pt;line-height:150%;font-family:宋体;">整数<span>n</span>通过键盘输入,<span>S</span>的值保留两位小数</span></b>

题目输入

输入一个整数(多组输入数据)

题目输出

输出s的值 (多组输出数据)

输入/输出样例

输入格式

20
15

输出格式

534.19
269.43

C语言解答

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
double fun(int  n)
{  int i;
  double fac=1.0;
  double sum=1.0;
  for(i=2;i<=n;i++) {
    fac+=sqrt(i);
    sum+=fac;
  }
  return sum;
}

int main()
{
   // freopen("in","r",stdin);
  //  freopen("out","w",stdout);

    int  n;    double  s;
 while(scanf("%d",&n)!=EOF)
 {
  s=fun(n);
  printf("%.2f\n",s);
 }



  return 0;
}

C++解答

#include<stdio.h>
#include<math.h>
main()
{
	int n,i;
	float s1,s2;
	while(scanf("%d",&n)!=EOF)
	{	
		s1=0;
		s2=0;
		for(i=1;i<=n;i++)
		{
			s2=s2+sqrt(i);
			s1=s1+s2;
		}
		printf("%.2f\n",s1);
	}
}