POJ 1844 Sum

简介: POJ 1844 Sum

Description


Consider the natural numbers from 1 to N. By associating to each number a sign (+ or -) and calculating the value of this expression we obtain a sum S. The problem is to determine for a given sum S the minimum number N for which we can obtain S by associating signs for all numbers between 1 to N.


For a given S, find out the minimum value N in order to obtain S according to the conditions of the problem.

Input


The only line contains in the first line a positive integer S (0< S <= 100000) which represents the sum to be obtained.

Output


The output will contain the minimum number N for which the sum S can be obtained.

Sample Input


12

Sample Output


7


第一次知道了,打表法原来也是要消耗时间的,只是相对少些;

还有,用scanf输入比用cin输入要节约时间;scanf是格式化输入,printf是格式化输出。

cin是输入流,cout是输出流。效率稍低,但书写简便。

格式化输出效率比较高,但是写代码麻烦。

流输出操作效率稍低,但书写简便。

cout之所以效率低,是先把要输出的东西存入缓冲区,再输出,导致效率降低。

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXX 100010
using namespace std;
int a[MAXX];
void aa()
{
    a[0]=0;
    for(int j=1; j<=MAXX; j++)
    {
        a[j]=a[j-1]+j;
    }
}
int main()
{
    aa();
    int n;
    scanf("%d",&n);
    int k;
    for(int j=1;j<n; j++)
    {
        if(a[j]>=n)
        {
            k=a[j]-n;
            if(k%2==0)
            {
                printf("%d\n",j);
                return 0;
            }
        }
    }
    return 0;
}
目录
相关文章
|
11月前
|
存储 算法 安全
LeetCode - #1 Two Sum
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #1 Two Sum
|
12月前
|
机器学习/深度学习
leetcode - two-sum
leetcode - two-sum
64 0
|
机器学习/深度学习
POJ 1775 (ZOJ 2358) Sum of Factorials
POJ 1775 (ZOJ 2358) Sum of Factorials
122 0
|
机器学习/深度学习 Android开发 C++
LeetCode之Two Sum
LeetCode之Two Sum
98 0
|
算法 索引 C++
|
存储 人工智能 算法
HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29942    Accepted Submissio...
2362 0
LeetCode - 15. 3Sum
15. 3Sum Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数列,找出这个数列中和为0的三元组.
821 0
LeetCode - 1. Two Sum
1. Two Sum  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数组nums和一个数target,求:id1和id2.
776 0