CodeForces - 1700B

题目链接: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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值