網易遊戲研發工程師2018春招線上筆試(不全 未測)

vivien_wei發表於2018-03-29

記錄一下自己的(zz)程式碼,題目只取其意。

一、畫等腰三角形

題目:給定數字N,輸出用‘*’表示邊,‘.’表示空白,的一個高為N底為2N-1的等腰三角形

不要輸出空格

如:N=5:

. . . . *

. . . *. *

. . *. . . *

. *. . . . . *

*********

import sys
for line in sys.stdin:
    a = line.split()
    N = int(a[0])
    print('.'*(N-1)+'*')
    for i in range(1,N-1):
        print('.'*(N-1-i)+'*'+'.'*(2*i-1)+'*')
    print('*'*(2*N-1))

二、數字特殊排序

題目

輸入前四個分別表示後面四個值個數的八個數字,尋找最好的一種排列所有這四種值的方式,
使得過程中當前數字和的個位數與新加入的數的乘積的總和最大,輸出最大乘積和

輸入:1 1 1 1 11 12 13 14

輸出:235

import sys
for line in sys.stdin:
    a = input().split()
    nums = [int(a[i]) for i in range(4)]
    mass = [int(a[i]) for i in range(4,8)]
    used = [0]*4
    maxi=0
    
    def findmax(nums,mass,maxi):
        if sum(nums)==0:
            maxi = 0
        else:
            used = nums.copy()
            for i in range(4):
                if used[i]>0:
                    used[i]-=1
                    new = mass[i]*(sum(mass[j]*used[j] for j in range(4))%10)
                    return max(maxi,findmax(used,mass,maxi)+new)
        return maxi
    
    print(findmax(nums,mass,maxi))

三、(沒做,略過)

四、字串新運算

題目

定義字串運算;字串僅限於A-Za-z0-9的所有組合;
按照優先順序排列:
()括號
~將字串順序顛倒;-改變字串大小寫
* 如 AAA*BB = ABBABBA;/ 如ABCABCAE/AB = CCE
+ 將兩個字串串一起;- 前者減後者(完全),如ABCDEF-DE = ABCF

如:ABC*~(XYZ+a)/az = AZYXBZYXC

import sys
for line in sys.stdin:
    a = line.split()
    form = a[0]
    res = []


    def isstring(each):
        if 'z' >= each >= 'a' or '0' <= each <= '9' or 'A' <= each <= 'Z':
            return True
        else:
            return False


    for each in form:
        if isstring(each):
            if len(res) > 0 and isinstance(res[-1], str) and isstring(res[-1]):
                res[-1] += each
            else:
                res.append(each)
        elif each == '+':
            res.append(-1)
        elif each == '-':
            res.append(1)
        elif each == '*':
            res.append(-2)
        elif each == '/':
            res.append(2)
        elif each == '~':
            res.append(3)
        elif each == '(':
            res.append(4)
        elif each == ')':
            res.append(0)
    for k in range(len(res)):
        if res[k] == 1:
            if k == 0 or not isstring(res[k - 1]):
                res[k] = -3


    def findmax(res):
        maxi = 0
        pos = 0
        for m in range(len(res)):
            if isinstance(res[m], int) and abs(res[m]) > maxi:
                pos = m
                maxi = res[m]
        return pos


    def findP(res):
        maxi = 0
        for p in range(len(res)):
            if res[p] == 0:
                maxi = p
        return maxi


    def calculate(res):
        if len(res) == 0:
            return ""
        if len(res) == 1:
            return res[0]
        pp = findmax(res)
        if res[pp] == 4:
            end = findP(res[pp:]) + pp
            return calculate(res[:pp] + [calculate(res[pp + 1:end])] + res[end + 1:])
        elif abs(res[pp]) == 3:
            if isstring(res[pp + 1]):
                if res[pp] == 3:
                    res[pp + 1] = res[pp + 1][::-1]
                else:
                    for c in range(len(res[pp + 1])):
                        if 'A' <= res[pp + 1][c] <= 'Z':
                            res[pp + 1][c].lower()
                        elif 'a' <= res[pp + 1][c] <= 'z':
                            res[pp + 1][c].upper()
                res.pop(pp)
                return calculate(res)
            else:
                for c in range(len(res[pp + 2])):
                    if 'A' <= res[pp + 2][c] <= 'Z':
                        res[pp + 2][c].lower()
                    elif 'a' <= res[pp + 2][c] <= 'z':
                        res[pp + 2][c].upper()
                res[pp + 1] = res[pp + 1][::-1]
                res.pop(pp)
                res.pop(pp)
                return calculate(res)

        elif res[pp] == 2:
            t = ""
            for ea in res[pp - 1]:
                if ea not in res[pp + 1]:
                    t += ea
            res[pp] = t
            res.pop(pp - 1)
            res.pop(pp)
            return calculate(res)
        elif res[pp] == -2:
            t = ""
            for ea in res[pp - 1][:-1]:
                t += ea + res[pp + 1]
            res[pp - 1] = t + res[pp - 1][-1]
            res.pop(pp)
            res.pop(pp)
            return calculate(res)
        elif res[pp] == 1:
            l = len(res[pp + 1])
            t = ""
            q = 0
            while q <= len(res[pp - 1]) - l:
                if res[pp - 1][q:q + l] == res[pp + 1]:
                    q += l
                    t += res[pp - 1][q:]
                    break
                else:
                    t += res[pp - 1][q]
                    q += 1
            res[pp - 1] = t
            res.pop(pp)
            res.pop(pp)
            return calculate(res)
        elif res[pp] == -1:
            res[pp - 1] += res[pp + 1]
            res.pop(pp)
            res.pop(pp)
            return calculate(res)


    print(calculate(res))

相關文章