poj 1019 Number Sequence && hdu 1597 find the nth digit(二分,模拟)

40 篇文章 0 订阅
27 篇文章 0 订阅

hdu1597 地址:点击打开链接

poj 地址:点击打开链接


两个题很相似,都是问你第i位是几,但排列的顺序有点不太一样.

hdu 1597的每个小串是前一个小串后加个1-9中的一个循环添加;

poj 1019的每个小串是前一个小串后加(i+1)


思路都一样,想办法记录截止到某个小串其前缀一共有几位,这样就可以二分快速找出第i

位在哪个小串,知道小串后,hdu在小串中因为都是1-9循环所以模9就是答案(注意

9%9==0); poj在小串中又是12345678910111213....所以还要二分出具体在哪个数字,知

道在哪个数字后再对这个数字拆解就能找到那位数。


hdu 1597:

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn = 1<<19;
const ll INF = 0x7fffffff;
ll a[maxn], k;

void init()
{
    for(k = 1; a[k-1]+k <= INF; k++)
        a[k] = a[k-1]+k;
}

int main(void)
{
    int t;
    init();
    cin >> t;
    while(t--)
    {
        ll n;
        scanf("%lld", &n);
        ll l = 1, r = k-1, ans;
        ans = lower_bound(a, a+k, n)-a;
        if(n == a[ans]) printf("%d\n", ans%9 == 0 ? 9 : ans%9);
        else printf("%d\n", (n-a[ans-1])%9 == 0 ? 9 : (n-a[ans-1])%9);
    }
    return 0;
}



 poj 1019:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
const int INF = 0x7fffffff;
ll group[maxn], sum[maxn];

int main(void)
{
    int i, t;
    cin >> t;
    for(i = 1; ; i++)
    {
        group[i] = group[i-1]+1+(i/10>0)+(i/100>0)+(i/1000>0)+(i/10000>0)+(i/100000>0);
        sum[i] = sum[i-1]+group[i];
        if(sum[i] > INF) break;
    }
    while(t--)
    {
        ll n;
        scanf("%lld", &n);
        ll ans = lower_bound(sum, sum+i, n)-sum;
        n = n-sum[ans-1];
        ans = lower_bound(group, group+i, n)-group;
        n -= group[ans-1];
        int len = 0, tmp[15];
        while(ans)
        {
            tmp[len++] = ans%10;
            ans /= 10;
        }
        printf("%d\n", tmp[len-n]);
    }
    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值