Little Sub and Mr.Potato's Math Problem

Little Sub and Mr.Potato's Math Problem

题目链接

通过打表我们可以发现

  • \(n\)是10的倍数的时候,位置一定是 $\log_{10}k $,如果不是的话就返回0
  • 当n不是10的倍数:
    • 如果位置小于n为最大数时n的位置,返回0
    • 如果位置等于n为最大数时n的位置,返回n
    • 大于的话,就让len++,让n增大,模拟找到合适的位置
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
string a;
ll pos,s[10];
ll gao(){
    int len=(int)a.length();
    ll ans=0,p=0,log=1;
    for(int i=0;i<len;++i){
        s[i]=a[i]-'0';
        p=p*10ll+s[i];
        ans+=p-log;
        log*=10ll;
        if(i!=len-1) ans++;
    }//ans为比n小的有多少
    if(ans+1==pos)return p;
    if(p*10==log)return 0;
    if(pos<ans+1) return 0;
    while(1){
        p*=10ll;
        if(ans+1+p-log>=pos){
            return log+pos-ans-2;
        }//pos-(ans+1)是差多少位 加上log-1就是答案
        ans+=p-log;
        log*=10ll;
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;cin>>t;
    while(t--){
        cin>>a>>pos;
        cout<<gao()<<endl;
    }
    return 0;
}

参考:https://www.cnblogs.com/Dup4/p/10292338.html

猜你喜欢

转载自www.cnblogs.com/smallocean/p/10293601.html