1699. Numeral System Problem code: NSYSTEM
def convert(s):
sum=0
init =1;
for i in s:
if i.isalpha()==False:
if i=='2':
init = init*2;
elif i=='3':
init = init*3;
elif i=='4':
init = init*4;
elif i=='5':
init = init*5;
elif i=='6':
init = init*6;
elif i=='7':
init = init*7;
elif i=='8':
init = init*8;
elif i=='9':
init = init*9;
if i.isalpha():
if i=='m':
init = init*1000
elif i=='c':
init =init*100
elif i=='x':
init =init*10
elif i=='i':
init =init*1
sum = sum+init
init =1
return sum
def convert_back(s):
r =s;
k=1
while r!=0:
r =r/10
k=k*10
k =k/10
r=s
q =""
while k!=0:
r =s
r =r%(k*10)
t =r//k
if t!=1 and t!=0:
q = q+str(t)
if k==1000 and t!=0:
q = q+'m'
elif k==100 and t!=0:
q = q+'c'
elif k==10 and t!=0:
q = q+'x'
elif k==1 and t!=0:
q = q+'i'
k=k/10
print q
for i in range(0, input()):
s = raw_input()
tag=0
m1 = ""
m2= ""
for c in s:
if c==' ':
tag=1
continue
elif tag==0:
m1 = m1+c
else:
m2 = m2+c
a =convert(m1)
b =convert(m2)
c = a+b
convert_back(c)
def convert(s):
sum=0
init =1;
for i in s:
if i.isalpha()==False:
if i=='2':
init = init*2;
elif i=='3':
init = init*3;
elif i=='4':
init = init*4;
elif i=='5':
init = init*5;
elif i=='6':
init = init*6;
elif i=='7':
init = init*7;
elif i=='8':
init = init*8;
elif i=='9':
init = init*9;
if i.isalpha():
if i=='m':
init = init*1000
elif i=='c':
init =init*100
elif i=='x':
init =init*10
elif i=='i':
init =init*1
sum = sum+init
init =1
return sum
def convert_back(s):
r =s;
k=1
while r!=0:
r =r/10
k=k*10
k =k/10
r=s
q =""
while k!=0:
r =s
r =r%(k*10)
t =r//k
if t!=1 and t!=0:
q = q+str(t)
if k==1000 and t!=0:
q = q+'m'
elif k==100 and t!=0:
q = q+'c'
elif k==10 and t!=0:
q = q+'x'
elif k==1 and t!=0:
q = q+'i'
k=k/10
print q
for i in range(0, input()):
s = raw_input()
tag=0
m1 = ""
m2= ""
for c in s:
if c==' ':
tag=1
continue
elif tag==0:
m1 = m1+c
else:
m2 = m2+c
a =convert(m1)
b =convert(m2)
c = a+b
convert_back(c)
No comments:
Post a Comment