#-*- codeing = utf-8 -*-
#@Time : 2021/3/18 21:17
#@Author : HUGBOY
#@File : 1.py
#@Software: PyCharm
无重复数字的三位数?各是多少?
num=0
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if (i!=j)and(i!=k)and(j!=k):
print(i,j,k)
num=num+1
print('共',num,'个。')
利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,
低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;20万到40万之间时,
高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,
高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,
求应发放奖金总数?
我的第一感觉就是用if判断,看了答案的算法太巧妙了。
想让数组倒序来算,但不知道数组倒序输出的方法
i = int(input('净利润:'))
arr=[1000000,600000,400000,200000,100000,0]
rat=[0.01,0.015,0.03,0.05,0.075,0.1]
r=0
for dex in range(0,6):
if i > arr[dex]:
r+=(i-arr[dex])*rat[dex]
print((i-arr[dex])*rat[dex])
i=arr[dex]
print(r)
,再加上168又是一个完全平方数,请问该数是多少?
没有思路的一道题。
程序分析:
1、则:x + 100 = n2, x + 100 + 168 = m2
2、计算等式:m2 - n2 = (m + n)(m - n) = 168
3、设置: m + n = i,m - n = j,i * j = 168,i和j至少一个是偶数
4、可得: m = (i + j) / 2, n = (i - j) / 2,i和j要么都是偶数,要么都是奇数。
5、从3和4推导可知道,i与j均是大于等于2的偶数。6、由于i * j = 168, j >= 2,则1 < i < 168 / 2 + 1。
7、接下来将i的所有数字循环计算即可。
代码:
for i in range(1, 85):
if 168 % i == 0:
j = 168 / i
if i > j and (i + j) % 2 == 0 and (i - j) % 2 == 0:
m = (i + j) / 2
n = (i - j) / 2
x = n * n - 100
print(x)
一年的第几天?
答案的算法复杂度更低,仅适用了判断和加法。我的用了乘法,遍历。
#我的
d=0
year=int(input('year:'))
month=int(input('month:'))
day=int(input('day:'))
arr_month=[0,31,28,31,30,31,30,31,30,30,31,31,30]
if year%100 == 0:
if year%400 == 0:
arr_month[2]=29
if year%4 == 0:
arr_month[2]=29
for i in range(1,month):
d=d+arr_month[i]
print('This is the',d+day,'th day !')
#答案
year = int(input('year:'))
month = int(input('month:'))
day = int(input('day:'))
months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0 < month <= 12:
sum = months[month - 1]
else:
print ('data error')
sum += day
leap = 0
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
leap = 1
if (leap == 1) and (month > 2):
sum += 1
print ('it is the %dth day.' % sum)
这三个数由小到大输出。
给爷整乐了。。。
#HugBoy
x=int(input('x:'))
y=int(input('y:'))
z=int(input('z:'))
if x<y:
if x<z:
if(y<z):
print(x,y,z)
else:
print(x,z,y)
else:
print(z,x,y)
else:
if y>z:
print(z,y,x)
else:
if x<z:
print(y,x,z)
else:
print(y,z,x)
#Answer
l = []
for i in range(3):
x = int(input('integer:'))
l.append(x)
l.sort()
print (l)
斐波那契数列(Fibonacci sequence),
又称黄金分割数列,指的是这样一个数列:0、1、1、2、3、5、8、13、21、34、……。
都差不多。
#HugBoy
dex=int(input('dex:'))
fib = []
fib.append(0)
fib.append(1)
for i in range(2,dex+1):
x=fib[i-1]+fib[i-2]
fib.append(x)
print(fib[dex])
print(fib)
#递归
def fib(n):
if n==1 or n==2:
return 1
return fib(n-1)+fib(n-2)
#遍历
def fib(n):
a,b = 1,1
for i in range(n-1):
a,b = b,a+b
return a
#函数
def fib(n):
if n == 1:
return [1]
if n == 2:
return [1, 1]
fibs = [1, 1]
for i in range(2, n):
fibs.append(fibs[-1] + fibs[-2])
return fibs
a=[1,2,3,4,5,6]
b=a[:]
print(b)
for m in range(1,10):
for n in range(1,m+1):
print('%d*%d = %d'%(m,n,m*n),end=" ")
print()
import time
Name={1:'h',2:'u',3:'g',4:'b',5:'o',6:'y'}
for key,value in dict.items(Name):
print(key,value)
time.sleep(1)
#1
import time,datetime
time_now = datetime.datetime.now()
print(time_now.strftime("%Y.%m.%d %H-%M-%S"))
time.sleep(1)
time_now = datetime.datetime.now()
print(time_now.strftime("%Y.%m.%d %H-%M-%S"))
#2
import time
print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
time.sleep(1)
print (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
#-*- codeing = utf-8 -*-
#@Time : 2021/3/19 12:30
#@Author : HUGBOY
#@File : 2.py
#@Software: PyCharm
都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,
假如兔子都不死,问每个月的兔子总数为多少?
"""
f1 = 1
f2 = 1
for i in range(1,22):
print ('%12ld %12ld' % (f1,f2), end=" ")
if (i % 3) == 0:
print ('')
f1 = f1 + f2
f2 = f1 + f2
"""
from functools import reduce
"""
答案好像还没我的简单哈哈哈
#HugBoy
sum = 0
for i in range(101,201):
for k in range(2,i):
if(i%k==0):
break
else:
if(k==i-1):
sum+=1
print(sum)
#Answer
程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
h = 0
leap = 1
from math import sqrt
from sys import stdout
for m in range(101,201):
k = int(sqrt(m + 1))
for i in range(2,k + 1):
if m % i == 0:
leap = 0
break
if leap == 1:
print ('%-4d' % m)
h += 1
if h % 10 == 0:
print ('')
leap = 1
print ('The total is %d' % h)
"""
个三位数,其各位数字立方和等于该数本身。
# 例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
"""
#HugBoy
for i in range(100,1000):
g=i%10
s=(i%100)//10
b=i//100
if(i==g*g*g+s*s*s+b*b*b):
print('%d = %d+%d+%d'%(i,b*b*b,s*s*s,g*g*g))
#Answer
for n in range(100,1000):
i = n // 100
j = n // 10 % 10
k = n % 10
if n == i*i*i + j*j*j + k*k*k:
print(n)
"""
例如:输入90,打印出90=2_3_3*5。
"""
没做出来,不知道算法思路
#HugBoy
def sig(n):
l=[2,3,5,7,11,13,17,19,23,29,31,37,41]
s=[]
for i in l:
if(n%i==0):
print(i,end="*")
n=n//i
else:
if(n==1):
return 1
sig(n)
sig(456982)
#2Hugboy
def zhishu(n):
print('{} ='.format(n),end=" ")
while n!=1:
for i in range(2,n+1):
if n%i==0:
n//=i
if n == 1:
print(i)
else:
print('{}*'.format(i),end=" ")
break
zhishu(9638)
#Answer
def reduceNum(n):
print ('{} = '.format(n), end=" ")#输出format中第0个值
if not isinstance(n, int) or n <= 0 :
print ('请输入一个正确的数字 !')
exit(0)
elif n in [1] :
print ('{}'.format(n))
while n not in [1] : # 循环保证递归
for index in range(2, n + 1) :
if n % index == 0:
n //= index # n 等于 n//index
if n == 1:
print (index )
else : # index 一定是素数
print ('{} *'.format(index), end=" ")
break
reduceNum(90)
"""
学习成绩>=90分的同学用A表示,60-89分之间的
用B表示,60分以下的用C表示。
"""
score=int(input('input score:'))
if not isinstance(score,int) or score<0 or score>100:
print('input error.')
exit(0)
if score >= 90:
grade = 'A'
elif score >= 60:
grade = 'B'
else:
grade = 'C'
print('%d belong to %s grade.'%(score,grade))
"""
"""
import datetime
if __name__ == '__main__':
print(datetime.date.today().strftime("NowDay = %d/%m/%Y"))#19/03/2021
zlBirthDay=datetime.date(2000,4,10)
print(zlBirthDay.strftime("zlBirthDay = %d/%m/%Y"))#创建时间对象10/04/2000
zlBirthDayNextDay=zlBirthDay + datetime.timedelta(days=1)
print(zlBirthDayNextDay.strftime("NextDay = %d/%m/%Y"))#时间计算10/04/2001
myBirthDay = zlBirthDay.replace(year=zlBirthDay.year+1,month=zlBirthDay.month-2,day=zlBirthDay.day+13)
print(myBirthDay.strftime("myBirthDay = %d/%m/%Y")) # 时间替换23/02/2001
"""
空格、数字和其它字符的个数。
"""
import string
alpha = 0
space = 0
digit = 0
others = 0
s = input("letters:\n")
for c in s:
if c.isalpha():
alpha += 1
elif c.isspace():
space += 1
elif c.isdigit():
digit += 1
else:
others += 1
print('Total: alpha=%d space=%d digit=%d other=%d'%(alpha,space,digit,others))
"""
其中a是一个数字。例如2+22+222+2222+22222
(此时共有5个数相加),几个数相加由键盘控制。
"""
n = int(input("n:"))
a = int(input("a:"))
def sum(a,n):
Tn=0
l=[]
for i in range(1,n+1):
Tn = Tn + a
a = a*10
l.append(Tn)
return reduce(lambda x,y : x+y,l)
print('Total:',sum(a,n))
"""
这个数就称为"完数"。例如6=1+2+3.编程
找出1000以内的所有完数。
"""
lambda灰常好用哈哈
#HugBoy
for i in range(2,1001):
root=[0]
for j in range(1,i):
if i%j==0:
root.append(j)
sum=reduce(lambda x,y : x+y ,root)
if(i == sum):
print(i)
#Answer
from sys import stdout
for j in range(2,1001):
k = []
n = -1
s = j
for i in range(1,j):
if j % i == 0:
n += 1
s -= i
k.append(i)
if s == 0:
print (j)
for i in range(n):
stdout.write(str(k[i]))
stdout.write(' ')
print (k[n])
"""
反跳回原高度的一半;再落下,求它在第10次落
地时,共经过多少米?第10次反弹多高?
"""
So easy !
#HugBoy
high=100.0
distance=100.0
for times in range(1,11):
high*=0.5
if times==10:
break
distance = distance + 2 * high
print(high,distance)
#Answer
tour = []
height = []
hei = 100.0 # 起始高度
tim = 10 # 次数
for i in range(1, tim + 1):
# 从第二次开始,落地时的距离应该是反弹高度乘以2(弹到最高点再落下)
if i == 1:
tour.append(hei)
else:
tour.append(2*hei)
hei /= 2
height.append(hei)
print('总高度:tour = {0}'.format(sum(tour)))
print('第10次反弹高度:height = {0}'.format(height[-1]))
"""
#-*- codeing = utf-8 -*-
#@Time : 2021/3/19 23:06
#@Author : HUGBOY
#@File : 3.py
#@Software: PyCharm
当即吃了一半,还不瘾,又多吃了一个第二天早上又
将剩下的桃子吃掉一半,又多吃了一个。以后每天早
上都吃了前一天剩下的一半零一个。到第10天早上想
再吃时,见只剩下一个桃子了。求第一天共摘了多少。
"""
#HugBoy
tao = 1
for i in range(1,10):
tao = (tao + 1)*2
print(tao)
#Answer
x2 = 1
for day in range(9,0,-1):#从第九天开始,step=-1
x1 = (x2 + 1) * 2
x2 = x1
print (x1)
"""
为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛
名单。有人向队员打听比赛的名单。a说他不和x比
,c说他不和x,z比,请编程序找出三队赛手的名单.
"""
比赛出场问题
1.每人只且仅只对1人
2.考虑定一移一,固定某队的出场顺序
#HugBoy
t1=['a','b','c']
t2=['x','y','z']
for i in t1:
for j in t2:
if(i=='a' and j=='x')or(i=='c' and j=='x')or(i=='c' and j=='z'):
pass
else:
print(i,' PK ',j)
#2HugBoy
t2=['x','y','z']
for i in t2:
for j in t2:
for k in t2:
if(i==j or i==k or j==k)or(i=='x' or k=='x' or k=='z'):
continue
else:
print('a--PK--%s b--PK--%s c--PK--%s'%(i,j,k))
#Answer
for i in range(ord('x'),ord('z') + 1):
for j in range(ord('x'),ord('z') + 1):
if i != j:
for k in range(ord('x'),ord('z') + 1):
if (i != k) and (j != k):
if (i != ord('x')) and (k != ord('x')) and (k != ord('z')):
print ('order is a -- %s\t b -- %s\tc--%s' % (chr(i),chr(j),chr(k)))
"""
#
# *
# ***
# *****
# *******
# *****
# ***
# *
"""
我的代码更健壮,能打印所有边长的菱形
思维定式,其实星星右边的空格不用打印出来的,看不出来。
#HugBoy
def diamond(n):
star=-1
space=n
for line in range(2*n-1):
if line<n:
star += 2
space -= 1
else:
star -= 2
space += 1
for j in range(space):
print(end=" ")
for k in range(star):
print(end="*")
#for j in range(space):
#print(end=" ")
print()
diamond(n=int(input('input diamond length:')))
#Answer
from sys import stdout
for i in range(4):
for j in range(2 - i + 1):
stdout.write(' ')
for k in range(2 * i + 1):
stdout.write('*')
print('')
for i in range(3):
for j in range(i + 1):
stdout.write(' ')
for k in range(4 - 2 * i + 1):
stdout.write('*')
print('')
"""
,13/8,21/13…求出这个数列的前20项之和。
"""
#HugBoy
b=1
Sn=0
def fib(n):
if n==1:
return 2
if n==2:
return 3
else:
return fib(n-1)+fib(n-2)
for i in range(1,21):
a=fib(i)
Sn+=a/b
b=a
print(Sn)
#Answer1
from functools import reduce
a = 2.0
b = 1.0
l = []
l.append(a / b)
for n in range(1,20):
b,a = a,a + b
l.append(a / b)
print (reduce(lambda x,y: x + y,l))
#Answer2
a = 2.0
b = 1.0
s = 0
for n in range(1,21):
s += a / b
t = a
a = a + b
b = t
print (s)
#Answer3
a = 2.0
b = 1.0
s = 0.0
for n in range(1,21):
s += a / b
b,a = a , a + b
print (s)
s = 0.0
for n in range(1,21):
s += a / b
b,a = a , a + b
print (s)
"""
"""
#HugBoy
Sn=0
for i in range(1,4):
n = 1
for g in range(1,i+1):
n*=g
Sn+=n
print(Sn)
#Answer1看做1~20相乘分别相加
n = 0
s = 0
t = 1
for n in range(1,21):
t *= n
s += t
print ('1! + 2! + 3! + ... + 20! = %d' % s)
#Answer2用python内置函数
s = 0
l = range(1,21)
def op(x):
r = 1
for i in range(1,x + 1):
r *= i
return r
s = sum(map(op,l))
print ('1! + 2! + 3! + ... + 20! = %d' % s)
"""
递归公式:fn=fn_1*4!
"""
我是天才hhh
.....忘了考虑0!了鹅鹅鹅
#HugBoy
def fn(n):
if n== 1:#if n==0:
return 1
else:
return n*fn(n-1)
print(fn(3))
#Answer
def fact(j):
sum = 0
if j == 0:
sum = 1
else:
sum = j * fact(j - 1)
return sum
print (fact(5))
"""
的5个字符,以相反顺序打印出来。
"""
def rput(s,l):
if l==0:
return
else:
print(s[l-1],end="")
rput(s, l - 1)
s=input('Enter 5 characters in English:')
l=len(s)
rput(s,l)
"""
少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。
问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
"""
def age(n):
if n==1:
c=10
else:
c=age(n-1)+2
return c
print(age(5))
"""
要求:一、求它是几位数,二、逆序打印出各位数字。
"""
递归上瘾? 答案的思路很简单清晰。
#HugBoy
def nice(n):
if n < 10:
print(n,end=" ")
else:
print(n-n//10*10,end=" ")
nice(n//10)
n=int(input('input a figure (>=1 and <=99999): '))
def fig(n,f):
if n<10:
print(f+1,end="位数:")
else:
n//=10
f+=1
fig(n,f)
fig(n,0)
nice(n)
#Answer
x = int(input("请输入一个数:\n"))
a = x // 10000
b = x % 10000 // 1000
c = x % 1000 // 100
d = x % 100 // 10
e = x % 10
if a != 0:
print ("5 位数:",e,d,c,b,a)
elif b != 0:
print ("4 位数:",e,d,c,b)
elif c != 0:
print ("3 位数:",e,d,c)
elif d != 0:
print ("2 位数:",e,d)
else:
print ("1 位数:",e)
"""
"""
即学即用,eee
#HugBoy
n=int(input('5th number:'))
a = n//10000
b = n%10000//1000
#c = n%1000//100
d = n%100//10
e = n%10
if(a==e and b==d):
print('%d 是回文数.'%n)
else:
print('%d 不是回文数.' % n)
#Answer
a = int(input("请输入一个数字:\n"))
x = str(a)
flag = True
for i in range(len(x)//2):
if x[i] != x[-i - 1]:
flag = False
break
if flag:
print ("%d 是一个回文数!" % a)
else:
print ("%d 不是一个回文数!" % a)
"""
#-*- codeing = utf-8 -*-
#@Time : 2021/3/20 16:08
#@Author : HUGBOY
#@File : 4.py
#@Software: PyCharm
是星期几,如果第一个字母一样,则继续判断第二个字母。
1~7 : Monday-Tuesday-Wednesday-Thursday-Friday-Saturday-Sunday
"""
本以为是要考察BF比较算法,结果答案是用if写的。。。
#HugBoy
week = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
inc = []
flag = 1
inc.append(input('input char:'))
if inc[0]=='T' or inc[0]=='S':
inc.append(input('input char agin:'))
flag = 0
for day in week:
if flag and day[0]==inc[0]:
print(day)
if flag==0 and day[0]==inc[0] and day[1]==inc[1]:
print(day)
"""
"""
#HugBoy
l = [1,2,'dsf','dsfe',5,6,99,8,9,0]
for i in range(len(l),0,-1):
print(l[i-1])
#Answer
a = ['one', 'two', 'three']
for i in a[::-1]:#a[start:stop:step]
print (i)
"""
"""
join()貌似只可链接字符的列表
#HugBoy
l = ['one', 'two', 'three']
i = ','
g = '-'
print(i.join(l))
print(g.join(l))
#Answer
L = [1,2,3,4,5]
s1 = ','.join(str(n) for n in L)
print (s1)
"""
使用函数,输出三次 RUNOOB 字符串。
"""
def hello_runoob():
print ('RUNOOB')
def hello_runoobs():
for i in range(3):
hello_runoob()
if __name__ == '__main__':
hello_runoobs()
"""
"""
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
print (bcolors.HEADER + "警告的颜色字体?" + bcolors.HEADER)
print (bcolors.OKBLUE+"警告的颜色字体?" + bcolors.OKBLUE)
print (bcolors.OKGREEN + "警告的颜色字体?" + bcolors.OKGREEN)
print (bcolors.WARNING + "警告的颜色字体?" + bcolors.WARNING)
print (bcolors.FAIL + "警告的颜色字体?" + bcolors.FAIL)
print (bcolors.ENDC + "警告的颜色字体?" + bcolors.ENDC)
print (bcolors.BOLD + "警告的颜色字体?" + bcolors.BOLD)
print (bcolors.UNDERLINE + "警告的颜色字体?" + bcolors.UNDERLINE)
"""
"""
for里的if 和for外的 else 可以连用
#HugBoy
prime=[]
for i in range(2,101):
flag=1
for n in range(1,i):
if i%n==0 and n != 1:
flag=0
continue
if flag:
prime.append(i)
print(prime)
#Answer
lower = int(input("输入区间最小值: "))
upper = int(input("输入区间最大值: "))
for num in range(lower, upper + 1):
# 素数大于 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
"""
"""
a,b=b,a #a,b互换赋值
if a>b:print('yes') #可以写在一行
#Hugboy
t=0
nb = [2,63,968,56,878,56,0,25,12,68,32,720,63,659]
for i in range(len(nb)):
for j in range(i+1,len(nb)):
if nb[i]>nb[j]:
t = nb[i]
nb[i]=nb[j]
nb[j]=t
print(nb)
#Answer
if __name__ == "__main__":
N = 10
# input data
print ('请输入10个数字:\n')
l = []
for i in range(N):
l.append(int(input('输入一个数字:\n')))
print
for i in range(N):
print (l[i])
print
# 排列10个数字
for i in range(N - 1):
min = i
for j in range(i + 1,N):
if l[min] > l[j]:min = j
l[i],l[min] = l[min],l[i]
print ('排列之后:')
for i in range(N):
print (l[i])
"""
利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。
"""
#二维数组不可直接定义。
#二维数组构造:列表的元素是一个个空列表
#Hugboy
arr=[]
sum=0
for r in range(3):
arr.append([])
for c in range(3):
arr[r].append(float(input('input a figure:')))
sum+=arr[r][r]
print(sum)
#Answer
if __name__ == '__main__':
a = []
sum = 0.0
for i in range(3):
a.append([])
for j in range(3):
a[i].append(float(input("input num:\n")))
for i in range(3):
sum += a[i][i]
print (sum)
"""
个数,要求按原来的规律将它插入数组中。
首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。
"""
#1.我看到题,竟然思考 有序列表是高到低/低到高的问题
#2.列表元素个数-列表长度-列表下标 的关系麻烦
#3.别忘了 有序列表可以当条件用 直接判断input_number > for i in (order_arr) 即可
#Hugboy
order_arr=[1,6,15,56,89,102,156,789]
ins = float(input('insert number:'))
if ins>order_arr[-1]:
order_arr.append(ins)
else:
for i in range(len(order_arr)-1):
if ins>order_arr[i] and ins<order_arr[i+1]:
#扩大一个空间
order_arr.append(0)
for k in range(len(order_arr)-1,i,-1):
order_arr[k]=order_arr[k-1]
#print('all_length:%d order_arr[%d]=order_arr[%d]'%(len(order_arr),k,k-1))
order_arr[i+1]=ins
print(order_arr)
#Answer
if __name__ == '__main__':
# 方法一 : 0 作为加入数字的占位符
a = [1,4,6,9,13,16,19,28,40,100,0]
print ('原始列表:')
for i in range(len(a)):
print (a[i])
number = int(input("\n插入一个数字:\n"))
end = a[9]
if number > end:
a[10] = number
else:
for i in range(10):
if a[i] > number:
temp1 = a[i]
a[i] = number
for j in range(i + 1,11):
temp2 = a[j]
a[j] = temp1
temp1 = temp2
break
print ('排序后列表:')
for i in range(11):
print (a[i])
"""
用第一个与最后一个交换。
"""
1.len(i)类似常用的变量,可以定义个变量代替。减小算法复杂度。
2.为自己点赞,第40题马上一半了.
#Hugboy
l = [1,2,3,4,5,6,7,8,9,10]
for i in range(len(l)//2):
l[i],l[len(l)-i-1]=l[len(l)-i-1],l[i]
print(l)
#Answer
if __name__ == '__main__':
a = [9,6,5,4,1]
N = len(a)
print (a)
for i in range(len(a) // 2):
a[i],a[N - i - 1] = a[N - i - 1],a[i]
print (a)
"""
#-*- codeing = utf-8 -*-
#@Time : 2021/3/21 21:18
#@Author : HUGBOY
#@File : 5.py
#@Software: PyCharm
"""
很好奇类里的函数varfunc(),怎么知道传的参数n是什么类型的呢?.
#Answer
class Static:
StaticVar = 5
def varfunc(self):
self.StaticVar += 1
print (self.StaticVar)
print (Static.StaticVar)
a = Static()
for i in range(3):
a.varfunc()
"""
程序分析:没有auto关键字,使用变量作用域来举例吧。
"""
#Hugboy
#Answer
num = 2
def autofunc():
num = 1
print ('internal block num = %d' % num)
num += 1
for i in range(3):
print ('The num = %d' % num)
num += 1
autofunc()
"""
程序分析:演示一个python作用域使用方法
"""
#Hugboy
#Answer
class Num:
nNum = 1
def inc(self):
self.nNum += 1
print ('nNum = %d' % self.nNum)
if __name__ == '__main__':
nNum = 2
inst = Num()
for i in range(3):
nNum += 1
print ('The num = %d' % nNum)
inst.inc()
"""
位置的数据相加,并返回一个新矩阵:
#X = [[12,7,3],
# [4 ,5,6],
# [7 ,8,9]]
#
#Y = [[5,8,1],
# [6,7,3],
# [4,5,9]]
"""
#Hugboy
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
sum=[[],[],[]]
for i in range(3):
for j in range(3):
sum[i].append(X[i][j]+Y[i][j])
print(sum)
#Answer
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
# 迭代输出行
for i in range(len(X)):
# 迭代输出列
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
"""
"""
n=0
for i in range(1,101):
n+=i
print(n)
"""
于 50 则退出。
"""
#Hugboy
def SQ(n):
return n*n
again=1
while again:
n=int(input('input a figure:'))
if SQ(n)<50:
again=0
#Answer
TRUE = 1
FALSE = 0
def SQ(x):
return x * x
print ('如果输入的数字小于 50,程序将停止运行。')
again = 1
while again:
num = int(input('请输入一个数字:'))
print ('运算结果为: %d' % (SQ(num)))
if SQ(num) >= 50:
again = TRUE
else:
again = FALSE
"""
"""
def exchange(x,y):
x,y=y,x
return (x,y)
a=int(input('enter a digit:'))
b=int(input('again:'))
a,b = exchange(a,b)
print('a = {}, b = {}'.format(a,b))
"""
"""
MIXNUM = lambda x,y:(x>y)*x+(x<y)*y
MINNUM = lambda x,y:(x<y)*x+(x>y)*y
if __name__ == '__main__':
x, y = 10, 20
print('The largar one is %d.' % MIXNUM(x, y))
print('The lower one is %d.' % MINNUM(x, y))
"""
"""
import random
print(random.uniform(0,9))
print(random.randint(0,9))
"""
手机扫一扫
移动阅读更方便
你可能感兴趣的文章