zoukankan      html  css  js  c++  java
  • POJ 1019 Number Sequence

    题意:问有如下规律的数列的第n项是啥:

    11212312341234512345612345671234567812345678912345678910123456789101112345678910

    解法:每段连续数字为一组,算每组序列的长度,累加直到超过n,说明n在前一组连续数字内,枚举组内数字,累加长度直到超过n,说明n在前一数字中,找出这一数位输出。

    总的来说就是一有点恶心的模拟……最后我想说……

    注意longlong

    对……每次都记不住……_(:з」∠)_

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<limits.h>
    #include<time.h>
    #include<stdlib.h>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #define LL long long
    
    using namespace std;
    
    int main()//变量名起的变幻莫测不忍吐槽嗷
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            LL n;
            scanf("%I64d", &n);
            LL sum = 0, x = 0, i = 1;
            for(; n > sum; i++)
            {
                x += (LL)log10(i + 0.5) + 1;
                sum += x;
            }
            i--;
            LL tmp = n - (sum - x);
            LL sum1 = 0;
            for(LL j = 1; j <= i; j++)
            {
                sum1 += (LL)log10(j + 0.5) + 1;
                if(sum1 >= tmp)
                {
                    if(sum1 > tmp)
                        sum1 -= (LL)log10(j + 0.5) + 1;
                    LL ttmp = tmp - sum1;
                    vector <int> ans;
                    LL x1 = j;
                    while(x1)
                    {
                        ans.push_back(x1 % 10);
                        x1 /= 10;
                    }
                    if(ttmp == 0) ttmp = ans.size();
                    printf("%d
    ", ans[ans.size() - ttmp]);
                    break;
                }
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    js截取字符串区分汉字字母代码
    List 去处自定义重复对象方法
    63. Unique Paths II
    62. Unique Paths
    388. Longest Absolute File Path
    41. First Missing Positive
    140. Word Break II
    139. Word Break
    239. Sliding Window Maximum
    5. Longest Palindromic Substring
  • 原文地址:https://www.cnblogs.com/Apro/p/4790130.html
Copyright © 2011-2022 走看看