zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 7 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.

    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 test(s)
    input
    3
    output
    2
    input
    5
    output
    2
    input
    10
    output
    4
    input
    55
    output
    10
    input
    56
    output
    1
    没什么好说的,先是一个等差数列求和,然后判断一下就行,如果等于一个和,说明一个循环结束,不是就减去
    #include<stdio.h>
    //#include<bits/stdc++.h>
    #include<string.h>
    #include<iostream>
    #include<math.h>
    #include<sstream>
    #include<set>
    #include<queue>
    #include<map>
    #include<vector>
    #include<algorithm>
    #include<limits.h>
    #define inf 0x3fffffff
    #define INF 0x3f3f3f3f
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define ULL unsigned long long
    using namespace std;
    int t;
    int n,m;
    int sum,ans,flag;
    int a,b,c,d;
    int main()
    {
        LL n;
        LL b;
        LL ans;
        LL i=0;
        LL sum=0;
        cin>>n;
        while(sum<=n)
        {
            i++;
            sum=1*i+(i)*(i-1)/2;
           // i++;
        }
       // cout<<i-1<<endl;
        ans=i-1;
       // cout<<1*ans+(ans)*(ans-1)/2<<endl;
        b=1*ans+(ans)*(ans-1)/2;
        if(n==1*ans+(ans)*(ans-1)/2)
        {
            cout<<ans<<endl;
        }
        else
        {
            cout<<n-b<<endl;
        }
        return 0;
    }
    

      

     
  • 相关阅读:
    【20190226】JavaScript-知识点记录:dom0级事件,dom2级事件
    【20190226】CSS-知识点记录::nth-child,:nth-of-type
    【20190223】HTTP-知识点整理:HTTPS
    【20190221】HTTP-URI与URL
    【20190220】HTTP-知识点整理:TCP/IP与HTTP
    【20190220】JavaScript-知识点整理:对象创建方式、原型、闭包
    【20190219】CSS-知识点整理:float、em、浏览器的渲染过程
    【20190201】天猫首页仿站写完了
    CSS动画
    css工程化
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/5186834.html
Copyright © 2011-2022 走看看