基础程序教学教程习题解答

来源:互联网 发布:php下载文件到本地 编辑:程序博客网 时间:2024/04/29 06:46

1. 第一个程序 & 基础IO和运算

(1)猴子有长为long的尾巴,他想要从一个容器中抓出来桃子,容器高height,建立一个程序输入long和height输出猴子把尾巴伸入后距离容器底的距离

long = int(input('please input the length of the monkey tail: '))height = int(input('please input the height of the cup: '))ans = long - 2 * heightprint(ans)

(2)不调用int方法做一个python的整除函数:输入a,b,输出a整除b

a = int(input('please input a: '))b = int(input('please input b: '))print((a - a % b) / b)

(3)做一个python四舍五入的整除:同上

a = int(input('please input a: '))b = int(input('please input b: '))print(int(a / b + 0.5))


2. if 语句与for,while语句

(1)输入一个整数,输出他的绝对值

num = int(input('please input a number: '))if num < 0:    print(-num)else:    print(num)

(2)输入一个整数,将其倒序输出

num = int(input('please input a number: '))count = 1while count < num:    count *= 10if num == count:    print(1)else:    ans = 0    count1 = count    count /= 10    while count1 != 1:        count1 /= 10        ans += int(num/count1)*(count/count1)        num = num % count1    print(int(ans))

(3)简单验算质数【输入一数精确判断其是否是质数  】

num = int(input('please input a number: '))if num == 1:    print('not define')else:    x = 0    i = 2    while i ** 2 < num:        if num % i == 0:            x = 1            break        i += 1    if x == 1:        print('not prime')    else:        print('prime')

(4)num个小朋友围成一圈,从第一个小朋友开始,第一次:将一个桃子传给第二(1+1)个小朋友,第二次:第二个小朋友传给第四(1+1+2)个小朋友,第三次:第二个小朋友你传给第七个小朋友(1+1+2+3+4),那么第n个小朋友什么时候拿到桃子?【输入:num,n,输出:什么时候拿到桃子】将规律变换,并且加上超时自动停止

num = int(input('please input the number of children (num): '))n = int(input('please input the child\'s number (n): '))i = 0numkid = 0while numkid != n:    i += 1    numkid = (i * (i + 1) / 2 + 1) % num    if i > 10 ** 10:        breakif i > 10 ** 10:    print('cannot find the answer')else:    print(i)


3. 字符串处理

(1)重新实现所有字符串处理函数(join和split需要数组知识,不用做。len函数可以使用,不用做)

# capitalizestr = input('input an str: ')asc = ord(str[0])if 123 > asc > 96:    asc -= 32    newstr = chr(asc)    for i in range(1, len(str)):        newstr = newstr + str[i]    print(newstr)else:    print(str)
# findsubstr = input('input a sub-str: ')str = input('input an str: ')count = 0where = 0exist = Falsefor i in range(0, len(str)):    if str[i] == substr[count]:        count += 1        if count == len(substr):            exist = True            where = i - count + 1            breakif exist:    print(where)else:    print(-1)
# countsubstr = input('input a sub-str: ')str = input('input an str: ')count = 0number = 0for i in range(0, len(str)):    if str[i] == substr[count]:        count += 1        if count == len(substr):            number += 1            count = 0print(number)
# isalnum# isalpha, isdigit, islower, isspace, isupper is similarstr = input('input an str: ')isalnum = Truefor cha in str:    if ord(cha) < 48 or 57 < ord(cha) < 65 or 90 < ord(cha) < 97 or 122 < ord(cha):        isalnum = False        breakprint(isalnum)
# swapcase# upper and lower are similarstr = input('please input a string: ')newstr = ''for char in str:    if 64 < ord(char) < 91:        b = chr(ord(char)+32)    elif 96 < ord(char) < 123:        b = chr(ord(char)-32)    else:        b = char    newstr += bprint(newstr)

(2)输入一个字符串将其倒序输出

str = input('please input a string: ')newstr = ''for i in range(len(str)-1, -1, -1):    newstr += str[i]print(newstr)

3)简单的小黄鸡:输入固定语句,输出固定语句,如果超出输出 ‘我不会’

print('hello')while True:    str = input().lower()    if str == 'hello':        print('Nice to meet you')    elif str == 'how are you':        print('I am great')    else:        print('I don\'t know what are you talking about.')

(4)讨厌,居然没啥好玩的= =,想起来了再写


4. 数组与多维数组:

(1)输入一个数组的长度len,然后依次输入数组的元素,计算数组的平均数【不用数组再实现一遍】

# without arraylen = int(input('input the length of the series: '))sum = 0for i in range(0, len):    num = int(input('input the '+str(i+1)+' number:'))    sum += numprint(sum/len)

(2)输入一个数组长度len,然后依次输入数组元素,对数组冒泡排序后输出

len = int(input('input the length of the series: '))sum = 0num = []for i in range(0, len):    num.append(int(input('input the '+str(i+1)+' number:')))for i in range(0, len-1):    for j in range(0, len-1):        if num[j] > num[j+1]:            [num[j], num[j+1]] = [num[j+1], num[j]]print(num)

(3)输入一定数量个以‘ ’分隔的数,然后输出其最大值和最小值【要求最快】,然后输出快速排序后输出

(4)输入一个数,输出其以下的质数, 要求算法最优

(5)国际象棋中皇后可以横向竖向斜向行走,输入皇后的坐标,输入另一个旗子的坐标,输出皇后是否可以在下一步吃到这个旗子,如果不能,输出皇后吃到棋子的最短路线(移动一个格视为移动一步)


5. 文件输入输出

(1)做一个简单的小黄鸡,在不会的语句之后提问:需不需要教?,然后将用户输入的语句作为回答语句。拥有用户可以直接在UI中更改已有设定的语句的功能

(2)在文件中用'*'画简单的三角形,倒三角还有沙漏形等

(3)输入数字1,输出a,输入数字2,输出a旁边围着一圈b,输入27,输出a旁边围着一圈b外面围着一圈c…外面围着一圈z,再外面围着一圈a

0 0
原创粉丝点击