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;
    }
    

      

  • 相关阅读:
    linux中和salt中的fqdn测试小节
    centos7离线安装rpm包自动解决依赖
    (转)mysql创建表时反引号的作用
    mysql更新一个表里的字段等于另一个表某字段的值
    Navicat permium工具连接Oracle的配置
    IA64与x64的区别
    vsphere和vmware快照的不足之处
    mysql查看某库表大小
    sql之left join、right join、inner join的区别(转)
    读锁和写锁
  • 原文地址:https://www.cnblogs.com/tonghao/p/5236162.html
Copyright © 2011-2022 走看看