京东校招规律题详解

题目描述

东东从京京那里了解到有一个无限长的数字序列: 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, ...(数字k在该序列中正好出现k次)。东东想知道这个数字序列的第n项是多少,你能帮帮他么

输入描述:

输入包括一个整数n(1 ≤ n ≤ 10^18)

输出描述:

输出一个整数,即数字序列的第n项

示例1

输入

169

输出

18

这是典型的规律题目,等价于 n>1+2+3+m-1 and n<1+2+3...+n , 可以用数学的方法解决,但要特别注意ceil是向上取整,floor是向下取整

#include <iostream>
#include <cmath>
using namespace std;
typedef long long LL;

// mid*(mid+1)/2 > n
// mid*(mid-1)/2 < n

int main()
{
    LL n;
    cin>>n;
    LL res = ceil((-1.0+sqrt(8*n+1))/2);
    cout<<res<<endl;
}

猜你喜欢

转载自blog.csdn.net/wwxy1995/article/details/89438633
今日推荐