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

本文解析了Codeforces上的一道A级题目,该题目要求计算一个特殊的数学序列f(n)=-1+2-3+...+(-1)^n*n。通过分析给出了一种简洁高效的解题思路:根据n的奇偶性进行分情况讨论,对于奇数n结果为(n-1)/2-n,而对于偶数n结果为n/2。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Input

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

Output

Print f(n) in a single line.

Sample Input

4

Sample Output

2

HINT

 

题意
f(n) =  - 1 + 2 - 3 + .. + ( - 1)^n

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

题解:

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

代码

 

#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);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值