网易游戏研发工程师2018春招在线笔试(不全 未测)

记录一下自己的(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))

你可能感兴趣的:(网易游戏研发工程师2018春招在线笔试(不全 未测))