1 #!/usr/bin/python
2 # -*- coding: UTF-8 -*-
3 import string
4 import math
5 import time
6 import sys
7 import os
8 #import pygame
9 #eg1:有1,2,3,4的数字,可以由这几个不同的数字组成,多少组没有重复的三位数?
10 for i in range(1,5):
11 for j in range(1,5):
12 for k in range(1,5):
13 if (i != k) and (i != j) and (j != k):
14 print("The number is ",i,j,k)
15 #eg2:企业按照利润佣金支付的奖金。利润总额1.5%之间7.5%的利润(I)低于或等于1000万元,奖金提供10%;利润1000万元以上,2000万元以下,1000万元以下,10%以内扣除一个百分比,从一笔钱中扣除一个百分
比,1000万元以上的佣金;2000万到4000万元,2000万元以上的佣金,给佣金5%;4000万到6000万元以上,给佣金3%;六千万到一亿元,六千万多万元,从一笔钱中扣除一个百分比,一亿多元以上,一亿多元按1%佣金,
从键盘输入月利润一次,想要奖金吗?
16 #Solution 1
17 percent_less10mili = 0.1
18 percent_less20mili = 0.075
19 percent_less40mili = 0.05
20 percent_less60mili = 0.03
21 percent_less100mili = 0.015
22 percent_last = 0.01
23 I=input("Please enter a the profit for this year :")
24 i=int(I)
25 if i <= 10:
26 Sum = i*percent_less10mili
27 elif 10 < i and i <= 20:
28 Sum = 1+(i-10)*percent_less20mili
29 elif 20 < i and i <= 40:
30 Sum = 1+0.75+(i-20)*percent_less40mili
31 elif 40 < i and i <= 60:
32 Sum = 1+0.75+1+(i-40)*percent_less60mili
33 elif 60 < i and i <= 100:
34 Sum = 1+0.75+1+0.6+(i-60)*percent_less100mili
35 elif 100 < i:
36 Sum = 1+0.75+1+0.6+0.6+(i-100)*percent_last
37 else :
38 print("Your have enter a wrong number!")
39 print("The profit of this year for MaMiao",Sum)
40 #Solution 2
41 i = int(input("The profit:"))
42 arr = [1000000,600000,400000,200000,100000,0]
43 rat = [0.01,0.015,0.03,0.05,0.075,0.1]
44 r = 0
45 for idx in range(0,6):
46 if i>arr[idx]:
47 r+=(i-arr[idx])*rat[idx]
48 print((i-arr[idx])*rat[idx])
49 i=arr[idx]
50 print(r)
51 #eg3:一个整数,加上100和268是一个完美的平方,这个数字是多少?
52 #Solution 1
53 n=0
54 m=0
55 for K_f in range(1,12):#This algorithm has a simple mathematical pretreatment and analysis, to a certain extent, the time complexity of the algorithm is simplified.
56 K_s = 168/K_f
57 if (K_s == int(K_s)):
58 m=(K_s+K_f)/2
59 n=(K_s-K_f)/2
60 if n == int(n) and m == int(m) and n!=0 and m!=0:
61 print(m,n)
62 print(m*m-268)
63 #Solution 2
64 for i in range(10000):
65 x = int(math.sqrt(i + 100))
66 y = int(math.sqrt(i + 268))
67 if(x * x == i + 100) and (y * y == i + 268):
68 print(i)
69 #eg4:进入某一天,审判日是今年的头几天吗?
71 month = int(input("Please enter the month:"))
70year = int(input("Please enter the year:"))
72 day = int(input("Please enter the day:"))
73 sum = 0
74 month_day=(31,28,31,30,31,30,31,31,30,31,30,31)
75 if (year%4==0 and year%100!=0) or year%400==0 :
76 day_plus = 1
77 print("This year is a leap year!")
78 else :
79 day_plus = 0
80 print("This year is not a leap year!")
81 if 2 < month :
82 sum += day_plus
83 for i in range(0,month-1):
84 sum += month_day[i]
85 sum += day
86 print("The sum of today is equal to:",sum)
87 #eg5:输入三个整数x,y,z,请把三个数字的从小到大输出。
88 #Solution 1
89 def compare(A,B):
90 if A>B:
91 result = A
92 else:
93 result = B
94 return result
95 NUM1=int(input("Please enter the first number:"))
96 NUM2=int(input("Please enter the second number:"))
97 NUM3=int(input("Please enter the third number:"))
98 Big1=compare(NUM1,NUM2)
99 Big2=compare(Big1,NUM3)#caculate the biggest one
100 if NUM1 == Big2:
101 NUM1=0
102 if NUM2 == Big2:
103 NUM2=0
104 if NUM3 == Big2:
105 NUM3=0
106 middle1=compare(NUM1,NUM2)
107 middle2=compare(middle1,NUM3)#caculate the second biggest one
108 if NUM1 == middle2:
109 NUM1=0
110 if NUM2 == middle2:
111 NUM2=0
112 if NUM3 == middle2:
113 NUM3=0
114 small1=compare(NUM1,NUM2)
115 small2=compare(small1,NUM3)#caculate the second biggest one
116 print(small2,middle2,Big2)
117 #Solution 2
118 l = []
119 for i in range(3):
120 x = int(input("Please enter the first number:"))
121 l.append(x)
122 l.sort()
123 print(l)
124 #eg6:Fibonacci序列
125 #Solution 1
126 F = [0,1,1]
127 print(F[0])
128 print(F[1])
129 for i in range(1,10):
130 F[0]=F[1]
131 F[1]=F[2]
132 F[2]=F[0]+F[1]
133 print(F[2])
134 #Solution 2
135 def fib(n):
136 if n==1 or n==2:
137 return 1
138 return fib(n-1)+fib(n-2)
139 print(fib(10))
140 #将数据列表复制到另一个列表中。
141 #Solution 1
142 a = [1, 2, 3]
143 b = a[:]
144 print(b)
145 #Solution 2
146 c=[0,0,0]
147 for i in range(0,3):
148 c[i]=a[i]
149 print(c)
150 #eg8:9*9乘法表的输出
151 for i in range(1,10):
152 for j in range(1,10):
153 print(i,"*",j,"=",i*j)
154 #eg9:暂停1秒输出
155 #它需要导入名为time的头文件,如下所示:导入时间(import time)
156 myD = {1: 'a', 2: 'b'}
157 for key,value in dict.items(myD):
158 print(key, value)
159 print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
160 time.sleep(1)
161 print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
162 #确定101-200之间有多少素数,以及所有素数的输出。
163 h = 0
164 leap = 1
165 from math import sqrt
166 from sys import stdout
167 for m in range(101,201):
168 k = int(sqrt(m + 1))
169 for i in range(2,k + 1):
170 if m % i == 0:
171 leap = 0
172 break
173 if leap == 1:
174 print('%-4d' % m)
175 h += 1
176 if h % 10 == 0:
177 print('')
178 leap = 1
179 print('The total is %d' % h)
180 #打印出所有的“水仙花”,所谓的“水仙花”是一个三位数。所有数字立方体,等于数字本身。例如:153是“水仙花”,因为153=1^3+5^3+3^3
181 for i in range(100,1000):
182 sum = math.pow(int(i/100),3)+ math.pow(int((i%100)/10),3)+ math.pow(i%10,3)
183 if sum==i:
184 print("This number is a daffodils number equal to:",i)
185 #eg11:正整数分解例如:输入90,打印出90=2*3*3*5
186 n = int(input("input number:\n"))
187 print("n = %d" % n)
188 for i in range(2,n + 1):
189 while n != i:
190 if n % i == 0:#All the number,which is not a prime number can't appear at there,because that the number(not prime number) have ever been diverse to be the premi number ,for Example:10 have already been diverse to be 2 multiple 5
191 print(str(i))
192 print("*")
193 n = n / i
194 else:
195 break
196 print("%d"% n)
197 #eg11使用条件运算符来完成这个问题:研究结果>=90分的学生用A,60-89分之间用B,用C表示以下60分
198 Score = int(input("Please enter the score:"))#Python haven't the loop function for three member like this : A = expression ? "Q" : "P"
199 if Score < 60:
200 print("Your Grade is equal to C")
201 elif 60<= Score < 90:
202 print("Your Grade is equal to B")
203 else :
204 print("Your Grade is equal to A")
205 #eg12:输入一行字符,分别统计英文字母、空格、数字和其他字符的数字。
206 write = "Come on,Baby!"
207 write = write.encode()
208 fo = open("eg_test.txt",'wb',1000)
209 print("The filename is :",fo.name)
210 print("The Rquest_Model of files :",fo.mode)
211 print("This is the sentence which I have already written into the file named ",fo.name,write)
212 fo.write(write)
213 fo.close()
214 fo = open("eg_test.txt","rb",1)
215 Str = fo.read(15)
216 Str = Str.decode()#You have to decode the string to count how many times the letter "C" have appear in the str!
217 print("The string i have already read from txt file is :",Str)
218 C = Str.count('C')
219 Where = Str.find("Ba")
220 print("The latter C have appear for ",C,"times!")
221 print("The string om have appear at the address:",Where)
222 fo.close()
223 letters = 0
224 space = 0
225 digit = 0
226 others = 0
227 for c in Str:
228 if c.isalpha():#remember the function to find alpha
229 letters += 1
230 elif c.isspace():#rememeber the function to find the space
231 space += 1
232 elif c.isdigit():#remember the function to find the digit
233 digit += 1
234 else:
235 others += 1
236 print('char = %d,space = %d,digit = %d,others = %d' % (letters,space,digit,others))#输出
237 #eg13:检测键盘以控制键的大小
238 print("Please enter the keyboard values,this example is used to detect the values of top bottom right and left values of keyboard!")
239 top = input("Please enter the top key in keyboard!")
240 #bottom = input("Please enter the bottom key in keyboard!")
241 #left = input("Please enter the left key in keyboard!")
242 #right = input("Please enter the right key in keyboard!")
243 print("The top values is equal to ",top,int(top))
244 #print("The bottom values is equal to ",bottom,int(bottom))
245 #print("The left values is equal to ",left,int(left))
246 #print("The right values is equal to ",right,int(right))
247 #eg14:求s=a+aa+aaa+aaaa+aa.A,其中a是一个数字。例如,2+22+222+2222+22222(此时总共加5个数字),几个数字加一个键盘控件。
248 K = int(input("Please enter a Number to calculate:"))
249 N = int(input("Please enter a Number to define how many time you want to calculate:"))
250 #for i in range(1,10):#范围是1到10-1=9,记住了!!!
251 # print(i)
252 #for i in range(10):#范围是0到9之间,记住了!!!
253 # print(i)
254 sum = 0#出初始化一定要做到啊!!!!
255 for i in range(1,N+1):
256 temp = i*math.pow(10,N-i)
257 sum += temp
258 print("The precudure is :",temp,"+")
259 print(sum)
260 print("The result is calculate to be :",sum*K)
261 #eg15:如果一个数字完全等于它的因子之和,这个数字被称为“数的结束”。例如,6=123。编程以在1000内找到全部完成
262 i=1
263 while (i<=1000):
264 Buffer = i
265 for j in range(1,i):
266 if (i%j==0):
267 Buffer -= j
268 if (Buffer==0):
269 print("This number is a complete number equal to:",i)
270 i += 1
271 #eg16:支乒乓球队参加比赛,三人中各有一人。A队,B队,C3队,B队,X队,Y队,Z3队。抽签。有人问球队比赛的情况。A说他没有x比,C说他没有和Z,X比,请编程序找出三队比赛的名单
272 Heigth = int(input("Please enter a Number for Heigth:"))
273 sum = Heigth
274 Times = int(input("Please enter a Number for calculate times:"))
275 print("The Heigth of 10th bounce is equal to :",math.pow(0.5,Times)*Heigth)
276 for i in range(1,Times):
277 sum += 2*Heigth*math.pow(0.5,i)
278 print("The total length is equal to:",sum)
279 #eg17:Two table tennis team for the game, each out of the three. A team for the A, B, C three, B team for the X, y, Z three. Draw a draw. Someone asked the team about the competition. A said he did not x than, C said he does not and Z, X than, please make up the program to find out the list of three teams race
280 for i in range(ord('x'),ord('z') + 1):
281 for j in range(ord('x'),ord('z') + 1):
282 if i != j:
283 for k in range(ord('x'),ord('z') + 1):
284 if (i != k) and (j != k):
285 if (i != ord('x')) and (k != ord('x')) and (k != ord('z')):
286 print('order is a -- %s\t b -- %s\tc--%s' % (chr(i),chr(j),chr(k)))
287 #这种题目有很强的抽象性,一定要注意求解的方法
288 #eg8:打印下列图案(钻石)
289 print(" *")
290 print(" ***")
291 print(" *****")
292 print("*******")
293 print(" *****")
294 print(" ***")
295 print(" *")
296 #eg19:有一个数列:2/1,3/2,5/3,8/5,13/8,21/13,以及该系列前20项的和
297 #分析题目的基本规律:
298 #分子:2+3=5 3+5=8 5+8=13 8+13=21
299 #分母:1+2=3 2+3=5 3+5=8 5+8=13
300 Top_num1=2
301 Bot_num1=1
302 Top_num2=3
303 Bot_num2=2
304 sum=7/2
305 for i in range(18):
306 #Num = (Top_num1+Top_num2)/(Bot_num1+Bot_num2)
307 print("The number is :",Top_num1,Bot_num1)
308 Temp1=Top_num1#中间变量的申请和数据更新及存放注意啊!!
309 Temp2=Bot_num1
310 Top_num1 = Top_num2
311 Bot_num1 = Bot_num2
312 Top_num2 = (Temp1+Top_num2)
313 Bot_num2 = (Temp2+Bot_num2)
314 sum += Top_num2/Bot_num2
315 print(sum)
316 #eg20:对5使用递归方法!
317 def recursive(N):
318 result = N
319 if (N==1):
320 return 1
321 else :
322 result *= recursive(N-1)
323 return result
324 result = recursive(5)
325 print(result)
326 #eg21:通过使用递归函数调用,输入5个字符,以便打印出相反的顺序。
327 #Solution 1:
328 FIFO=['A','B','C','D','E']
329 print("Please enter five letter for FIFO!")
330 for i in range(5):
331 FIFO[i] = input("Please enter the letter:")
332 print(FIFO[0],FIFO[1],FIFO[2],FIFO[3],FIFO[4])
333 print(FIFO[4],FIFO[3],FIFO[2],FIFO[1],FIFO[0])
334 print(FIFO[4]+FIFO[3]+FIFO[2]+FIFO[1]+FIFO[0])
335 #Solution 2(采用递归算法):
336 def output(s,l):
337 if l==0:
338 return
339 print(s[l-1])
340 output(s,l-1)
341 s = FIFO[4]+FIFO[3]+FIFO[2]+FIFO[1]+FIFO[0]
342 l = len(s)
343 output(s,l)
344 #eg22:一个5位数的数字,它不是回文判断。那12321是一个回文,一点也是一千万,几千个一样。
345
346 #del Num
347 Num = 12321
348 print("The number for test is :",Num)
349 if (int(Num/10000)==Num%10) and (int(Num/1000)%10==int((Num%100)/10)):
350 print("This is a palindrome number!")
351 #eg23:请输入本周的第一个字母来确定什么是一周,如果是第一个字母,则继续确定第二个字母。
352 days = ['Monday','Thuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
353 print("Please enter the first letter to determine the day in week:")
354 Ch1=input("Please enter the first letter:")
355 if Ch1=='M':
356 print("Today is Monday!")
357 elif Ch1=='W':
358 print("Today is Wednsday!")
359 elif Ch1=='F':
360 print("Today is Friday!")
361 elif Ch1=='T':
362 print("Please enter the second letter for determined!")
363 Ch2=input()
364 if Ch2=='h':
365 print("Today is Thuesday!")
366 else :
367 print("Today is Tursday!")
368 else:
369 print("Please enter the second letter for detemined!")
370 Ch2=input()
371 if Ch2=='a':
372 print("Today is Saturday!")
373 else :
374 print("Today is Sunday!")
375 #eg24:*逗号分隔 list按逗号分隔列表
376 L = [1,2,3,4,5]
377 s1 = ','.join(str(n) for n in L)
378 print(s1)
379 #eg25:文本颜色设置--示例35
380 class bcolors:
381 HEADER = '\033[95m'
382 OKBLUE = '\033[94m'
383 OKGREEN = '\033[92m'
384 WARNING = '\033[93m'
385 FAIL = '\033[91m'
386 ENDC = '\033[0m'
387 BOLD = '\033[1m'
388 UNDERLINE = '\033[4m'
389 print(bcolors.WARNING + "Warning mesg color?" + bcolors.ENDC)