【CodeForces】[75A]Life Without Zeros

Problem Description

Loading...

Problem Analysis

Meaning:
With normal arithmetic, 101+102=203
When all 0s in the expression are removed, it becomes
11+12=23
The equality still holds
And 105+106=211, after removing all 0s, becomes
15+16=211, which clearly does not hold
So given two numbers
determine whether the equation for their sum still holds after all 0s are removed

Solution process:
Since the problem says to remove all 0s
we can use

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Int tc=0;
	while(c) {
		if(c%10)
			tc*=10;
		tc+=c%10;
		c/=10;
	}
c=0;
	while(tc) {
		c=c*10+tc%10;
		tc/=10;
	}

to remove the 0s from number c
Finally, check whether the sum of the two numbers after removing 0s
equals the sum after removing 0s.

Also note the data range:
int may overflow

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>
int main() {
	__int64 a,b;
	while(scanf("%I64d",&a)!=EOF) {
		scanf("%I64d",&b);
		__int64 c=a+b;
		__int64 tc=0;
		while(c) {
			if(c%10)
				tc*=10;
			tc+=c%10;
			c/=10;
		}
		c=0;
		while(tc) {
			c=c*10+tc%10;
			tc/=10;
		}
		__int64 ta=0;
		while(a) {
			if(a%10)
				ta*=10;
			ta+=a%10;
			a/=10;
		}
		a=0;
		while(ta) {
			a=a*10+ta%10;
			ta/=10;
		}
		__int64 tb=0;
		while(b) {
			if(b%10)
				tb*=10;
			tb+=b%10;
			b/=10;
		}
		b=0;
		while(tb) {
			b=b*10+tb%10;
			tb/=10;
		}
		printf("%s\n",c==a+b?"YES":"NO");
	}

	return 0;
}

Problem link:【CodeForces】[75A]Life Without Zeros

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...