您的位置:首页 > 其它

Codeforces Round #277 (Div. 2) A. Calculating Function 水题

2015-10-25 13:30 323 查看

A. Calculating Function

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/486/problem/A

Description

For a positive integer n let's define a function f:

f(n) =  - 1 + 2 - 3 + .. + ( - 1)nn

Your task is to calculate f(n) for a given integer n.

[b]Input[/b]

The single line contains the positive integer n (1 ≤ n ≤ 1015).

[b]Output[/b]

Print f(n) in a single line.

[b]Sample Input[/b]

4

[b]Sample Output[/b]

2

HINT

[b]题意[/b]
f(n) =  - 1 + 2 - 3 + .. + ( - 1)^n

给你n,然后让你求f(n)

[b]题解:[/b]

分奇偶啦,奇数就是(n-1)/2-n,偶数就是n/2

[b]代码[/b]

#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
long long n;scanf("%lld",&n);
if(n%2==1) printf("%lld\n",(n-1LL)/2LL - n);
else printf("%lld\n",n/2LL);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: