Python习题十

6-1 jmu-python-组合数据类型-1.计算坐标点欧氏距离

读取若干个点,每个点放入元组。并将所有点的点信息、点的类型、点与原点的距离打印出来。

函数接口定义:

readPoint() #从一行以,分隔的数中读取坐标,放入元组并返回
distance(point) #计算point与原点的距离并返回,要math库中的函数

裁判测试程序样例:

/* 请在这里填写答案 */
n = int(input())
for i in range(n):
    p = readPoint()
    print('Point = {}, type = {}, distance = {:.3f}'.format(p,type(p),distance(p)))

输入格式:

输入n,代表底下要输入n行点坐标。坐标全部为整数。
点坐标x,y,z以,分隔。坐标全部为整数。

注意: 坐标以,分隔,相应位置可能无字符或者包含多个空格字符,读入时按照0进行处理。

输出格式:

见输出样例

输入样例:

5
1,1,1
,,
2,,1
3,1,3
5,,

输出样例:

Point = (1, 1, 1), type = <class 'tuple'>, distance = 1.732
Point = (0, 0, 0), type = <class 'tuple'>, distance = 0.000
Point = (2, 0, 1), type = <class 'tuple'>, distance = 2.236
Point = (3, 1, 3), type = <class 'tuple'>, distance = 4.359
Point = (5, 0, 0), type = <class 'tuple'>, distance = 5.000

代码长度限制 16 KB

时间限制 400 ms

内存限制 64 MB

参考答案

#方法一
def readPoint():
    n = input()
    n = n.split(',')
    m = []
    for i in n:
        if i != '':
            m.append(int(i))
        else:
            m.append(0)
    m = tuple(m)
    return m
 
 
def distance(point): 
    sum1 = 0
    for i in point:
        sum1 += i ** 2
    return sum1 ** 0.5
#方法二
import math

def readPoint():
    coord = input().split(',')
    t=tuple(map(int,["0"+i for i in coord]))
    return t
    
def distance(point):
    d = pow(point[0],2) + pow(point[1],2)+pow(point[2],2)
    s = math.sqrt(d)
    return s

6-2 求列表中后一个数是前一个数的两倍的数

本题要求实现一个函数doubles(),带一个整数列表作为输入参数,输出列表中正好是前一个数的两倍的整数,每个数占一行。

函数接口定义:

doubles(lst)

lst是一个整数列表

裁判测试程序样例:

#  请在这里填写答案 

def main():
    mylist = list(map(int,input().split(",")))
    doubles(mylist)

main()

输入样例:

 3,0,1,2,3,6,2,4,5,6,12

输出样例:

在这里给出相应的输出。例如:

2
6
4
12

代码长度限制 16 KB

时间限制 400 ms

内存限制 64 MB

参考答案

#方法一
def doubles(lst: list):
    for i in range(len(lst) - 1):
        if lst[i] * 2 == lst[i + 1]:
            print(lst[i + 1])
#方法二
def doubles(lst):
    for i in range(1,len(lst)):
        if lst[i]==lst[i-1]*2:
            print(lst[i])

6-3 整数数位和

编写一个函数,该函数接受一个正整数作为参数,返回该整数的各位数字之和。

函数接口定义:

def digitSum(v)

v为输入整数(正整数);
函数返回一个整数,其值为v的各位数字之和。

裁判测试程序样例:

a = int(input())
print(digitSum(a))

输入样例:

291

输出样例:

12

代码长度限制 16 KB

时间限制 400 ms

内存限制 64 MB

参考答案

#方法一
def digitSum(v):
    lis = list(str(v))
    lis = list(map(int, lis))
    return sum(lis)
#方法二
def digitSum(v):
    return sum(map(int,str(v)))

6-4 特殊的数

一个特殊的正整数,它加上150后是一个完全平方数,再加上136又是一个完全平方数。从键盘输入 一个整数 n,求符合条件从n开始的最小的一个数。
用函数来实现判断一个数的是否是完全平方数。

函数接口定义:

在这里描述函数接口。例如:
def  Perfectsquare (x )

裁判测试程序样例:

在这里给出函数被调用进行测试的例子。例如:

/* 请在这里填写答案 */


n  = int(input())
while True:
    if Perfectsquare(n+150):        
        if Perfectsquare(n+136+150):                
            print(n)                        
            break                        
    n = n+1

输入样例:

在这里给出一组输入。例如:

10

输出样例:

在这里给出相应的输出。例如:

75

代码长度限制 16 KB

时间限制 400 ms

内存限制 64 MB

参考答案

#方法一
def Perfectsquare(x):
    if int(x ** 0.5) == x ** 0.5:
        return True
    else:
        return False
#方法二
def Perfectsquare(x):
    a=int(pow(x,0.5))
    if a*a==x:
        return True
    else:
        return False

7-1 jmu-Java&Python-统计文字中的单词数量并按出现次数排序

现在需要统计若干段文字(英文)中的单词数量,并且还需统计每个单词出现的次数

注1: 单词之间以空格(1个或多个空格)为间隔。
注2: 忽略空行或者空格行。

基本版:
统计时,区分字母大小写,且不删除指定标点符号。

进阶版:

  1. 统计前,需要从文字中删除指定标点符号!.,:*?。 注意:所谓的删除,就是用1个空格替换掉相应字符。
  2. 统计单词时需要忽略单词的大小写。

输入说明

若干行英文,最后以!!!!!为结束。

输出说明

单词数量
出现次数排名前10的单词(次数按照降序排序,如果次数相同,则按照键值的字母升序排序)及出现次数。

输入样例1

failure is probably the fortification in your pole

it is like a peek your wallet as the thief when you
are thinking how to spend several hard-won lepta
          
when you are wondering whether new money it has laid
background because of you then at the heart of the
     
most lax alert and most low awareness and left it

godsend failed
!!!!!

输出样例1

46
the=4
it=3
you=3
and=2
are=2
is=2
most=2
of=2
when=2
your=2

输入样例2

Failure is probably The fortification in your pole!

It is like a peek your wallet as the thief when You
are thinking how to. spend several hard-won lepta.

when yoU are? wondering whether new money it has laid
background Because of: yOu?, then at the heart of the
Tom say: Who is the best? No one dare to say yes.
most lax alert and! most low awareness and* left it

godsend failed
!!!!!

输出样例2

54
the=5
is=3
it=3
you=3
and=2
are=2
most=2
of=2
say=2
to=2

代码长度限制 16 KB

时间限制 400 ms

内存限制 64 MB

参考答案

#方法一
dict1 = {}
sum1 = 0
while True:
    str = input()
    lis = list(str.lower().split())
    if len(lis) == 0:
        continue
    else:
        if "!!!!!" in lis:
            break
        for i in lis:
            for q in "!.,:*?":
                i = i.replace(q, '')
            if i.lower() in dict1:
                dict1[i.lower()] += 1
            else:
                dict1[i.lower()] = 1
sum1 = len(dict1)
print(sum1)
sum1 = 0
for i in sorted(dict1.items(), key=lambda x: (-x[1], x[0])):
    if sum1 == 10:
        break
    print("%s=%s" % (i[0], i[1]))
    sum1 += 1
#方法二
dic = {}

while True:
    x=input()
    if x=="!!!!!":
        break
    else:
        n = x.lower()
        n = n.replace('!',' ').replace('!', ' ').replace(',', ' ').replace('?', ' ').replace('*', ' ').replace(':', ' ').split()
        for i in n:
            dic[i] = dic.get(i,0)+1

print(len(dic))
d = sorted(dic.items(),key = lambda x:(-x[1],x[0]))[:10]

for i,j in d:
    print(i,'=',j,sep='')

7-2 解析车间里的阀门状态

CPU通过一个8位IO口读取了1个字节的内容,现在存储在一个bytes对象里,示例: b’\x45’;这8位分
别代表了车间里8个阀门的当前状态,1表示该阀门通,0表示该阀门断。请设计一个程序,从bytes对象解析出8个
阀门的当前状态,True表示通,False表示断。这8个状态应组织在一个列表中,其中,第i个元素对应输入字节的第i
位。

输出格式示例:[True, False, False, True, True,True,False,False]

输入格式:

形如 b’\x45’的单字节bytes。(注意是16进制)

输出格式:

包含8个布尔值的列表。其中,第i个元素代表输入字节的第i位(从低到高分别是0 ~ 7位)。

[True, False, True, False, False, False, True, False]

输入样例:

b'\x01'

输出样例:

[True, False, False, False, False, False, False, False]

代码长度限制 16 KB

时间限制 400 ms

内存限制 64 MB

参考答案

#方法一
bit = eval(input())
lis1 = list(bin(int.from_bytes(bit, byteorder='little', signed=False)).replace('0b', ''))
lis2 = list(reversed(lis1))
lis3 = []
if len(lis2) < 8:
    while True:
        lis2.append('0')
        if len(lis2) == 8:
            break
for i in range(8):
    if lis2[i] == '1':
        lis3.append(True)
    else:
        lis3.append(False)
print(lis3)
#方法二
value = eval(input())
x=str(bin(value[0]))[2:]
s=x[::-1]+"0"*(8-len(str(x)))
ls=[True if i=="1" else False for i in s]
print(ls)

7-3 统计输入字符串中的单词个数及单词的平均长度

编写一个程序,接受用户输入的一行英文句子(假设该句子仅由英文单词及空格构成,不包括逗号等符号),统计并输出该行句子包含的单词个数及单词的平均长度。

输入格式:

单词1 单词2 … 单词n

输出格式:

单词个数,单词平均长度(保留两位小数)

输入样例:

aaa bbb ccccccccccc

输出样例:

3,5.67

代码长度限制 16 KB

时间限制 400 ms

内存限制 64 MB

参考答案

#方法一
n = list(input().split())
m = len(n)
sum1 = 0
for i in n:
    sum1 += len(i)
print("%d,%.2f" % (m, sum1 / m))
#方法二
n = input().split()
lst=[len(i) for i in n]
print("{},{:.2f}".format(len(lst),sum(lst)/len(lst)))

7-4 列表排序、逆序

已知列表元素为[12,3,48,6,79,63,89,7],对列表进行逆序输出、升序排序输出、逆序排序输出。

输入格式:

输出格式:

列表逆序结果为:[7, 89, 63, 79, 6, 48, 3, 12]
列表升序排序结果为:[3, 6, 7, 12, 48, 63, 79, 89]
列表降序排序结果为:[89, 79, 63, 48, 12, 7, 6, 3]

输入样例:

在这里给出一组输入。例如:

输出样例:

在这里给出相应的输出。例如:

列表逆序结果为:[7, 89, 63, 79, 6, 48, 3, 12]
列表升序排序结果为:[3, 6, 7, 12, 48, 63, 79, 89]
列表降序排序结果为:[89, 79, 63, 48, 12, 7, 6, 3]

代码长度限制 16 KB

时间限制 400 ms

内存限制 64 MB

参考答案

#方法一
lis1 = [12, 3, 48, 6, 79, 63, 89, 7]
lis2 = list(reversed(lis1))
lis3 = sorted(lis1, reverse=False)
lis4 = sorted(lis1, reverse=True)
print("列表逆序结果为:%s\n列表升序排序结果为:%s\n列表降序排序结果为:%s" % (lis2, lis3, lis4))
#方法二
ls = [12,3,48,6,79,63,89,7]
ls.reverse()
print('列表逆序结果为:{}'.format(ls))
print('列表升序排序结果为:{}'.format(sorted(ls)))
print('列表降序排序结果为:{}'.format(sorted(ls,reverse=True)))

7-5 列表插入

输入一个字符串 s 和一个非负整数 i, 列表 ls = [‘2’, ‘3’, ‘0’, ‘1’, ‘5’],在指定的位置 i 和 列表末尾分别插入用户输入的字符串 s。当 i >=5 时,相当于在列表末尾插入两次字符串 s 。

输入格式:

第一行输入一个字符串

第二行输入一个非负整数

输出格式:

插入新数据后的列表

输入样例:

在这里给出一组输入。例如:

a
2

输出样例:

在这里给出相应的输出。例如:

['2', '3', 'a', '0', '1', '5', 'a']

代码长度限制 16 KB

时间限制 400 ms

内存限制 64 MB

参考答案

#方法一
ls = ['2', '3', '0', '1', '5']
n = input()
i = int(input())
if i < 5:
    ls.insert(i, n)
else:
    ls.append(n)
ls.append(n)
print(ls)
#方法二
ls = ['2', '3', '0', '1', '5']
s = input()
i = int(input())
ls.insert(i,s)
ls.append(s)
print(ls)

7-6 奇特的四位数

一个四位数,各位数字互不相同,所有数字之和等于6,并且这个数是11的倍数。
满足这种要求的四位数有多少个?各是什么?

输入格式:

该题目没有输入

输出格式:

第一行输出符合条件的数字个数

第二行以列表形式输出所有满足条件的四位数,列表元素按由小到大顺序排列

输入样例:

在这里给出一组输入。例如:

输出样例:

在这里给出相应的输出。例如:

6
[1023, 1320, 2013, 2310, 3102, 3201]

代码长度限制 16 KB

时间限制 400 ms

内存限制 64 MB

参考答案

#方法一
def digitSum(v):
    lis = list(str(v))
    lis = list(map(int, lis))
    return sum(lis)

def digitsame(v):
    lis = list(str(v))
    lis = list(map(int, lis))
    lis1 = list(set(lis))
    if len(lis1) == 4:
        return True
    return False

lis = []
for i in range(1000, 10000):
    if digitSum(i) == 6 and i % 11 == 0 and digitsame(i):
        lis.append(i)

print(6)
print(lis)
#方法二
ans=[i for i in range(1000,10000) if sum(map(int,str(i)))==6 and len(set(str(i)))==4 and i % 11 == 0]
print(len(ans))
print(ans)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值