1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

東京大学大学院情報理工学系研究科 創造情報学専攻 2016年度 実技試験

Posted at

東京大学大学院情報理工学系研究科 創造情報学専攻の2016年度 実技試験(プログラミング)の解答例(Python3)です。
https://www.i.u-tokyo.ac.jp/common/file/edu/course/ci/2015-8-program.pdf

※公開元ページはこちら
※記載の内容は筆者が個人的に解いたものであり、正答を保証するものではなく、また東京大学及び本試験内容の提供に関わる組織とは無関係です。

#(1)

mport sys
  
a = sys.argv[1]
l = len(a)
i = 1

s = 0
for c in range(l):
    n=4**(l-i)*int(a[c])
    #print(n)
    s=s+n
    i=i+1
print(s)

#(2)

import sys

a = sys.argv[1]
l = len(a)
i = 1

ds = {'a':0,'b':1,'c':2,'d':3,'e':4,'f':5,'g':6,'h':7}
s = 0
for c in range(l):
    n=len(ds)**(l-i)*int(ds[a[c]])
    #print(n)
    s=s+n
    i=i+1
print(s)

#(3)

import sys
  
a = sys.argv[1]
r = {'I':1,'IV':4,'V':5,'IX':9,'X':10,'XL':40,'L':50,'XC':90,'C':100,'CD':400,'D':500,'CM':900,'M':1000}

def putChars(n, cs=""):
    for k, v in sorted(r.items(), key=lambda x: -x[1]):
        if (v<=n):
            cs = cs + k
            if (n-v==0):
                return cs
            else:
                return putChars(n-v,cs)
            break

ret=putChars(int(a))
print(ret)

#(4)

import sys

a = sys.argv[1]
l = len(a)
r = {'I':1,'IV':4,'V':5,'IX':9,'X':10,'XL':40,'L':50,'XC':90,'C':100,'CD':400,'D':500,'CM':900,'M':1000}
sum = 0
c = 0
while(1):
    try:
        n1 = r[a[c]]
        n2 = r[a[c+1]]
        n3 = r[a[c+2]]
        if (n1>n2 and n3>n2):
            nx = n1-n2+n3
            sum = sum + nx
            c = c + 3
            if (c>=len(a)):
                break
            else:
                continue
    except IndexError:
        try:
            n1 = r[a[c]]
            n2 = r[a[c+1]]
            if (n2>n1):
                nx = n2-n1
                sum = sum + nx
                c = c + 2
                if (c>=len(a)):
                    break
                else:
                    continue
        except IndexError:
            pass

    try:
        ps = a[c] + a[c+1]
    except IndexError:
        ps = a[c]
    try:
        r[ps]
        c = c + 1
    except KeyError:
        ps = a[c]
    sum = sum + r[ps]
    c = c + 1
    if (c>=len(a)):
        break
print(sum)

#(5)
(3)と同じなため省略。

#(6)
(4)と同じなため省略

#(7)

import sys
  
a = sys.argv[1]
a = a.split(' ')
l = len(a)
ds = {'one':1,'two':2,'three':3,'four':4,'five':5,'six':6,'seven':7,'eight':8,'nine':9,'ten':10,'eleven':11,'twelve':12,'thirteen':13,'fourteen':14,'fifteen':15,'sixteen':16,'seventeen':17,'eighteen':18,'nineteen':19,'twenty':20,'thirty':30,'fourty':40,'fifty':50,'sixty':60,'seventy':70,'eighty':80,'ninety':90,'hundred':100,'thousand':1000}

def getUpper(si):
    i = si-1
    while(1):
        if (i<0 or ds[a[i]]>ds[a[si]]):
            break
        i = i -1
    return i+1

def calcPartial(si, ei):
    if (si==ei):
        return ds[a[si]]
    else:
        i = si - 1
        sum = 0
        while(1):
            sum = sum + ds[a[i]]
            if (i==ei):
                break
            i = i - 1
        return sum*ds[a[si]]

sum = 0
c = l-1
while(1):
    gu = getUpper(c)
    cp = calcPartial(c,gu)
    c = gu - 1
    sum = sum + cp
    if (c<0):
        break
print(sum)
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?