2017秋季網易校園招聘編程題和我的解答(python)

 

作菜

題幹

大牛喜歡作菜,每種菜須要不一樣的材料,求須要準備多少種材料。python

每一行是一個菜譜,<50字節,輸入行數不肯定,不超過50行。app

難點:如何獲取輸入結束的標誌spa

輸入示例

apple banana
potato apple egg

輸出示例

4
import sys

food = {}
for i in sys.stdin.readlines():
    for f in i.split(' '):
        food[f] = 1
print len(food)

 

迷宮

大牛要按特定的移動方式在迷宮中跳躍移動,在最糟糕的狀況下,將迷宮出口設置在某個位置時,大牛走出迷宮所需的最少步數值最大,求這個最大值。若是將除垢設置在某處大牛永遠也走不出去的話,輸出-1.code

輸入n和m,0<n,m<=50input

接下來輸入n行,每行有m個字符,'.'表示可走的地方,非'.'表示不可走的地方it

接下來輸入大牛的起始位置,x,y, 0<=x<n, 0<y<=m,左上角爲0, 0io

接下來輸入合法移動方式數量kclass

接下來的k行輸入合法移動方式, dx, dy, -50<=dx, dy <=50import

題幹

輸入示例

3 3
...
...
...
1 0
4
0 1
1 0
-1 0
0 -1

輸出示例

2

轉載請註明原始連接map

mn = raw_input().split(' ')
n = int(mn[0])
m = int(mn[1])
tmap = {}
for i in range(n):
    tmap[i] = {}
    s = raw_input()
    for j in range(m):
        if s[j] == '.':
            tmap[i][j] = '.'
        else:
            tmap[i][j] = 'x'

starts = raw_input().split(' ')
start = (int(starts[0]), int(starts[1]))

k = int(raw_input())
steps = []
for i in range(k):
    s = raw_input().split(' ')
    steps.append((int(s[0]), int(s[1])))

def move(start, step):
    return (start[0] + step[0], start[1] + step[1])

def check(loc):
    global m, n, tmap
    x, y = loc
    if x < 0 or x >= n:
        return False
    if y < 0 or y >= m:
        return False
    if tmap[x][y] != '.':
        return False
    return True

lst = []
lst.append(start)
tmap[start[0]][start[1]] = 0

while(len(lst) != 0):
    loc = lst[0]
    c = tmap[loc[0]][loc[1]]
    del lst[0]

    for step in steps:
        if check(move(loc, step)):
            newloc = move(loc, step)
            lst.append(newloc)
            tmap[newloc[0]][newloc[1]] = c + 1

def final():
    global tmap, n, m

    max = 0
    for i in range(n):
        for j in range(m):
            if tmap[i][j] == '.':
                return -1
            elif tmap[i][j] != 'x':
                if tmap[i][j] > max:
                    max = tmap[i][j]
    return max

def output():
    global tmap, n, m


    for i in range(n):
        for j in range(m):
            print tmap[i][j],
        print ''

print final()

數列

題幹

一個數列l中, i < j且l[i] < l[j]的數量稱爲這個數列的"對數"。

大牛草稿紙上有一個數列,它的長度爲n,由一、二、3……n不重複的數字排列而成,大牛知道它的對數爲k。因爲草稿紙上有污漬,這個數列的一部分不見了。試求原來數列可能的狀況有多少種。

輸入n, k

下一行輸入數列,0表示看不見的數字(<=10個)

輸入示例

5 5
4 0 0 2 0

輸出示例

3
s = raw_input().split(' ')
n = int(s[0])
k = int(s[1])
l = raw_input().split(" ")
ls = []
for i in l:
    ls.append(int(i))

import itertools


count = 0
zeros = []
nums = {}
unums = []
for i in range(1, n+1):
    nums[i] = 0

for i in range(n):
    nums[ls[i]] = 1
    if ls[i] == 0:
        zeros.append(i)

for i in nums:
    if nums[i] == 0 and i != 0:
        unums.append(i)

for x in itertools.permutations(unums, len(unums)):
    lls = ls[:]
    adc = 0
    for i in range(len(zeros)):
        lls[zeros[i]] = x[i]

    for i in range(n):
        for j in range(i + 1, n):
            if lls[i] < lls[j]:
                adc += 1

    if adc == k:
        count += 1

print count
相關文章
相關標籤/搜索