Hello！小伙伴！

## 第五章

### 5-1 输出星期名缩写 (70 分)

1 Mon
2 Tue
3 Wed
4 Thu
5 Fri
6 Sat
7 Sun

1

Mon

#### 解答

``````days = {1:'Mon',2:'Tue',3:'Wed',4:'Thu',5:'Fri',6:'Sat',7:'Sun'}
index = int(input())
print(days[index])``````

### 5-2 图的字典表示 (20 分)

#### 题目

4
{‘a’:{‘b’:10,‘c’:6}}
{‘b’:{‘c’:2,‘d’:7}}
{‘c’:{‘d’:10}} {‘d’:{}}

4 5 35

#### 解答

``````n=int(input())
sum=0
length=0
for i in range(n):
dic=eval（input())
for j in dic:
temp=dic[j]
for k in temp:
sum+=1
length+=temp[k]
print("{} {} {}".format(n,sum,length))``````

### 5-3 四则运算（用字典实现） (30 分)

7 / 3

2.33

10 / 0

divided by zero

#### 解答

``````res = {'+': 'x+y', '-': 'x-y', '*': 'x*y',
'/': "x/y if y!=0  else 'divided by zero'"}
# 除法部分分三元式表示
x = int(input())
operation = input()
y = int(input())
# print(res[operation]) 除零返回字符串，其余返回数字类型
r = eval（res[operation])
if type(r) != str:
print(format(r, '.2f'))
else:
print(r)``````

### 5-4 分析活动投票情况 (20 分)

#### 题目

1,5,9,3,9,1,1,7,5,7,7,3,3,1,5,7,4,4,5,4,9,5,10,9

6 8

#### 解答

``````tickets=list(set(map(int,input().split(','))))
a=[]
for i in range(6,11):
if i not in tickets:
a.append(i)
print(' '.join('%s' %id for id in a))``````

``````tickets=list(set(map(str,input().split(','))))
a=[]
for i in range(6,11):
if str(i) not in tickets:
a.append(str(i))
ans=' '.join(a)
print(ans)``````

### 5-5 统计字符出现次数 (20 分)

#### 题目

programming is More fun! m

2

#### 解答

``````str1=input()
a=input()
count=0
for i in str1:
if i==a:
count+=1
print(count)``````

### 5-6 统计工龄 (20 分)

#### 题目

8 10 2 0 5 7 2 5 2

0:1 2:3 5:2 7:1 10:1

#### 解答

``````n=int(input())
ages=list(map(int,input().split()))
dic={}
for i in ages:
dic[i]=dic.get(i,0)+1
for i in sorted(dic):
print("{}:{}".format(i,dic[i]))``````

### 5-7 列表去重 (40 分)

#### 题目

[4,7,5,6,8,6,9,5]

4 7 5 6 8 9

#### 解答

``````lst1=eval（input())
lst2=list(set(lst1))
print(' '.join(map(str,sorted(lst2,key=lst1.index))))``````

### 5-8 能被3,5和7整除的数的个数（用集合实现） (30 分)

10 100

0

1000 100000

943

#### 解答

``````m,n=map(int,input().split())
set_1=set()
set_2=set()
set_3=set()
for i in range(m,n+1):
if i%3==0:
if i%5==0:
if i%7==0:
print(len(set_1 & set_2 & set_3))``````

### 5-9 求矩阵鞍点的个数 (30 分)

4
1 7 4 1
4 8 3 6
1 6 1 2
0 7 8 9

1

2 1 7 4 1

0

3
4 7 8
1 3 3
2 3 1

2

#### 解答

``````n=int(input())
a=[]
count=0
for i in range(n):
a.append(list(map(int,input().split())))
for i in range(n):
max_num_row=max(a[i][k] for k in range(n))
for j in range(n):
max_num_col=min(a[k][j] for k in range(n))
if max_num_row == max_num_col:
count+=1
print(count)``````

``````n = int(input())
# 原矩阵
lis = [list(map(int, input().split()))for i in range(n)]
# 转置矩阵
lis_1 = [[lis[j][i] for j in range(n)]for i in range(n)]
print(lis_1)
# 依次遍历lis max(lis[i])：原矩阵行最大值 min(lis_1[j])：原矩阵列最大值
print(len([0 for i in range(n)
for j in range(n) if lis[i][j] == max(lis[i]) == min(lis_1[j])]))``````

### 5-10 两数之和 (30 分)

2,7,11,15 9

0 1

3,6,9 10

#### 解答

``````lst=list(map(int,input().split(',')))
target=int(input())
m=dict()
for i,num in enumerate(lst):
if target-num in m:
print("{} {}".format(m[target-num],i))
exit()
m[num]=i

### 5-11 字典合并 (40 分)

#### 题目

“1” 的 ASCII 码为 49，大于 1，排序时 1 在前，“1” 在后。其它的字符同理。

{1:3,2:5}
{1:5,3:7}

{1:8,2:5,3:7}

{“1”:3,1:4} {“a”:5,“1”:6}

{1:4,“1”:9,“a”:5}

#### 解答

``````dict1=dict(eval（input()))
dict2=dict(eval（input()))
for i in dict2:
if i in dict1:
dict1[i]+=dict2[i]
else:
dict1[i]=dict2[i]
intKeys=[]
strKeys=[]
for i in dict1.keys():
if type(i)==int:
intKeys.append(i)
else:
strKeys.append(i)
intKeys.sort()
strKeys.sort()
ans=[]
for i in intKeys+strKeys:
temp=""
if type(i)==int:
temp=str(i)+":"+str(dict1[i])
else:
temp='"'+i+'"'+":"+str(dict1[i])
ans.append(temp)
s1=','.join(ans)
s1='{'+s1+'}'
print(s1)``````