10分,求调

P3612 [USACO17JAN] Secret Cow Code S

Mei20 @ 2024-10-04 21:15:12

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
string s;
long long n;
long long cnt,sum,k;
int main()
{
    cin>>s>>n;
    sum=cnt=s.size();

    while(sum<n) sum*=2;
    while(n>cnt)
    {
        if(n==sum/2+1)
            n--;
        else
            n=n-1-sum/2;
        sum/=2;
    }
    cout<<s[n-1];
    return 0;
}

by sb458hhhhhhh @ 2024-10-04 23:14:59

第一个if sum没/2 我写了,但错了,也是十分(恼)


by hp13307747077 @ 2024-10-10 12:40:34

#include<bits/stdc++.h>
using namespace std;
string s;
long long n,num,i;
int main()
{
    cin>>s>>n;
    num=s.length(); 
    while(num<n)
    {
        i=num;
        while(n>i)
        {
            i*=2;
        }
        i=i/2;
        n-=(i+1); 
        if(n==0)
        {
            n=i;
        }
    }
    cout<<s[n-1]<<endl;
    return 0;
}

|