csu_1179 sum 数学 计算

1179: Sum

Time Limit: 10 Sec Memory Limit: 128 MB
SUBMIT: 773 Solved: 118
[SUBMIT] [STATUS]

Description

Here is an easy problem.It is so easy that you just need to solve a sum:
∑min(k,max(0,x-k)).The index k is an integer,sum from 0 to +inf.
Give you real x,you should evaluate the sum.

Input

There are sevral test cases.One line for each case containing 1 real x,0<=x<1000000.00.

Output

There should be one output line per test case containing the value of the sum,correct to two decimal places.

Sample Input

0
1
2
4.6

Sample Output

0.00
0.00
1.00
5.20

HINT

直接累加求和了
#include<stdio.h>

double solve(double x)
{
    double k,n,n2,m;
    //尼玛一不小心开始这里直接整形显然会溢出啊。。。
    n=double(int(x));
    n2=double(int(x/2));
    m=n-n2;
    double sum;
    sum=(1+n2)*n2*1.0/2.0-(n2+1 +n)*m*1.0/2.0+m*x;
    return sum;
}
int main()
{
    double x;
    while(scanf("%lf",&x)==1)
        printf("%.2lf\n",solve(x));
    return 0;
}


看人家大牛的标称内心瞬间翻滚了
/*
A:k比x-k小的时候加的是k,当k比x/2大的时候k比x-k大,
加的是x-k,第1~n次x-k与第1~n次k
结合,得到n个x-n,结果就是 (x - n) * n
*/
//题目:NARUTO
//题解:CSGrandeur
//2012.03.05
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
	double x;
	int n;
	while(scanf("%lf", &x) != EOF)
	{
		n = (int)(x + 1) >> 1;
		printf("%.2f\n", (x - n) * n);
	}
	return 0;
}



你可能感兴趣的:(Integer,input,each,n2,output)