【数论】[luoguP1895]数字序列

题目

这个题本来自己想了一个方法 用一个数组记录序列的数字 另一个记录到当前这个数字序列一共有多长 结果没想到第一个数组还是需要很大的 所以得换一种方法
于是学了题解的做法
题解
来自题解主人 luoguID 大奕哥

代码如下



#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
int a[40011],s[40011];
int main()
{
     int n;
     for(int i=1;i<=40000;i++)
     {
             int k,m=1;
             k=i;
             while(k/10!=0)
             {
                           m++;
                           k/=10;
            }
             a[i]=a[i-1]+m;
             s[i]=a[i]+s[i-1];
             //cout<<a[i]<<endl;
    }
    cin>>n;
     for(int i=1;i<=n;i++)
     {
             int k,j=0;
             cin>>k;
             while(k>s[j])
             j++;
             int q=k-s[j-1];
             int w=1;
             while(q>a[w+1])
             {
                        w++;
            }
            if(q-a[w]==0)
            {cout<<w%10<<endl;k=0;}//注意要特判,一旦要是第一个数就无法输出
            if(k!=0)
             {q-=a[w];
             char x[10];
             w++;
             sprintf(x+1,"%d",w);
             cout<<x[q]<<endl;}
    }
    system("pause");
    return 0;
}


//COYG
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值