Python basic test questions (Lanqiao Cup provincial test questions)


# According to CSDN and other test questions on the official website, practice. The code of the test questions also includes my own ideas and ideas for your reference. I hope I can communicate with you more. Welcome to correct.

1

1.1 Fibonacci sequence problem

Problem Description:
The recursive formula of the Fibonacci sequence is:
Fn=Fn-1+Fn-2, where F1=F2=1. When n is relatively large, Fn is also very large.
Now we want to know, what is the remainder when Fn is divided by 10007.

Input description: (input format input contains an integer n)

  • n

Output description: (the output format outputs one line, including an integer)

  • Indicates the remainder of dividing Fn by 10007

说明:在本题中,答案是要求Fn除以10007的余数,因此我们只要能算出这个余数即可,而不需要先计算出Fn的准确值,再将计算的结果除以10007取余数,直接计算余数往往比先算出原数再取余简单。 数据规模与约定1 <= n <= 1,000,000。

# 1.1 Fibonacci数列
n = int(input("输入包含一个整数:"))
# n = 10
fib = [1 for i in range(n+1)]
k = 3
while k <= n:
    fib[k] = (fib[k-1] +fib[k-2]) % 10007
    k += 1
print("第", n, "的数列除以10007余数为", fib[n])

1.2 Area of ​​a circle

Problem Description:
Given the radius r of a circle, find the area of ​​the circle.

Input description: (input contains an integer r)

  • Indicates the radius of a circle such as:
    4

Output description: (a line of output contains a real number, rounded to retain 7 decimal places)

  • Indicates the area of ​​a circle such as:
    50.2654825

数据规模与约定: 1 <= r <= 10000

# 1.2 圆的面积
import math
r = int(input("输入圆的半径:"))
s = math.pi * r * r
print("{:.7f}".format(s))

1.3 Sequence summation

Problem Description:
Find the value of 1+2+3+...+n.

Input description: (input contains an integer n)

  • 4

Output description: (one line of output includes an integer, representing the value of 1+2+3+...+n)

  • 10

数据规模与约定: 1 <=n <= 1,000,000,000

# 1.3 序列求和
n = int(input("输入一个整数: "))
s = n * (n + 1) / 2 # 等差数列公式
print("序列和为: %d" %s)

1.4 A+B problem

Problem Description:
Input A, B, output A+B.

Input description: (The first line of input includes two integers, separated by spaces, representing A and B respectively)

  • 12 45

Output description: (one line of output includes an integer, indicating the value of A+B)

  • 57

数据规模与约定: -10000 <= A, B <= 10000

# 1.4 A+B问题
#只适用于数据规模较小的情况下。若过大,则通过大数加法计算
a, b = map(int, input("输入两个整数").split())
print(a+b)


2

2.1 Array sorting

Problem Description:
Given an array of length n,
arrange the array in ascending order. 1<=n<=200

Input description:
(the first line is an integer n)
(the second line contains n integers, which are the numbers to be sorted, and the absolute value of each integer is less than 10000)

  • 5
    8 3 6 4 9

Output description: (output one line, output the sorted sequence in ascending order)

  • 3 4 6 8 9

# 2.1 数列排序 (即输入的第二行整数以空格间隔)
# 方法一:
n = int(input("输入数列长度: "))
num = list(map(int, input("输入待排序整数,整数的绝对值小于10000:").split()))
num.sort()  #None是不可迭代的    或者sorted(num)
print(" ".join("{:d}".format(i) for i in num ))

# 方法二:
num = input("输入待排序整数,整数的绝对值小于10000:").split()
num = sorted(num)    #list类型
print(" ".join(num[:n]))

2.2 Binary conversion

hexadecimal to octal
Problem Description:
Given n hexadecimal positive integers, output their corresponding octal numbers

Input description:
(the first input line is a positive integer n (1<=n<=10))
(the next n lines, each line is a string consisting of 09 and uppercase letter AF, indicating the hexadecimal positive integer to be converted, and the length of each hexadecimal number does not exceed 100000)

  • 2
    39
    123ABC

Output description: (output n lines, each line corresponds to the octal positive integer input)

  • 71
    4435274

【注意】输入的十六进制数不会有前导0,比如012A
输出的八进制数也不能有前导0

n = int(input("输入需转换的行数: "))
for i in range(1,n+1):
    print("第", i, "行")
    m = input("输入十六进制正整数: ")
    ans = format(int(m, 16), 'o')
    print(ans)
#十六进制转二进制
ans = format(int(m, 16), 'b')
hexadecimal to decimal
Problem Description:
Input a positive hexadecimal string of no more than 8 digits from the keyboard, convert it to a positive decimal number and output it.
Note: 10~15 in hexadecimal numbers are represented by uppercase English letters A, B, C, D, E, and F respectively

Enter a description:

  • FFFF

Output description:

  • 65535

#十六进制转十进制
print(int(input("输入十六进制正整数: "), 16))
Decimal to hexadecimal
Problem Description:
Hexadecimal number is an integer representation that is often used in programming. It has 16 symbols of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F, which represent decimal numbers 0 to 15
.
The hexadecimal counting method is 16 to 1, so the decimal number 16 is 10 in hexadecimal, and the decimal 17 is 11 in hexadecimal, and so on, the decimal 30 is 1E in hexadecimal. Given a non-negative integer, represent it in hexadecimal form

Input description: (input contains a non-negative integer a, indicating the number to be converted, 0<=a<=2147483647)

  • 30

Output description: (output the hexadecimal representation of this integer)

  • 1E

#十进制转十六进制
print(format(int(input("输入十进制正整数: ")), 'X'))

2.3 Special palindromes

Problem Description:
123321 is a very special number that reads the same from the left as it reads from the right. Enter a positive integer n, program to find all such five-digit and six-digit decimal numbers, satisfying that the sum of each digit is equal to n

Input description: (the first line of input contains a positive integer n)

  • 52

Output description: (output the integers that meet the conditions in ascending order, and each integer occupies one line)

  • 899998
    989989
    998899

数据规模与约定: 1<=n<=54

# 2.3 特殊回文数
#方法一:
n = int(input("输入需得到回文数之和的整数: "))
#先判断是否为回文数  (方法一:特点按顺序:从10000,10001,10002,...,999999)
for i in range(10000, 1000000):
    num_pd = str(i)  #回文数
    if num_pd == num_pd[::-1]:
        sum_pd = 0
        #在单独求各个 位数字之和
        for j in num_pd:
            sum_pd += int(j)
        if sum_pd == n:
            print(num_pd)
#方法二:按照回文数顺序遍历,如:10001,20002,...,999999)
my_list = []
for i in range(100, 1000):
    num_pd_half = str(i) #回文数的前半部分,复制,对称处理 ,从而减少运算
    #回文数是五位数
    if sum(map(int, num_pd_half + num_pd_half[:2][::-1])) == n:  #三位数 + 三位数的前2位数 倒序
        my_list.append(num_pd_half + num_pd_half[::-1])
    #回文数是六位数
    if sum(map(int, num_pd_half + num_pd_half[::-1])) == n:    #三位数 + 三位数的倒序
        my_list.append(num_pd_half + num_pd_half[::-1])

for i in sorted(map(int, my_list)):
    print(i)
#方法三:
# ***** 绝绝子方法 *****
n = int(input())
for i in range(10000, 1000000):
    num_pd = str(i) #可能出现的回文数
#符合正序与倒序相等,同时将字符串以 + 分割并以eval()转化为表达式进行求值
    if num_pd == num_pd[::-1] and eval('+'.join(num_pd)) == n:
    #eval:***转化为有效的表达式 或 complie函数的代码对象(其可以创建函数compile(表达式,'','编译代码'))
        print(i)
four digit palindrome
Problem Description:
The forward and backward numbers are the same, output all such four-digit numbers
such as 1221
#输出四位数的回文
# import time
# start = time.time()
num_list = []
for i in range(10, 101):  #两位数
    num_list.append(str(i) + str(i)[::-1])  #两位数 + 两位数的倒序
for l in map(int, num_list):
    print(l)
# print(time.time()-start)

2.4 Number of daffodils

Problem Description:
153 is a very special number, it is equal to the cube sum of its digits, that is, 153=111+555+333.
Program to find all three-digit decimal numbers that satisfy this condition

Output description: (output the three-digit decimal numbers that meet the conditions in ascending order, and each number occupies a line)

# 2.4 水仙花数
for i in range(100, 1000):
    # 个位十位百位数 各个立方,相加之和
    num_sum = int(str(i)[0]) ** 3 + int(str(i)[1]) ** 3 + int(str(i)[2]) ** 3
    if num_sum == i:
        print(i)

2.5 Yang Hui Triangle

Problem Description:
Yang Hui's triangle is also called Pascal's triangle, and its i+1th row is the coefficient of the expansion of (a+b)i.
One of its important properties is that each number in a triangle is equal to the sum of the numbers on its two shoulders.
The following gives the first 4 rows of the Yanghui triangle:
1
1 1
1 2 1
1 3 3 1
Given n, output its first n rows

Input description: (input contains a number n)

  • 4

Output description: (output the first n lines of Yang Hui triangle. Each line is output sequentially from the first number of this line, separated by a space in the middle. Please do not output extra spaces in front)

  • 1
    1 1
    1 2 1
    1 3 3 1

数据规模与约定: 1 <= n <= 34

# 2.5 杨辉三角
n = int(input("输入行数:"))

old_line = [1]    #打印第一列 1
print(' '.join(str(i) for i in old_line))
for i in range(1, n):   #打印第一列之后,第二列开始(从[1,1]开始)
    new_line = []       #创建新列表
    for j in range(len(old_line)-1):  #打印第二列之后,第三列开始(从[1,2,1]开始)
        new_line.append(old_line[j] + old_line[j+1])  #添加的数据,(从[1]+[1]: 1+1 = 2开始)
    # 添加新列表中的参数   *表示:将列表解开成多个独立的参数
    new_line = [1, *new_line, 1]

    print(' '.join(str(i) for i in new_line))


2.6 Finding Integers

Problem Description:
Given a sequence of n integers, what is the first occurrence of the integer a in the sequence?

Input description:
(the first line contains an integer n)
(the second line contains n non-negative integers, which is a given sequence, and each number in the sequence is not greater than 10000)
(the third line contains an integer a, which is the number to be searched)

  • 6
    1 9 4 8 3 9
    9

Output description: (output the first n lines of Yang Hui triangle. Each line is output sequentially from the first number of this line, separated by a space in the middle. Please do not output extra spaces in front)

  • 2

数据规模与约定: 1 <= n <= 1000

# 2.6 查找整数, 输入含有n个整数的数列a,搜索整数m在a数列中的数量
n = int(input("输入数列中整数的数量: "))
a = input("输入数列: ").split()
m = input("输入待查的整数: ")
num = 0
##方法缺n值
for j in range(n):
    if a[j] == str(m):  #按顺序搜索数列中的整数
        num += 1
if num == 0:
    print(-1)
else:
    print("出现的次数为: ", num)


2.7 Sequence features

Problem Description:
Given n numbers, find the maximum, minimum, and

Input description:
(the first line is an integer n, indicating the number of numbers)
(the second line has n numbers, which are given n numbers, and the absolute value of each number is less than 10000)

  • 5
    1 3 -2 4 5

Output description: (Output three lines, one integer per line. The first line represents the maximum value of these numbers, the second line represents the minimum value of these numbers, and the third line represents the sum of these numbers)

  • 5
    -2
    11

数据规模与约定: 1 <= n <= 1000

# 2.7 数列特征
n = int(input("输入整数的个数: "))
m = input("输入数列: ").split()
# max(),min()函数,对于列表取最大/小的数字,
# 对于字符串取最大/小的字母(如:"1,2,3" --> 逗号最小), 其他的与字符串同理

if n == len(m):
    print("最大数值为:", max(m))  #求最大数值
    print("最小数值为:", min(m))  #求最小数值
    print("最小值为:", eval("+".join(m))) #求和


2.8 Alphabet Graphics

Problem Description:
Letters can be used to form some beautiful graphics, an example is given below:
ABCDEFG
BABCDEF
CBABCDE
DCBABCD
EDCBABC
This is a graphic with 5 rows and 7 columns, please find out the pattern of this graphic, and output a graphic with n rows and m columns

Input description: (Enter a line, including two integers n and m, respectively representing the number of rows and columns of the graph you want to output)

  • 5 7

Output description: (output n lines, each m characters, for your graphics)

  • ABCDEFG
    BABCDEF
    CBABCDE
    DCBABCD
    EDCBABC

数据规模与约定: 1 <= n, m <= 26

# 2.8 字母图形
try:
    n, m = map(int, input("请输入需输出的行数 、列数: ").split())
    letter = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    output = letter[:m]    #初始为m列的第一行列表
    for i in range(1, n+1):
        print(output)
        output = letter[i] + output[:-1]  #重新赋值第二列列表之后的数列
        #为字母表第n个为字母列的首字母 + 前一行除最后一位数的字母列
except:
    pass


2.9 String 01

Problem Description:
For a 01 string with a length of 5 bits, each bit may be 0 or 1, and there are 32 possibilities in total. The first few of them are:
00000
00001
00010
00011
00100
Please output these 32 kinds of 01 strings in ascending order

Output description: (Output 32 lines, each line has a string of 01 with a length of 5 in ascending order)

  • 00000
    00001
    00010
    00011

数据规模与约定: 1 <= n, m <= 26

# 2.9 字串01
for i in range(32):
    print("{:0>5}".format(format(i, 'b')))
    

2.10 Leap Year Judgment

Problem Description:
Given a year, determine whether this year is a leap year.
A year is a leap year when one of the following conditions is true:
the year is a multiple of 4 but not 100; the year is a multiple of 400.
All other years are not leap years.

Input description: (input contains an integer y, representing the current year)

  • 2013

  • 2016

Output description: (output one line, if the given year is a leap year, output yes, otherwise output no)

  • no

  • yes

数据规模与约定: 1990 <= y <= 2050

#  2.10 闰年判断
n = int(input("输入年份: "))
if n % 4 == 0 and n % 100 != 0 or n % 400 == 0:
    print("yes")
else:
    print("no")
    

[1]: Part of the test questions comes from https://blog.csdn.net/qq_31910669

Guess you like

Origin blog.csdn.net/Fuziqp/article/details/116431536