CodeForces 622A

本文介绍如何解决在特定数列中查找指定位置元素的问题,通过编程实现找到数列中第n个位置的数值。重点在于理解数列构建规律,并运用算法高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Input
The only line contains integer n (1 ≤ n ≤ 1014) — 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.

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

Sample Input
Input
3
Output
2
Input
5
Output
2
Input
10
Output
4
Input
55
Output
10
Input
56
Output
1

水题直接做:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
int main()
{
    LL n;
    scanf("%I64d",&n);
    LL a=1;
    while(n>a)
    {
        n-=a;
        a++;
    }

    printf("%d\n",n);
    return 0;
}

仅代表个人观点,欢迎交流探讨,勿喷~~~

这里写图片描述

PhotoBy:WLOP

http://weibo.com/wlop

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值