pycharm多项目打开 进setting,添加项目所在的文件夹
打开的项目切换快捷键
alt+左右箭头
切换控制台执行脚本快捷键??
没有找到
问题描述
1、数字输出
1,2,3,4四个数字组成多少不重复的三位数,输出它们
for i in range(1,5)
输出1,2,3,4
total=0
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if ((i!=j)and(j!=k)and(k!=i)):
print(i,j,k)
total+=1
print(total)
引申:占位符输出
1.双引号
2.% 10d 代表输出的数据宽度为10,不够补空格
%011d 代表输出的数据宽度为11,不够补0
3.后加%
for s in range(1,5):
print("%10d"%s )
for s in range(1,5):
print("%10d"%s )
print("%011d"%s)
格式化format输出
for s in range(1,5):
t=s/1000
print("%.3f"%t)
# print("%011d"%s)
print("x{0},输出{1:.100f}".format("yz",s))
print(format(t,".2f"))
简便方法
导入itertools 是python的迭代器模块,itertools提供的工具相当高效且节省内存。
import itertools
sum2=0
a=[1,2,3,4]
for i in itertools.permutations(a,3):
print(i)
sum2+=1
print(sum2)
返回可迭代对象的所有数学全排列方式
2、个税计算
企业发放的奖金根据利润提成。利润低于或等于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%提成,从键盘输入当月利润,求应发放奖金总数?
区间计算
for i in range(5)
输出默认0开始到4
python中if不需要( )
python和其他语言一样,数组实际第一位编号为0
python 数组[-1] 表示数组最后一位数字,数组[:-1]从0位到最后一位
数组[::-1]倒序从最后一位到0位
普通解法:按照题目描述一点点写
profit=int(input('输入给公司销售的利润: '))
bonus=0
threshold=[100000,200000,400000,600000,1000000]
rates=[0.1,0.075,0.05,0.03,0.015,0.01]
if profit<=threshold[0]:
bonus=profit*rates[0]
print(bonus)
elif threshold[1]>=profit>threshold[0]:
bonus=threshold[0]*rates[0]+(profit-threshold[0])*rates[1]
print(bonus)
elif threshold[2]>=profit>threshold[1]:
bonus=threshold[0]*rates[0]+threshold[0]*rates[1]+(profit-threshold[1])*rates[2]
print(bonus)
elif threshold[3]>=profit>threshold[2]:
bonus=threshold[0]*rates[0]+threshold[0]*rates[1]+threshold[1]*rates[2]+(profit-threshold[2])*rates[3]
print(bonus)
elif threshold[4]>=profit>threshold[3]:
bonus=threshold[0]*rates[0]+threshold[0]*rates[1]+threshold[1]*rates[2]+threshold[1]*rates[3]+(profit-threshold[3])*rates[4]
print(bonus)
elif profit>threshold[4]:
bonus=threshold[0]*rates[0]+threshold[0]*rates[1]+threshold[1]*rates[2]+threshold[1]*rates[3]+threshold[2]*rates[4]+(profit-threshold[4])*rates[5]
print(bonus)
然后是简便解法,阈值选取价格的差,加起来也是1000000.
profit=int(input('Show me the money: '))
bonus=0
thresholds=[100000,100000,200000,200000,400000]
rates=[0.1,0.075,0.05,0.03,0.015,0.01]
for i in range(len(thresholds)):
if profit<=thresholds[i]:
bonus+=profit*rates[i]
profit=0
break
else:
bonus+=thresholds[i]*rates[i]
profit-=thresholds[i]
bonus+=profit*rates[-1]
print(bonus)
精妙 想不到
3、完全平方数
一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
** 代表乘方
n=0
#找到可能在加上168为另一平方数的值的范围:输出一下
#一共列出84个数,超过84 85**2-84**2=169
while (n+1)**2-n*n<=168:
n+=1
print(n)
print(85**2-84**2)
#然后是遍历这些直到可能的最大值(n+1)**2-1
for i in range((n+1)**2):
if i**0.5==int(i**0.5)and (i+168)**0.5==int((i+168)**0.5):#**0.5 开平方,int取整
print(i-100)#输出原来的值
4、输入年月日,判断是这一年的第几天?
python中含有函数的脚本,有个三角号,可以展开
考虑二月闰年多加一天,设置函数判断闰年,整除4,整除400,
def isLeapYear(y):
return (y%400==0 or (y%4==0 and y%100!=0))
DofM=[0,31,28,31,30,31,30,31,31,30,31,30]
res=0
year=int(input('Year:'))
month=int(input('Month:'))
day=int(input('day:'))
if isLeapYear(year):
DofM[2]+=1
for i in range(month):
res+=DofM[i]
print(res+day)
python一点点的缩进都会错误,说明python要求编程很流程,直接一路自动回车和缩进,python的‘’ “” 都一样,目前看的,但是‘’明显常用 只打一个键,“还需要按shift”
5、初级排序
输入三个整数x,y,z,请把这三个数由小到大输出
排序简介:
按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作。优秀的算法可以节省大量的资源,尤其是在大量数据的处理方面。
- 内部排序和外部排序
- 时间复杂度,空间复杂度
- 稳定排序:如果a原来在b前面,并且a等于b,排序过后,a还是在b前面
- 非稳定排序:如果a原来在b前面,a等于b,排序后a可能不在b前面
- 原地排序:排序过程不申请多余存储空间,只用原来的空间比较和排序
- 非原地排序:需要额外的数组来辅助排序
选择排序
和冒泡排序都是交换式排序,但是选择排序的最好情况和最差情况下,时间复杂度都是O(n^2),空间复杂度为1。
raw=[]
for i in range(3):
x=int(input('int%d: '%(i)))
raw.append(x)
for i in range(len(raw)):#i在0 1 2 循环
for j in range(i,len(raw)):#j在i到2之间遍历 第一个分别与第二个,第三个比,如果大于第二或第三就交换
if raw[i]>raw[j]:
raw[i],raw[j]=raw[j],raw[i]
print(raw)
中间插一段如何输出键盘打进去的字
raw=[]
a=input('随便输入几个数字:')
print(a)
b=a.split(',')
print(b)
for i in range(len(b)):
raw.append(int(b[i]))
print(raw)
print([int(b[i]) for i in range(len(b))])#列表生成式
input把字输进去,split把逗号分隔开,b已经成为列表输出,但是缺点有引号,一种用.append方法用于在列表raw末尾添加字符,这种需要建立一个空列表。另外一种直接用列表生成式输出,运行结果如下:
简便算法:
sorted函数对所有迭代的对象排序,默认升序
输出时调用一下即可。
raw2=[]
for i in range(3):
x=int(input('int%d: '%i))
raw2.append(x)
print(sorted(raw2))
6、斐波那契数列
Fibonacci sequence
从1,1开始,后面每一项等于前面两项之和
f[n]=f[n-1]+f[n-2]
兔子数列:
第一年有一对小兔子,一年后成年。成年的兔子又可以生出一对小兔子,如此循环往复,每年的兔子数就构成了一个斐波那契数列。
注重性能就用循环,简便就用递归实现。
输入n年数,求n年以后兔子的数目。
马甲
用C语言编程,有20级台阶的楼梯,一次可以迈一级或者俩级台阶,那么要爬完此台阶有几种方法?
可以用21的小矩形横着或者竖着去覆盖更大的矩形,请问用n个21的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
target=int(input())
res=0
a,b=1,1
for i in range(target-1):
a,b=b,a+b
print(a)
def Fib(n):
return 1 if n<=2 else Fib(n-1)+Fib(n-2)
print(Fib(int(input())))