-
Chapter 4-15 Exchange coins (20 cents)
Change a change into coins of 5 cents, 2 cents and 1 cent. It is required to have at least one coin of each kind. How many different ways are there?
Input format:
Enter the amount of change to be exchanged x∈(8,100) in one line.
Output format:
It is required to output various exchange methods according to the number of 5 cents, 2 cents and 1 cent coins in descending order. Each line outputs a replacement method, and the format is: "fen5: the number of 5-cent coins, fen2: the number of 2-cent coins, fen1: the number of 1-cent coins, total: the total number of coins." The last line outputs "count = number of substitutions".
Input example:13
Output sample:
fen5:2, fen2:1, fen1:1, total:4 fen5:1, fen2:3, fen1:2, total:6 fen5:1, fen2:2, fen1:4, total:7 fen5:1, fen2:1, fen1:6, total:8 count = 4
x=int(input()) if 8<x and x<100: fen5=0;fen2=0;fen1=0;count=0 fen5=int(x/5) #最多多少个5 ##根据题目要求由内到外循环,依次求得满足条件的最大的5的个数 while fen5>0: fen2=int(x/2) #最多多少个2 while fen2>0: fen1=x ##最多多少个1 while fen1>0: if fen5*5+fen2*2+fen1*1==x: print("fen5:{}, fen2:{}, fen1:{}, total:{}".format(fen5,fen2,fen1,fen1+fen2+fen5)) count=count+1 fen1=fen1-1 fen2=fen2-1 fen5=fen5-1 print("count = {}".format(count))
-
Chapter 4-18 Monkeys choose a king (20 points)
A group of monkeys want to choose a new monkey king. The method of selecting the new monkey king is: let N candidate monkeys form a circle, and number them sequentially from 1 to N starting from a certain position. Counting starts from No. 1, and counts from 1 to 3 in each round. Any monkey that counts 3 will exit the circle, and then the same counting will start from the next monkey. This cycle continues until the last remaining monkey is chosen as the monkey king. May I ask which monkey was originally elected as the Monkey King?
Input format:
Enter a positive integer N (≤1000) in one line.
Output format:
Output the number of the selected monkey king in one line.
Input example:11
Output sample:
7
N=int(input()) if N<=1000: #首先分配编号,设置每个猴子在的标记,如果淘汰,则换标记 l=['n'] for i in range(N): l.append('y') #y表示在,n表示淘汰,为了契合题目,从列表索引1开始 count=0 while(l.count('y')>1): #如果还有一个以上的猴子则继续报数 ##模仿规则,报数淘汰报数为3的猴 for i in range(1,N+1): if l[i]=='y': count=count+1 if count == 3: l[i]='n' count=0 idx=l.index('y') print(idx)
-
Chapter 4-30 Find a complete number (20 points)
The so-called perfect number is that the number is exactly equal to the sum of factors except itself. For example: 6=1+2+3, where 1, 2, and 3 are the factors of 6. This question requires writing a program to find all perfect numbers between any two positive integers m and n.
Input format:
Input two positive integers m and n (1<m≤n≤10000) in one line, separated by spaces.
Output format:
Output line by line the factor accumulation form of each perfect number within a given range. Each perfect number occupies one line. The format is "perfect number = factor 1 + factor 2 + ... + factor k", where the perfect number occupies one line. and factors are given in increasing order. If there is no perfect number in the interval, "None" is output.
Input example:2 30
Output sample:
6 = 1 + 2 + 3 28 = 1 + 2 + 4 + 7 + 14
import math m,n=map(int,input().split()) if 1 < m and m <= n and n <= 10000: flag=False for i in range(m,n+1): l=[1] for j in range(2,int(math.sqrt(i))+1): if i%j==0: l.append(j) if j*j!=i: l.append(i//j) if sum(l)==i: flag=True l.sort() print("{} = {}".format(i,l[0]),end="") for j in l[1:]: print(" + {}".format(j),end="") print() if(flag == 0): print("None")
-
Chapter 5-11 Dictionary merging (40 points)
Dictionary merging. The input is represented by two dictionaries as strings, and the merged dictionary is output. The key of the dictionary is represented by a letter or number. Note: 1 and '1' are different keywords!
Input format:
Enter the first dictionary string in the first line and enter the second dictionary string in the second line.
Output format:
Output the merged dictionary in one line, and the output is in lexicographic order. The ASCII value of "1" is 49, which is greater than 1. When sorting, 1 is in front, "1" is in the back, and the same is true for others.
Input example 1:{1:3,2:5} {1:5,3:7}
Output sample 1:
{1:8,2:5,3:7}
Input example 2:
{"1":3,1:4} {"a":5,"1":6}
Output sample 2:
{1:4,"1":9,"a":5}
dic1=eval(input()) dic2=eval(input()) for k2,v2 in dic2.items(): if dic1.get(k2) == None: dic1[k2]=v2 else: dic1[k2]=dic1[k2]+v2 print("{",end="") l1=[i for i in dic1.keys() if type(i)==type(1)] l2=[i for i in dic1.keys() if type(i)==type('a')] l1.sort() l2.sort() cnt=0 ##这里的重点就是控制输出 for i in l1+l2: cnt=cnt+1 if type(i)==type(1): print("{}:{}".format(i,dic1[i]),end="") elif type(i)==type('a'): print('"{}":{}'.format(i,dic1[i]),end="") if cnt<len(dic1): print(",",end="") print("}")
Some questions from the Zhejiang University edition of "Python Programming" question set
Guess you like
Origin blog.csdn.net/weixin_43489041/article/details/111399891
Ranking