V1

2021/05/31阅读：97主题：默认主题

# 题目1

Write a program which will find all such numbers which are divisible（可被整除） by 7 but are not a multiple（倍数） of 5, between 2000 and 3200 (both included).The numbers obtained should be printed in a comma-separated sequence on a single line.

## 关键分析：

1. 2000和3200(包括在内)
2. 被7整除但不是5的倍数
3. 打印数字应以逗号分隔，单行

## 关键对策：

1. range(start, end, step)
2. i%7==0 and i%5!=0
3. print(i, end=',')

## 方法实现：

### method 1:

``for i in range(2000,3200+1):  if i%7==0 and i%5!=0:        print(i, end=',')``

### output:

``2002,2009,2016,2023,2037,2044,2051,2058,2072,2079,2086,2093,2107,2114,2121,2128,2142,2149,2156,2163,2177,2184,2191,2198,2212,2219,2226,2233,2247,2254,2261,2268,2282,2289,2296,2303,2317,2324,2331,2338,2352,2359,2366,2373,2387,2394,2401,2408,2422,2429,2436,2443,2457,2464,2471,2478,2492,2499,2506,2513,2527,2534,2541,2548,2562,2569,2576,2583,2597,2604,2611,2618,2632,2639,2646,2653,2667,2674,2681,2688,2702,2709,2716,2723,2737,2744,2751,2758,2772,2779,2786,2793,2807,2814,2821,2828,2842,2849,2856,2863,2877,2884,2891,2898,2912,2919,2926,2933,2947,2954,2961,2968,2982,2989,2996,3003,3017,3024,3031,3038,3052,3059,3066,3073,3087,3094,3101,3108,3122,3129,3136,3143,3157,3164,3171,3178,3192,3199``

## 关键注释：

``def print(self, *args, sep=' ', end='\n', file=None): # known special case of print    """    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)        Prints the values to a stream, or to sys.stdout by default.    Optional keyword arguments:    file:  a file-like object (stream); defaults to the current sys.stdout.    sep:   string inserted between values, default a space.    end:   string appended after the last value, default a newline.    flush: whether to forcibly flush the stream.    """    pass``

• end：可以设置print打印结束时最后跟的字符形式，默认换行符。
• sep：可以设置print中分隔不同值的形式，默认为一个空格。

### method 2：

``[print(i, end=',') for i in range(2000,3200+1) if i%7==0 and i%5!=0]``

### method 3：

``print(*(i for i in range(2000,3200+1) if i%7==0 and i%5!=0), sep=',')``

## 关键注释：

``l=[x*x for x in range(10)]print(l)g=(x*x for x in range(10))print(g)``

### output：

``[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<generator object <genexpr> at 0x000001A7F4A995C8>``

## 关键收获：

1. python中的range(start, end, step)
2. 与(and)或(or)非(!)
3. print()打印操作

# 题目2

Write a program which can compute the factorial（阶乘） of a given numbers. The results should be printed in a comma-separated sequence on a single line.Suppose the following input is supplied to the program: 8 Then, the output should be:40320

1. 数量阶乘
2. 键盘接收输入

## 关键对策：

1. 阶乘，例如：5！=5x4x3x2x1
2. input 键盘接收输入

## 方法实现：

### method 1：

5！=5x4x3x2x1，那也就是5！=1x2x3x4x5

``n=int(input('Please input a int num:'))factorial_num=1x=1for i in range(1,n+1):    factorial_num = factorial_num*iprint(factorial_num)``

### method 2：

``n=int(input('Please input a int num:'))def factorial(x):    return 1 if x<=1 else x*factorial(x-1)print(factorial(n))``

### output:

``Please input a int num:840320``

## 关键收获：

1. input 键盘接收输入方式
2. 阶乘计算方法
3. 自我调用，学名递归调用

# 题目3

With a given integral number n, write a program to generate a dictionary that contains (i, i x i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.Suppose the following input is supplied to the program: 8

## 关键分析：

1. 生成(i, i * i)的字典
2. 范围i属于[1,n]
3. 打印字典

## 关键对策：

1. python dict[key]=value
2. range(1,n+1)
3. print(dict)

## 方法实现：

### method 1：

``n=int(input('Please input a int num:'))d={}for i in range(1,n+1):    d[i]=i**2print(d)``

### method 2：

``n=int(input('Please input a int num:'))print({i:i**2 for i in range(1,n+1)})``

output:

``Please input a int num:8{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}``

## 关键收获：

1. python dict的构建，dict[key]=value
2. python中的平方形式，i**2

Python

V1