zoukankan      html  css  js  c++  java
  • codeforces 622A Infinite Sequence

    A. Infinite Sequence
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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).

    Examples
    input
    3
    output
    2
    input
    5
    output
    2
    input
    10
    output
    4
    input
    55
    output
    10
    input
    56
    output
    1

    题意:一个数列 1,1,2,1,2,3,1,2,3,4,1,2,3,4,5 .....以此类推给你一个数n问第n个数是多少
    题解:由题知数列的数的个数为1+2+3+4+5+6+.......所以求出第n个数是哪个子序列里的即可(第一个子序列中一个数第二个字序列里两个数第三个里三个数....)
    #include<stdio.h>
    #include<string.h>
    #include<string>
    #include<math.h>
    #include<algorithm>
    #define LL long long
    #define PI atan(1.0)*4
    #define DD double
    #define MAX 5100
    #define mod 10007
    #define dian 1.000000011
    #define INF 0x3f3f3f
    using namespace std;
    int main()
    {
    	LL n,m,j,i,k,t,ans;;
    	while(scanf("%lld",&n)!=EOF)
    	{
    		ans=0;
    		for(i=1;i<2*sqrt(n)+1;i++)
    		{
    			if((i*(i+1)/2)>=n)
    			{
    				ans=i;
    				break;
    			}
    		}
            m=ans*(ans-1)/2;
            m=n-m;
            printf("%lld
    ",m);
    	} 
    	return 0;
    }
    

      

  • 相关阅读:
    235. Lowest Common Ancestor of a Binary Search Tree
    234. Palindrome Linked List
    233. Number of Digit One
    232. Implement Queue using Stacks
    231. Power of Two
    230.Kth Smallest Element in a BST
    229. Majority Element II
    228. Summary Ranges
    postgres 数组中获取最后一个元素的值
    excel 拆分单元格并填充上一行的数据
  • 原文地址:https://www.cnblogs.com/tonghao/p/5236162.html
Copyright © 2011-2022 走看看