• Codeforces Round #342 (Div. 2) D. Finals in arithmetic(想法题/构造题)


    传送门

    Description

    Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.

    Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.

    Oksana Fillipovna picked some number a without leading zeroes, and flipped it to get number ar. Then she summed a and ar, and told Vitya the resulting value n. His goal is to find any valid a.

    As Oksana Fillipovna picked some small integers as a and ar, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given n finds any a without leading zeroes, such that a + ar = n or determine that such a doesn't exist.

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 10100 000).

    Output

    If there is no such positive integer a without leading zeroes that a + ar = n then print 0. Otherwise, print any valid a. If there are many possible answers, you are allowed to pick any.

    Sample Input

    4

    11
    5

    33

    Sample Output

    2

    10

    0

    21

    Note

    In the first sample 4 = 2 + 2, a = 2 is the only possibility.

    In the second sample 11 = 10 + 1, a = 10 — the only valid solution. Note, that a = 01 is incorrect, because a can't have leading zeroes.

    It's easy to check that there is no suitable a in the third sample.

    In the fourth sample 33 = 30 + 3 = 12 + 21, so there are three possibilities for aa = 30, a = 12, a = 21. Any of these is considered to be correct answer.

    思路

    题意:给出数字n,问是否存在一个数x,使得 x + flip(x) = n (其中 flip(x)为x的反转,反转后忽略前导0)

    题解:对于前后两个对称位置,如果相等,则ans[i]=(num[i]+1)/2,ans[n-i-1]=num[i]/2,若不相等,考虑两种情况,一种是来自低位的进位,一种是来自高位的退位

    • 当num[i] == num[n - i - 1] + 1 || num[i] == num[n - i - 1] + 11,说明第 i 位有来自低位的进位,因此将其还原即可,亦即使得num[i]--,num[i + 1] += 10;
    • 当num[i] == num[n - i - 1] + 10,说明第 i 位有来自高位的退位,因此使得第 n - i - 1 位也有来自高位的退位,,亦即使得num[n - i - 2]--,num[n - i - 1] += 10;

    另外,对于n为 “1”开头的数值时,需要特判,因为这位1是由x 和 flip(x )相加而得的进位,至于大于 “1”的数字开头的数值不需要进位是因为 9 + 9  + 1 = 19,最多只能进1。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 1000005;
    char s[maxn],res[maxn];
    int num[maxn];
    
    bool check(int n)
    {
    	for (int i = 0;i < n / 2;)
    	{
    		if (num[i] == num[n - i - 1])	i++;
    		else if ((num[i] == num[n - i - 1] + 1) || (num[i] == num[n - i - 1] + 11))  //来自低位的进位 
    		{
    			num[i]--;
    			num[i + 1] += 10;
    		}
    		else if (num[i] == num[n - i - 1] + 10)     //来自高位的退位 
    		{
    			num[n - i - 2]--;
    			num[n - i - 1] += 10;
    		}
    		else	return false;
    	}
    	if (n % 2 == 1)
    	{
    		if ((num[n/2]%2 == 1) || (num[n/2] > 18) || (num[n/2] < 0))	return false;
    		else	res[n/2] = num[n/2]/2 + '0';	
    	} 
    	for (int i = 0;i < n / 2;i++)
    	{
    		if (num[i] > 18 || num[i] < 0)	return false;
    		res[i] = (num[i] + 1) / 2 + '0';
    		res[n - i - 1] = num[i] / 2 + '0';
    	}
    	return res[0] > '0';
    }
    
    
    int main()
    {
    	scanf("%s",s);
    	int len = strlen(s);
    	for (int i = 0;i < len;i++)	num[i] = s[i] - '0';
    	if (check(len))	puts(res);
    	else if (s[0] == '1' && len > 1)   //为 “1”开头的数值进行特判
    	{
    		for (int i = 0;i < len;i++)	num[i] = s[i + 1] - '0';
    		len--;
    		num[0] += 10;
    		if (check(len))	puts(res);
    		else	puts("0"); 
    	}
    	else	puts("0");
    	return 0;
    }
    

      

  • 相关阅读:
    DB2 关联更新
    postgresql 开启审计日志
    Delphi D10.X中Tpath引发的单元引用及代码编写的思考
    自己写的函数或者过程与Delphi提供的重名了怎么办?(50分)
    技巧四 Delphi XE3 代码自动提示bug解决
    想开发经典界面吗?
    初涉Delphi下Windows消息机制——同一程序内自定义消息实例
    Delphi的DirectShow开发概述
    delphi接口(抄自万一)
    fkInternalCalc和fkCalculated有何区别?
  • 原文地址:https://www.cnblogs.com/ZhaoxiCheung/p/6032759.html
Copyright © 2020-2023  润新知