构造 D - Finals in arithmetic ★ ★

题意:一个数字a + 反过来的ar == n (<=10^100000),已知n,求a

分析:完全看别人代码看懂的。不考虑进位的话,那么n应该是是回文的。那么处理成不进位的,每一位0~18。

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define INF  0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 100100
#define MOD 1000000007

char str[maxn];
char ans[maxn];
int s[maxn];
int n;
int judge()
{
    for(int i = 0; i < n / 2; )
    {
        int l = i,r = n - 1 - i;
        if(s[l] == s[r])
            i++;
        else if(s[l] == s[r] + 1 || s[l] == s[r]+11)
        {
            s[l]--;
            s[l+1] += 10;
        }
        else if(s[l] == s[r] + 10)
        {
            s[r] += 10;
            s[r-1]--;
        }
        else
            return 0;
    }
    if(n & 1)
    {
        if(s[n/2] & 1 || s[n/2] > 18 || s[n/2] < 0)
            return 0;
        ans[n/2] = s[n/2] / 2 + '0';
    }
    for(int i = 0; i < n / 2; i++)
    {
        if(s[i] > 18 || s[i] < 0)   
            return 0;
        ans[i] = (s[i] + 1) / 2 + '0';
        ans[n-i-1] = s[i] / 2 + '0';
    }
    return ans[0] > '0';
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t,C = 1;
    //scanf("%d",&t);
    while(scanf("%s",str) != EOF)
    {
        n = strlen(str);
        for(int i = 0; i < n; i++)
            s[i] = str[i] - '0';
        if(judge())
            printf("%s\n",ans);
        else if(str[0] == '1' && n > 1)
        {
            for(int i = 0; i < n; i++)
                s[i] = str[i+1] - '0';
            s[0] += 10;
            n--;
            if(judge())
                printf("%s\n",ans);
            else
                printf("0\n");
        }
        else
            printf("0\n");
    }
    return 0;
}


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

构造 D - Finals in arithmetic ★ ★

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭