Niu Ke.net programming question python implementation (1) -------- Huawei 2016 R & D engineer programming question

1. Delete the number

Time limit: C / C ++ 1 second, other languages ​​2 seconds

Space limitation: C / C ++ 32M, other languages ​​64M

There is an array a [N] which stores 0 ~ N-1 in order, and it is required to delete a number every two numbers. When the end is reached, loop to the beginning to continue, and find the original subscript position of the last deleted number. Take 8 numbers (N = 7) as an example: {0, 1, 2, 3, 4, 5, 6, 7}, 0-> 1-> 2 (deleted)-> 3-> 4-> 5 ( Delete)-> 6-> 7-> 0 (Delete), so cycle until the last number is deleted.
 

Enter description:

每组数据为一行一个整数n(小于等于1000),为数组成员数,如果大于1000,则对a[999]进行计算。

 

Output description:

一行输出最后一个被删掉的数的原始下标位置。

 

Input example 1:

8

Output example 1:

6

 

Code:

import sys

for line in sys.stdin:
    n = int(line)
    if n > 1000:
        n = 1000
    a = [i for i in range(n)]
    i, deletes, k = -1, 0, 0
    while deletes < n:
        k = 0
        while k <= 2:
            i += 1
            i = i % n
            if a[i] == -1:
                continue
            k+=1
        a[i] = -1
        deletes += 1
    print(i)
        
    

[Programming question] character set

Time limit: C / C ++ 1 second, other languages ​​2 seconds

Space limitation: C / C ++ 32M, other languages ​​64M

Enter a string to find the character set contained in the string
 

Enter description:

每组数据输入一个字符串,字符串最大长度为100,且只包含字母,不可能为空串,区分大小写。

 

Output description:

每组数据一行,按字符串原有的字符顺序,输出字符集合,即重复出现并靠后的字母不输出。

 

Input example 1:

abcqweracb

 

Output example 1:

abcqwer

 

Code:

import sys

for line in sys.stdin:
    d = {}
    s = ''
    for c in line:
        if c not in d and c != '\n':
            s += c
            d[c] = 1
    print(s)

 

[Programming question] Sudoku

Time limit: C / C ++ 1 second, other languages ​​2 seconds

Space limitation: C / C ++ 32M, other languages ​​64M

Sudoku is a classic game that we are all very familiar with. Using a computer, we can quickly solve the Sudoku puzzle. Now there are some simple Sudoku problems. Please write a program to solve it.
 

Enter description:

输入9行,每行为空格隔开的9个数字,为0的地方就是需要填充的。

 

Output description:

输出九行,每行九个空格隔开的数字,为解出的答案。

 

Code:

import sys
import copy

def check_results(results, i, j):
    row = results[i]
    for m in range(0, j):
        if row[m] == row[j]:
            return False

    for n in range(0, i):
        if results[n][j] == results[i][j]:
            return False

    k, l = i//3, j//3
    for m in range(k*3, i+1):
        for n in range(l*3, l*3+3):
            if m == i and n == j:
                return True
            if results[m][n] == results[i][j]:
                return False

    return True

def prt_results(results):
    for row in results:
        s = ''
        for col in row:
            s += ' ' + str(col)
        print(s)

def get_sd(a, i=0, j=0, results=None):
    if results is None:
        results = copy.deepcopy(a)

    if a[i][j] != 0:
        if j < 8:
            get_sd(a, i, j+1, results)
        elif i < 8:
            get_sd(a, i+1, 0, results)
        else:
            prt_results(results)
            sys.exit(0)
    else:
        for s in range(1, 10):
            results[i][j] = s
            if not check_results(results, i, j):
                continue
            else:
                if i == 8 and j == 8:
                    prt_results(results)
                    sys.exit(0)
                elif j < 8:
                    get_sd(a, i, j+1, results)
                elif i < 8:
                    get_sd(a, i+1, 0, results)
        results[i][j] = 0
    return results

if __name__ == "__main__":
    a=[[0 for _ in range(9)] for _ in range(9)]
    get_sd(a, 0, 0
Published 230 original articles · 160 praises · 820,000 views

Guess you like

Origin blog.csdn.net/happyAnger6/article/details/104235684