U E D R , A S I H C RSS

Primary Arithmetic/황재선

2005.2.15

PrimaryArithmetic

~cpp 
MAX = 9

class PrimaryArithmetic:
    def addTwoNum(self, n1, n2):
        self.carry = 0
        self.each = 0
        s1, s2 = n1.zfill(MAX), n2.zfill(MAX)
        for bit in range(MAX-1, -1, -1):
            self.carry += self.addEachBit(self.each, s1[bit], s2[bit])
        return self.carry

    def addEachBit(self, each, b1, b2):
        self.each = 0
        if each + int(b1) + int(b2) >= 10:
            self.each = 1
        return self.each

    def result(self):
        if self.carry == 0:
            print 'No carry operation.'
        elif self.carry == 1:
            print '1 carry operation.'   
        else:
            print self.carry, 'carry operations.'

if __name__ == '__main__':
    pa = PrimaryArithmetic()
    while True:
        n1, n2 = raw_input().split()
        if n1 == '0' and n2 == '0':
            break
        pa.addTwoNum(n1, n2)
        print n1, n2
        pa.result()
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:24:02
Processing time 0.0113 sec