关于福州大学超算团队一轮考核题心得
概述:本次考核有两种语言供选择学习,分别是C++和python语言。
具体见图:
听闻python的强大,于是我选择了python语言,并开始学习之旅。由于只有一丢丢的C语言基础,刚上手python时总感觉有些别扭(比如不用定义变量了导致数据类型有些混乱、很多方法的使用方式不清楚等等)。经过几天的摸索,上网查询资料视频开始学习python的语法,不得不说和C语言很像(除了for循环,困惑了很久)。基础语法,数据类型,比较简单的方法学完后就开始python第一题
题目
- 1、输入自己的个人信息(学号、性别、专业、性别等)并输出到控制台。
student_number = input("student number:")
name = input("name:")
sex = input("sex:")
major = input("major:")
print(student_number, name, sex, major)
这题应该是对输入输出的考察。
- 2、设置一个程序,实现判断是否闰年。
year = int(input("请输入年份:"))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(f"{year}年是闰年")
else:
print(f"{year}年不是闰年")
本题应该是考察判断结构。if语句实现。
- 3、输入一个正整数,反转这个数字,例如输入123,输出321。
这道题我的想法是将输入的数的每一位数存入一个列表,求余后存入列表按顺序打印出来就是倒序了,但是倒序后的数字有可能首位是0,因此就先将原来的数字末尾的0去掉。
a = []
num = int(input("input your data:\n"))
while num % 10 == 0 and num != 0:
num = num / 10
while num >= 10:
a.append(int(num % 10))
num = (num - num % 10) / 10
a.append(int(num))
print(*a, end='', sep='')
后来我想着不用列表,于是用while循环。这样的话原数末尾的0自然就没有影响了。
t = 0
num = int(input("input your data:\n"))
while num >= 10:
t = t * 10 + num % 10
num = int(num / 10)
t = t * 10 + num
print(t)
- 4、输入两个整数,求给定的两个整数的最大公约数和最小公倍数
这题要了解最大公约数和最小公倍数的算法。求最小公倍数只要求出最大公约数就可以简单得出。
在网上查询资料,主要了解到求最大公约数的算法主要有更相减损法,辗转相除法,stein算法等。这里我选取通俗易懂的辗转相除法并用函数进行操作。
def gcd(a, b):
while b != 0:
temp = a % b
a = b
b = temp
return a
def lcm(a, b):
return a * b / gcd(a, b)
x = int(input("input first integer:"))
y = int(input("input second integer:"))
if x < y:
t = x
x = y
y = t
print(int(gcd(x, y)), int(lcm(x, y)))
- 5、输出前1000的素数。
这个题目的话在C语言中常出现。通过这题学到了range()函数,了解了for循环遍历列表等。
a = [i]
cnt = 0
for item in range(3, 1000, 2):
k = int(pow(item, 0.5))
while i <= k + 1:
if item % i == 0:
break
elif i >= k + 1:
a.append(item)
cnt += 1
i += 1
i = 2
print(*a)
- 6、输入n,输出杨辉三角前n行。
这题原理不难,但是对于我这种小白用for循环,并且嵌套还是理解欠佳。在查询相关知识后,还是艰难地写了一串代码。
n = int(input("请输入n的值:"))
a = []
for i in range(n):
b = []
if i == 0:
b = [1]
elif i == 1:
b = [1, 1]
else:
for j in range(i+1):
if j == 0 or j == i:
b.append(1)
else:
b.append(a[i-1][j-1] + a[i-1][j])
a.append(b)
space = len(a[:])
for i in a:
print(' ' * (space * 5), end='')
for j in i:
print(f"{j:<10}", end='')
print()
space -= 1
- 7、统计单词数,输入一个要查询的单词以及一段英文短文,求出现单词的个数(不区分大小写)
这题我是默认单词之间以空格隔开了。并且默认最后一个单词后面紧跟标点符号。不区分大小写的话就将所有字母转成小写字母,再将字符串用split方法按空格切割。考虑到最后一个单词后面的标点符号,就将标点符号用pop方法弹出。
cnt = 0
word = input("input the word:")
word = word.lower()
strings = input("input test passage: ")
strings = strings.lower()
string = strings.split(' ')
s = list(string[-1])
s.pop()
s = "".join(s)
string[-1] = s
for none in string:
if word in string:
string.remove(word)
cnt += 1
else:
break
print(cnt)
- 8、输入一个正整数,判断是不是回文质数(回文数+质数)
这题就是前面数字反转和输出素数的合体。按前面的方法就行。
a = []
b = []
num = int(input("input your data:\n"))
n = int(pow(num, 0.5))
i = 2
while i < n+1:
if num % i != 0:
i += 1
else:
print("non prime palindromes")
exit(0)
while num >= 10:
a.append(int(num % 10))
num = (num - num % 10) / 10
a.append(int(num))
b = a[::-1]
if a == b:
print("prime palindromes")
else:
print("non prime palindromes")
- 9、计算字符串倒数第二个单词的长度,单词以空格隔开,同时计算最后一个单词中ld包含的次数,给定输入如下。(字符串处理,提示:可以灵活使用split()函数)
// test1
hello world hello worldhellohello worldhello worldhello worldhe1lo worldhello
// test2
worldhello worldhello worldhello worldhello hellohelloworld
// test3
worldworldhelloworldworld
计算长度用len()函数即可。对于最后一个单词包含的ld个数计算一开始并没有想到很好的方法,刚开始是设置sep='l’进行切割统计列表元素开头含d的个数,显然是非常麻烦的。后来倒不如干脆设置sep='ld’切割,计算切割完列表长度-1。
s = input("请输入一串字符串:\n")
s = s.split(' ')
length = len(s[-2])
a = s[-1].split('ld')
print(length, len(a) - 1)
- 10、用字典存储以下10个好朋友的姓名和年龄, 然后: (字典的使用和遍历)-遍历出名字和对应年龄 -只遍历出朋友的名字-按字母顺序遍历出朋友的名字-遍历出所有你朋友的年龄,查看自己朋友都在什么年龄段-把遍历出的年龄去重。- 插入18岁的"xiaoming" -删除大于等于23岁的朋友
friends={
'jiaLiang':23,
'wangChen':24,
'jiaMing':22,
'wangJie':22,
'yuQing':24,
'wenZhen':23,
'haoNan':25,
'luYi':23,
'boWen':23
}
字典的增删改查
name = []
age = []
friend = {
'jiaLiang': 23,
'wangChen': 24,
'jiaMing': 22,
'wangJie': 22,
'yuQing': 24,
'wenZhen': 23,
'haoNan': 25,
'luYi': 23,
'boWen': 23
}
print()
print("names and ages: ")
for k in friend.items():
print(k)
print()
print("names: ")
for names in friend.keys():
print(names)
print()
print("sorted names: ")
for names in sorted(friend.keys()):
print(names)
print()
print("sorted ages: ")
for ages in friend.values():
age.append(ages)
print(sorted(age))
age = list(set(age))
print(sorted(age))
print()
for names, ages in friend.items():
if ages >= 23:
name.append(names)
for k in name:
del friend[k]
print(friend)
print()
friend["xiaoming"] = 18
print(friend)
由于本人水平有限,剩下的附加题还需慢慢研究
总结
通过这次考核,基本了解了python的语法,数据类型等基础知识,上述程序是个人见解可能有些不足之处欢迎大家指出。编程没有捷径,努力定有回报,加油!