寒假每日一题day5 1346. 回文平方(无脑暴力判断回文) + 我爱喝水qwq + 回文还是写string的reverse好

1346. 回文平方

题意:

给你一个k进制,要求你判断10进制下的1-300之间的数,哪个数的平方在k进制表示下是回文串。

思路:

直接暴力就好。

  1. 先除k取余
  2. 接着判断是否是回文即可。

反思

一开始想都没想直接暴力了,但是还是可以运用string函数,少码好多qwq

  1. rend是一个string的起始位置。
  2. rebegin是最后一个字符的位置。

AC1(string)

# include <bits/stdc++.h>

using namespace std;
int b;
string f(int x){
    
    
    string s;
    while(x){
    
    
        if(x%b>9) s+= (char)('A'+x%b-10);
        else s+=(char)('0'+x%b);
        x/=b;
    }
    reverse(s.begin(),s.end());
    return s;
}

int main(){
    
    
    cin>>b;
    for(int i = 1; i <= 300; i++){
    
    
        string s = f(i*i);
        if(s == string(s.rbegin(),s.rend())){
    
    
            cout<<f(i)<<' '<<s<<endl;
        }
    }
    return 0;
}

AC2(暴力)

# include <bits/stdc++.h>

using namespace std;
int b;
string divd(int x){
    
    
    string s;
    while(x){
    
    
        if(x%b>=10)s+=(char)('A'-10+x%b);
        else s+=(char)('0'+x%b);
        x/=b;
    }
    reverse(s.begin(),s.end());
    return s;
}

int main(){
    
    
    cin>>b;
    for(int i = 1; i <= 300; i ++ ){
    
    
        string s = divd(i*i);
        if(s== string(s.rbegin(),s.rend())){
    
    
            cout<<divd(i)<<' '<<s<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45377553/article/details/112592249