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

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

题意:

PS:
如果用循环,结果当n很大时候,会超时,要求1000 ms。
因为-n + n + 1 可以发现,偶数个时候,会有n/2个1。
奇数个呢,n-1为偶数,n-1后除2-n既可。

代码:

1
2
3
4
5
6
7
8
9
10
#include <bits/stdc++.h>
using namespace std;
long long n;

int main() {
cin >> n;
if (n % 2 == 0) cout << n / 2 << endl;
else cout << n / 2 - n << endl;
return 0;
};