Calculating Function

简介: Calculating Function

文章目录

一、Calculating Function

总结


一、Calculating Function

本题链接:Calculating Function


题目:

A. Calculating Function

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

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


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.


Examples


input

4

output

2

input

5

output

-3

Note

f(4) =  - 1 + 2 - 3 + 4 = 2

f(5) =  - 1 + 2 - 3 + 4 - 5 =  - 3

本博客给出本题截图

image.png

题意:输入一个整数,按照上述式子计算最后结果

AC代码

#include <iostream>
using namespace std;
typedef long long LL;
int main()
{
    LL a;
    cin >> a;
    if (a % 2)
        cout << -(a + 1) / 2 << endl;
    else cout << a / 2 << endl;
    return 0;
}

总结

分析一下可以知道这个题暴力会TLE,又没有什么可以优化的方式,显然是一个找规律的题目,打表输出一下就可以知道规律,下面提供一个打表代码

#include <iostream>
using namespace std;
typedef long long LL;
LL f(LL n)
{
    LL res = 0;
    for (int i = 1; i <= n; i ++ )
        if (i % 2) res -= i;
        else res += i;
    return res;
}
int main()
{
    LL a;
    while(cin >> a)
        cout << f(a) << endl;
    return 0;
}
目录
相关文章
|
4月前
|
前端开发 JavaScript 算法
shouldComponentUpdate 是做什么的?
shouldComponentUpdate 是做什么的?
52 0
|
11月前
|
Linux 定位技术 数据安全/隐私保护
【Calculate】Calculate Linux安装操作记录
【Calculate】Calculate Linux安装操作记录
104 0
A. Calculating Function
A. Calculating Function
35 0
|
存储
LeetCode 227. Basic Calculator II
实现一个基本的计算器来计算一个简单的字符串表达式的值。 字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格 。 整数除法仅保留整数部分。
40 0
LeetCode 227. Basic Calculator II
|
Go
HDOJ 1012 u Calculate e
HDOJ 1012 u Calculate e
91 0
HDOJ 1012 u Calculate e
|
算法
HDOJ 1202 The calculation of GPA
HDOJ 1202 The calculation of GPA
73 0
HDOJ 2114 Calculate S(n)(找周期)
HDOJ 2114 Calculate S(n)(找周期)
84 0
|
数据可视化
Paraview: Calculate Derivatives of 3-D Unstructured Dataset
关注九天学者微信公众号(扫码关注)第一时间获取技术贴更新! Paraview 是一款基于VTK的、开源的、跨平台的科学数据可视化软件,其三维显示和后处理功能非常强大。
2664 0
|
JavaScript 安全 前端开发
What Is ElectronJS and Why Should You Use It?
In this three-part tutorial, we will explore how to create a fully functional invoice application using ElectronJS and ApsaraDB for MongoDB.
2616 0
What Is ElectronJS and Why Should You Use It?