CodeForces - 622A - A. Infinite Sequence

布满荆棘的人生 2022-06-12 13:51 257阅读 0赞

题目连接: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

  1. 3

Sample Output

  1. 2

Sample Input

  1. 5

Sample Output

  1. 2

Sample Input

  1. 10

Sample Output

  1. 4

解题思路

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

AC代码

  1. #include<iostream>
  2. using namespace std;
  3. int main () {
  4. long long n;
  5. while(scanf("%lld", &n) != EOF) {
  6. long long i = 0;
  7. while(n > i) {
  8. n -= i;
  9. i++;
  10. }
  11. printf("%lld\n", n);
  12. }
  13. return 0;
  14. }

发表评论

表情:
评论列表 (有 0 条评论,257人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CodeForces679A

    [CodeForces679A][] 也是交互题,这个要稍微难一些. 考虑的过程大概是: \\(1\\)肯定没有问的价值,如果问过\\(2\\),那么除了\\(4\

    相关 CodeForces1214A

    [CodeForces1214A][] 说起来你们可能不信,这题硬生生卡了我\\(1h\\),我想了背包,扩欧,二分....等等一坨办法.结果最后还是用了\\(bfs\\)

    相关 codeforce 141A

    /字符串问题 没AC的人可能是没看清楚题目吧, 先大概说下题目大意: 给你3个字符串,如果第一个串和第二个串组合在一起可以等于第三个串就输出“Y