洛谷 P1015 [NOIP 1999 普及组] 回文数
审题不清晰,一开始没注意到按进制来输入数字,拿int去存,调试了半天,写着写着成shit了,实在没办法。。思路很简单的,就是字符串模拟加法,测试点是没有考虑到一开始就是回文数的情况的,所以不用管88 99这种数据到底按0还是往后算。
AcCode:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
map<char, int> mp;
char a[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
string add(string x, int rx){
string ans = "";
int j = 0;
for(int i = 0; i < x.length(); i++){
int h = mp[x[x.length() - 1 - i]] + mp[x[i]] + j;
//cout << h << endl;
ans += a[h % rx];
j = h / rx;
}
if(j > 0) ans += a[j];
reverse(ans.begin(), ans.end());
int f = 0, b = ans.length() - 1;
bool flag = true;
while(f < b){
if(ans[f++] != ans[b--]) flag = false;
}
if(flag) return "yes";
else return ans;
}
int main(){
//cout << add("87", 16);
string M;
int N, ans = 0;
cin >> N >> M;
for(int i = 0; i <= 9; i++){
mp[(char)(i + '0')] = i;
}
mp['A'] = 10;
mp['B'] = 11;
mp['C'] = 12;
mp['D'] = 13;
mp['E'] = 14;
mp['F'] = 15;
for(int i = 1; i <= 30; i++){
M = add(M, N);
if(M == "yes") {
cout << "STEP=" << i << endl;
return 0;
}
}
cout << "Impossible!";
return 0;
}