题目链接:Problem - 1700B - Codeforces
他会给出一个数字, 让你用n位数的整数将这个数字凑成一个回文数, 乍一看感觉很难, 因为会有各种各样回文数组合. 那么有没有可能让回文数成为一个固定值呢? 答案是有的: 假设n=4, 那么除了第4位为9的情况下, 一定有可以凑成9999的情况, 这种情况直接减去他给的数字就能得到答案; 如果第四位数为9的话, 那就凑11111(多一位), 用11111减去他给的数字就能得到答案.
ps: 这题的数据很大, 记得使用大数减法
ac代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <stack>
#include <deque>
#include <map>
#include <set>
using namespace std;
#define ll long long
#define endl "\n"
#define rep(i, a, b) for (ll i = (a); i <= (b); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define rrep(i, a, b) for (ll i = (b); i >= (a); i--)
#define rrepr(i, a, b) for (ll i = (b); i > (a); i--)
#define min(a,b) (a)<(b)?(a):(b)
#define max(a,b) (a)>(b)?(a):(b)
ll cnt,n,m,t,ans,ant;
const int N=1e5+10;
ll arr[N];
string str1,str2;
inline ll read()
{
char c = getchar();int x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}//是符号
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}//是数字
return x*s;
}
void solve()
{
cin>>n;
//999或者1111
string str;
str1.clear();
cin>>str;
if(str[0]=='9')//大数减法
{
cnt=0;//进位
ant=0;
rrep(i,0,n-1)//模拟减法运算, 从高位数开始减
{
ant=1-(str[i]-'0')-cnt;//记录当前数位
if(ant<0)//进位
{
cnt=1;
ant+=10;
}else cnt=0;//进位清空
str1.push_back(ant+'0');
}
reverse(str1.begin(),str1.end());//将字符串倒置
cout<<str1<<endl;
}else
{
repr(i,0,n)
printf("%lld",9-(str[i]-'0'));//直接用9999减去字符串
puts("");
}
return;
}
int main()
{
cin>>t;
while(t--)
solve();
return 0;
}