PAT乙级(1-5)

001

n = int(input())
num = 0
while n != 1:
    s = n % 2
    if s == 0:
        n = n / 2
    else:
        n = (3 * n + 1) / 2
    num = num + 1
    if n == 1:
        break
print(num)

002

s = input()   #从键盘接收输入,输入默认是字符串类型
length = len(s)     #字符串长度
sum = 0
pinyin={0: 'ling', 1: 'yi', 2: 'er', 3: 'san', 4: 'si', 5: 'wu', 6: 'liu', 7: 'qi', 8: 'ba', 9: 'jiu'}
#求和
for i in range(len(s)):
    sum = sum+int(s[i])

yushu = []
while sum > 0:
    yushu.append(sum % 10)
    sum = int(sum / 10)

output_len = len(yushu)
j = output_len-1
while j>0:
    print(pinyin[yushu[j]],end=" ")
    j = j-1
print(pinyin[yushu[0]])


003

import re
nums = input()  # 接受输入值
result = [str(0) for i in range(int(nums))]  # 初始化结果列表

for i in range(int(nums)):
    t_str = input()
    if ('P' in t_str) & ('T' in t_str):  # 排除包含多个 P、T的情况,即代替模式匹配 A*PA+TA*、PA+T
        p_cnt, a_cnt, t_cnt, = t_str.count('P'), t_str.count('A'), t_str.count('T')  # 统计PAT个数
        if (p_cnt == 1)&(t_cnt == 1)&(t_str.index('P')<t_str.index('T')):  # 存在单个 P、T,并且P在T之后
            if (p_cnt*a_cnt*t_cnt!=0) & (p_cnt+a_cnt+t_cnt==len(t_str)):  # 规律
                tmp = re.split('P|T',t_str)  # 分片
                # 分支统计结果
                # 判断P之前的A的个数*PT之间A的个数等于T之后A的个数
                result[i] = 'YES' if tmp[0].count('A')*tmp[1].count('A')==tmp[2].count('A') else 'NO'
            else:
                result[i] = 'NO'
        else:
                result[i] = 'NO'
    else:
        result[i] = 'NO'

for val in result:
    print(val)

004

n = input()  #从键盘接收输入学生信息的个数
name = []    #List类型,姓名
number = []  #list类型,学号
score = []   # list类型,分数

while n != 0:
      s = input()
      name.append(s.split(' ')[0])  #把s这个List类型里面的下标为[0]的那一部分分离出来,追加到name
      number.append(s.split(' ')[1])#下标为1的那部分,分离出来,追加到number这个list后面
      score.append(int(s.split(' ')[2])) #类比上面,碰到空格分离,因为这一行输入的是数字,所以需要强制转换成整型
      n = int(n)-1

max_tag = score.index(max(score))   #用内置函数可以直接求出max
min_tag = score.index(min(score))   #用内置函数直接求出min
print(name[max_tag], number[max_tag]) #输出分数最大值对应的学生的名字和学号
print(name[min_tag], number[min_tag]) #输出分数最小值对应的学生的姓名和学号

005

K = int(input())
n = input().split(" ")#将输入按照集合存起来,以空格为分隔符
list1 = []
for i in range(K):
    list1.append(int(n[i]))

list2 = set()
for i in list1:
    while i != 1:
        if i % 2 == 0:
            i = i / 2
        else:
            i = (3 * i + 1) / 2

        if int(i) in list1:
            list2.add(int(i))

list3 = list(set(list1) - list2)  # 把重复元素去除

list3.sort(reverse=True)  # 排序

add = ' '
for i in list3:
    add += ' ' + str(i)
print(add.strip())#去除头尾空格
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值