(1) 有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去 掉不满足条件的排列。
简单解法
count = 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(int("{}{}{}".format(i, j, k)))
count += 1
print("一共有{}种".format(count))
复杂解法1
def dg(arr):
ls = []
for i in arr:
# tmp_ls = []
if len(ls) == 0:
ls.append([i])
else:
arr_copy = ls.copy()
ls.clear()
for j in arr_copy:
j_l = len(j) + 1
for k in range(j_l):
j_copy = j.copy()
j_copy.insert(k, i)
ls.append(j_copy)
return ls
# 共有四个数
arr = ["1", "2", "3", "4"]
# 组成三位
num = 3
#计数器
count = 0
for i in arr:
arr_copy = arr.copy()
arr_copy.remove(i)
for j in dg(arr_copy):
print(int(''.join(j)))
count += 1
print("一共有{}种".format(count))
复杂解法2
#从数组中任选出num的个元素的组合
def select(arr, num):
l = pow(2, len(arr))
for i in range(1, l):
s = bin(i)
if num == -1 or s.count("1") == num:
s = s[2:].zfill(4)
sum = ""
for i in range(len(s)):
if s[i] == "1":
sum += arr[i]
yield sum
def dg(arr):
ls = []
for i in arr:
if len(ls) == 0:
ls.append([i])
else:
arr_copy = ls.copy()
ls.clear()
for j in arr_copy:
j_l = len(j) + 1
for k in range(j_l):
j_copy = j.copy()
j_copy.insert(k, i)
ls.append(j_copy)
return ls
arr = ["1", "2", "3", "4"]
# 组成三位
num = 3
# 计数器
count = 0
for arr_item in select(arr, num):
for j in dg(arr_item):
print(int(''.join(j)))
count += 1
print("一共有{}种".format(count))
结果展示:
432
342
324
423
243
234
431
341
314
413
143
134
421
241
214
412
142
124
321
231
213
312
132
123
一共有24种
(2) 企业发放的奖金根据利润提成
- 利润(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 = int(input("你的净利润是:"))
bonus1 = 100000 * 0.1
bonus2 = bonus1 + 100000 * 0.075
bonus4 = bonus2 + 200000 * 0.05
bonus6 = bonus4 + 200000 * 0.03
bonus10 = bonus6 + 400000 * 0.015
if i <= 100000:
bonus = i * 0.1
elif i <= 200000:
bonus = bonus1 + (i - 100000) * 0.075
elif i <= 400000:
bonus = bonus2 + (i - 200000) * 0.05
elif i <= 600000:
bonus = bonus4 + (i - 400000) * 0.03
elif i <= 1000000:
bonus = bonus6 + (i - 600000) * 0.015
elif i > 1000000:
bonus = bonus10 + (i - 1000000) * 0.01
print("提成为:bonus={}".format(bonus))
复杂解法1
levels = [0.1, 0.075, 0.05, 0.03, 0.015, 0.01]
max_values = [100000, 100000, 200000, 200000, 400000]
def measure_bonus(bonus, lvl):
max_value = max_values[lvl]
rate = levels[lvl]
if bonus <= max_value:
b = bonus * rate
else:
b = measure_bonus(max_value, lvl) + eval(
"measure_bonus_{}({})".format(lvl + 1, bonus - max_value))
return b
def measure_bonus_0(bonus):
lvl = 0
return measure_bonus(bonus, lvl)
def measure_bonus_1(bonus):
lvl = 1
return measure_bonus(bonus, lvl)
def measure_bonus_2(bonus):
lvl = 2
return measure_bonus(bonus, lvl)
def measure_bonus_3(bonus):
lvl = 3
return measure_bonus(bonus, lvl)
def measure_bonus_4(bonus):
lvl = 4
return measure_bonus(bonus, lvl)
def measure_bonus_5(bonus):
lvl = 5
rate = levels[lvl]
return bonus * rate
bonus = int(input("你的净利润是:"))
print("提成为:bonus={}".format(measure_bonus_0(bonus)))
复杂解法2
levels = [0.1, 0.075, 0.05, 0.03, 0.015, 0.01]
max_values = [100000, 100000, 200000, 200000, 400000]
def measure_bonus(bonus, lvl=0):
rate = levels[lvl]
if lvl == 5:
b = bonus * rate
else:
max_value = max_values[lvl]
if bonus <= max_value:
b = bonus * rate
else:
b = measure_bonus(max_value, lvl) + measure_bonus(bonus - max_value, lvl + 1)
return b
bonus = int(input("你的净利润是:"))
print("提成为:bonus={}".format(measure_bonus(bonus)))
(9) 输出9*9口诀
分行与列考虑,共 9 行 9 列,i 控制行,j 控制列
for i in range(1, 10):
for j in range(1, i + 1):
print("{}*{}={}".format(i, j, i * j), end=" ")
print()
结果展示:
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
问题来源:
C语言百例