第一题:
给定公司N名员工的工龄,要求按工龄增序输出每个工龄段有多少员工。
输入格式:
输入首先给出正整数N(≤105),即员工总人数;随后给出N个整数,即每个员工的工龄,范围在[0, 50]。
输出格式:
按工龄的递增顺序输出每个工龄的员工个数,格式为:“工龄:人数”。每项占一行。如果人数为0则不输出该项。
输入样例:
8
10 2 0 5 7 2 5 2
输出样例:
0:1
2:3
5:2
7:1
10:1
第二题:
编写程序,统计每行字符串中若干整数的和。每行字符串中整数间的分隔符可能有逗号“,”、分号“ ;”和空格,有多少行就求多少行。
输入格式:
任意输入若干行由整数构成的字符串(回车换行),整数间以逗号或空格或分号分隔。测试数确保至少有一行数据,字符串中的整数数据均合法有效。最后以一个回车结束输入。
输出格式:
对应输出原输入串(一行中的字符序列),冒号后输出各个整数之和。
输入样例:
1; 2 ,3
2 3; 4
10,20 30; 40
9
输出样例:
1; 2 ,3:6
2 3; 4:9
10,20 30; 40:100
9:9
第一题:
a=int(input())
b=map(int,input().split())
c={}
for i in b:
if i in c:
c[i]+=1 #键存在就加1
else:
c[i]=1 #而键不存在就c[i]=1
for i in sorted(c.items(),key=lambda x:x[0]): #字典排序(按工龄的递增顺序输出每个工龄的员工个数)
print('{}:{}'.format(i[0],i[1]))
第二题:
n=input()
result=0
num=n
num=num.replace(';',' ').replace(',', ' ').split()
try:
for i in num:
result+=int(i)
print(n+f':{result}')
except:
print('divided by zero')
这样修改即可:
第一题:
a=int(input())
b=map(int,input().split())
c={}
for i in b:
if i in c:
c[i]+=1 #键存在就加1
else:
c[i]=1 #而键不存在就c[i]=1
for i in sorted(c.items(),key=lambda x:x[0]): #字典排序(按工龄的递增顺序输出每个工龄的员工个数)
print('{}:{}'.format(i[0],i[1]))
#第二题
num=[]
ns=[]
while True:
n = input()
if n == '':
break
else:
num .append(n.replace(';', ' ').replace(',', ' ').split())
ns.append(n)
try:
for a,b in zip(num,ns):
result=0
for i in a:
result += int(i)
print(b+f':{result}')
except:
print('divided by zero')
如满意解答,请点采纳。
第一题
a = int(input())
b = input()
c = [int(i) for i in b.split(' ')]
s = set(c)
print(s)
for i in s:
print(f"{i}: {b.replace(' ', '').count(str(i))}")
第一题可以用字符串的count()去统计数量
第二题
while True:
getStr = input("请输入字符串:")
tmp = getStr
tmp = tmp.replace(';',' ')
tmp = tmp.replace(',', ' ')
print(tmp)
tmp = tmp.split()
result = 0
for num in tmp:
result += int(num)
print(getStr+f':{result}')
如果对你有帮助,可以点击我这个回答右上方的【采纳】按钮,给我个采纳吗,谢谢
a=int(input())
b=map(int,input().split())
c={}
for i in b:
if i in c:
c[i]+=1 #键存在就加1
else:
c[i]=1 #而键不存在就c[i]=1
# 这个要减少一层缩进放上面for之外
for i in sorted(c.items(),key=lambda x:x[0]): #字典排序(按工龄的递增顺序输出每个工龄的员工个数)
print('{}:{}'.format(i[0],i[1]))
import re
li=[]
while True:
n=input()
if n=="":
break
li.append(n)
for n in li:
num = re.split(r'[;,\s]+',n.strip())
result = 0
try:
for i in num:
result+=int(i)
print(n+f':{result}')
except:
print('divided by zero')
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632