题解 | 无限长正整数排列字符串 | 按位分组定位法

无限长正整数排列字符串

https://www.nowcoder.com/practice/82c92d2321bb4220a3006d52a95a8bdd

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
using ll=long long;

int main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);
    int n; cin>>n;
    
    ll len=0;
    int digit=1;
    ll count=9;

    while (n>len+digit*count) {
        len+=digit*count;
        ++digit;
        count*=10;
    }

    ll remain=n-len;
    ll num=(ll)pow(10,digit-1)+(remain-1)/digit;
    int pos=(remain-1)%digit;

    string s=to_string(num);
    cout<<s[pos]<<'\n';

    return 0;
}

全部评论

相关推荐

回家当保安:今天刚过字节一面,接佬的好运
我的求职进度条
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务