咸鱼了一段时间,近期想翻翻身,所以开始练习python100例,顺便做个记录,有需要的自取。
有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
num = 0
for HundredBit in range(1,5):
for TenPlace in range(1,5):
for Bit in range(1,5):
if Bit!=TenPlace and Bit!=HundredBit and TenPlace!=HundredBit:
num += 1
print("第%s个数为:%s"%(num,int(str(HundredBit)+str(TenPlace)+str(Bit))))
print("能组成%s个互不相同且无重复数字的三位数"%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,求应发放奖金总数?
profit = input("Please enter the profit of the enterprise:\n")
profitList = [1000000,600000,400000,200000,100000,0]
rat = [0.01,0.015,0.03,0.05,0.075,0.1]
royalty = 0
if profit.isdigit():
profit = int(profit)
for index in range(len(profitList)):
if profit > profitList[index]:
royalty += (profit - profitList[index]) * rat[index]
profit = profitList[index]
print("royalty is %s"%royalty)
else:
print("Please enter the correct profit.")
一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
小小的分析:
假设该数为 x。 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)
输入某年某月某日,判断这一天是这一年的第几天?
temp = 0
year = int(input("please enter years:"))
month = int(input("please enter months:"))
day = int(input("please enter days:"))
def leapyear(y):
return (y % 4 == 0 and y % 100 != 0 or y % 400 == 0)
month_day = [0,31,28,31,30,31,30,31,31,30,31,30]
if leapyear(year):
month_day[2] += 1
for i in range(month):
temp += month_day[i]
few_day = temp + day
print("%s-%s-%s is the %sth day of %s"%(year,month,day,few_day,year))
import datetime
year = int(input("please enter years:"))
month = int(input("please enter months:"))
day = int(input("please enter days:"))
def fewday(y,m,d):
TargetDay = datetime.date(y,m,d)
InitialDay = datetime.date(y,1,1)
return (TargetDay - InitialDay).days + 1
result = fewday(year,month,day)
print("%s-%s-%s is the %sth day of %s"%(year,month,day,result,year))
输入三个整数x,y,z,请把这三个数由小到大输出。
num_one = int(input("please enter num_one:"))
num_two = int(input("please enter num_two:"))
num_three = int(input("please enter num_three:"))
if num_one >= num_two:
num_one,num_two = num_two,num_one
if num_two >= num_three:
num_two,num_three = num_three,num_two
if num_one >= num_two:
num_one,num_two = num_two,num_one
print(num_one,num_two,num_three)
num_list = []
for index in range(3):
num = int(input("please enter num:"))
num_list.append(num)
num_list.sort()
print(num_list)