-
Notifications
You must be signed in to change notification settings - Fork 11
/
kattis_primaryarithmetic.py
55 lines (38 loc) · 1.01 KB
/
kattis_primaryarithmetic.py
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
48
49
50
51
52
53
54
55
'''
Kattis - Primary Arithmetic
As this is a basically an adhoc problem, we use python as our "go to" language for fast coding with many different library functions (like reversing strings)
Time: O(num cases * num digits of larger number) Mem: O(num digits of larger number)
'''
import sys
for line in sys.stdin.readlines():
lin = line[:-1]
x = lin.split(" ")
a = x[0]
b = x[1]
if a == '0' and b == '0':
break
# a smaller than b
if int(a) > int(b):
a,b = b,a
a = str(a)
b = str(b)
a = a[::-1]
b = b[::-1]
for i in range(len(b) - len(a)):
a += "0"
a.split()
a = [int(i) for i in a]
b.split()
b = [int(i) for i in b]
cc = 0 # carry counter
carry = 0
for i in range(len(a)):
carry = (a[i] + b[i] + carry)//10
if carry != 0:
cc+=1
if (cc == 0):
print("No carry operation.")
elif (cc == 1):
print("1 carry operation.")
else:
print(cc, "carry operations.")