CodeForces

来源:互联网 发布:萧山网络问政进化镇 编辑:程序博客网 时间:2024/06/01 20:51

题目连接:http://codeforces.com/problemset/problem/622/A

题目描述

Description

Consider the infinite sequence of integers: 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5…. The sequence is built in the following way: at first the number 1 is written out, then the numbers from 1 to 2, then the numbers from 1 to 3, then the numbers from 1 to 4 and so on. Note that the sequence contains numbers, not digits. For example number 10 first appears in the sequence in position 55 (the elements are numerated from one).

Find the number on the n-th position of the sequence.

考虑一个有限序列,1,1,2,1,2,3,1,2,3,4,1,2,3,4,5…这个序列是按照以下的规则生成的:第一个数是1,接下来是1和2,接下来是1,2, 3,接下来是1,2,3, 4,等等。例如,第55个数是10。
现在,给你一个n,求出这个有限序列中第n个数是多少。

Input

The only line contains integer n (1 ≤ n ≤ 10^14) — the position of the number to find.

Note that the given number is too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.

输入只有一行一个正整数n(1<=n<=10^14) ,表示要找的数的位置。
注意,n较大,所以可能需要使用int64或者long long。

Output

Print the element in the n-th position of the sequence (the elements are numerated from one).

输出序列中第n个位置上的数。

Sample Input

3

Sample Output

2

Sample Input

5

Sample Output

2

Sample Input

10

Sample Output

4

解题思路

每次减去一个从0开始的公差为1的数

AC代码

#include<iostream>using namespace std;int main () {    long long n;    while(scanf("%lld", &n) != EOF) {        long long i = 0;        while(n > i) {            n -= i;            i++;        }        printf("%lld\n", n);    }     return 0; }
原创粉丝点击