题目
print("Hello World")
#方法一
name = input()
if len(name)==2:
print("{}同学,人生苦短,我学python".format(name))
print("{}大侠,学好python,走遍天下也不怕".format(name[0]))
print("{}小盆友,学好python,你最帅".format(name[1]))
elif len(name)==3:
print("{}同学,人生苦短,我学python".format(name))
print("{}大侠,学好python,走遍天下也不怕".format(name[0]))
print("{}小盆友,学好python,你最帅".format(name[1:3]))
#方法二
name = input()
print("{}同学,人生苦短,我学python".format(name))
print("{}大侠,学好python,走遍天下也不怕".format(name[0]))
print("{}小盆友,学好python,你最帅".format(name[1: ]))
[1:]表示选择了name[1~n-1]的数据
a=input()
print(a)
a=input()
a=int(a)
b=input()
b=int(b)
print(a+b)
c=input()
c=float(c)
print(c*1.8+32)
# a,b,c=map(int,input().split())
# print(b * b - 4 * a * c)
line=input()
a,b,c=line.split()
a=int(a)
b=int(b)
c=int(c)
print(b * b - 4 * a * c)
a,b,c=map(int,input().split())
p=(a+b+c)/2
p=float(p)
s=(p*(p-a)*(p-c)*(p-b))**0.5
s=float(s)
print("{:.3f}".format(s))
a,b=map(int,input().split())
print("%s*%s=%d"%(a,b,a*b))
一、input().split()用法
input() 一行输入多个需要与split()结合使用
#注意:split()参数为空,默认一行输入多个时用空格隔开
username, passwd = input("请输入用户名,密码:").split()
#注意:input()的返回类型是str,如果是整数需要转化为int才可正常使用
print(username,passwd)
输入样例:
lilian 123456
二、str.split()用法
#str.split(str="", num=string.count(str))
#str是分隔符(默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等),num是分隔次数
txt1 = "Google#Facebook#Runoob#Taobao"
x1 = txt1.split("#", 1)
print(x1)
txt2 = "Google#Facebook#Runoob#Taobao"
x2 = txt2.split("#", 2)
print(x2)
输出结果:
[‘Google’, ‘Facebook#Runoob#Taobao’]
[‘Google’, ‘Facebook’, ‘Runoob#Taobao’]
三、map()用法
map(function, iterable, …)
function – 函数
iterable – 一个或多个序列
返回值:
Python 2.x 返回列表。
Python 3.x 返回迭代器。
所以Python 3.x要加list()函数将迭代器转化为列表。
def f(x):
return x*x
print(list(map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])))
输出结果:
[1, 4, 9, 16, 25, 36, 49, 64, 81]
#用匿名函数
print(list(map(lambda x: x ** 2, [1, 2, 3, 4, 5])))
输出结果:
[1, 4, 9, 16, 25]
print("*********")
print(" *******")
print(" *****")
print(" ***")
print(" *")
print(" ***")
print(" *****")
print(" *******")
print("*********")
#1
s="Everything depends on human effort.\nJust do it."
print(s)
#2
print("Everything depends on human effort.")
print("Just do it.")
#3
print("""Everything depends on human effort.
Just do it.""")
year=input()
mouth=input()
day=input()
print("%s-%s-%s"%(year,mouth,day))
a,b=map(int,input().split())
print("s=%d"%(a-b))
#方法一
h1,m1,h2,m2=map(int,input().split())
if(m1>m2):
h2=h2-1
m=m2-m1+60
h=h2-h1
else:
m=m2-m1
h=h2-h1
print("%s:%s"%(h,m))
#方法二
array = list(input().split())
if (array[1]>array[3]):
array[2]=int(array[2])-1
m = int(array[3]) - int(array[1]) + 60
h = int(array[2]) - int(array[0])
else:
m = int(array[3]) - int(array[1])
h = int(array[2]) - int(array[0])
print("%s:%s"%(h,m),end="")
a=input()
a=int(a)
print(a*a*a)
题目
a,b=map(int,input().split())
print("%d*%d=%d"%(a,b,a*b))
print("@@@@@@@@@@@@@@@@@@@@")
print("@@@@@@@@@@@@@@@@@@@@")
print("@@@@@@@@@@@@@@@@@@@@")
print("@@@@@@@@@@@@@@@@@@@@")
# 输入变量解决问题
f = eval(input())
h = eval(input())
chicken = f / 2 - h
rabbit = 2 * h - f / 2
print("{}".format(int(rabbit)))
print("{}".format(int(chicken)))
# 使用循环解决鸡兔同笼问题
f = eval(input())
h = eval(input())
chicken = 0
while True:
rabbit = h - chicken # 头的总数
if 2 * chicken + 4 * rabbit == f: # 脚的总数
# print('鸡有 {} 只, 兔有 {} 只'.format(chicken, rabbit))
print("{}".format(int(chicken)))
print("{}".format(int(rabbit)))
break
chicken += 1
weight,high=map(float,input().split(","))
# print("{}".format(weight))
# print("{}".format(high))
result = weight / (high * high)
if result < 18:
print("超轻")
elif result < 25:
print("标准")
elif result < 27:
print("超重")
else:
print("肥胖")
name = input()
print("你好,{}同学。".format(name))
print("{}同学,很高兴认识你。".format(name[0]))
print("{}同学,我们交个朋友吧!".format(name[1: ]))
注意python里面import math用法
import math
a=input()
a=float(a)
L=a*math.pi
# L=float(L)
# round(L,2)
print("L={:.2f}".format(L))
# 法1
s = input()
print(s, end="")
print(s, end="")
print(s, end="")
print(s, end="")
print(s, end="")
# python 3.x版本输出不换行格式为:print(x, end="") 。end="" 可使输出不换行。
# 法2
s = input()
print(s * 5, end="")
建议使用方法2
方法一:try-except异常处理 :
详情可看这篇文章 python异常处理try-except语句
方法二:
for line in sys.stdin读文件,按行处理
# 方法1
while True:
try:
s = input()
arr = s.split()
for i in range(0, len(arr)):
arr[i] = arr[i].capitalize()
s1 = " ".join(arr)
print(s1)
except:
break
# 方法2
import sys
for line in sys.stdin:
line = line.strip() # 删除尾部的换行符
print(line.title())
# title函数:python中字符串函数,返回’标题化‘的字符串,就是单词的开头为大写,其余为小写。
s=input()
ans=0
for i in s:
if i == 'p' or i == 'P':
ans+=1
print(ans)
s = input()
# replace(m,n)方法:将字符串里面的m替换为n。
print(s.replace(' ', ''))
# split(s,num)方法:split(s,num)
print(''.join(s.split()))
import math
r=input()
r=float(r)
v=4/3*math.pi*r*r*r
print("v={:.3f}".format(v))
import math
a=input()
a=float(a)
b=input()
b=float(b)
c=math.sqrt(a*a+b*b)
print("对角度长度为:{:.1f}cm.".format(c))
180
300
输出样例:
原有排队游客份数:900.0, 每分钟新到游客份数:3.0, 10口同开需128.6分钟清零待检票游客.
m=int(input())
n=int(input())
x=2*m/(1-m/n)
y=(6*n-x)/n
z=x/(10-y)
print('原有排队游客份数:%.1f, 每分钟新到游客份数:%.1f, 10口同开需%.1f分钟清零待检票游客.'%(x,y,z))
# 方法1
import math
d1 = float(input())
d2 = float(input())
r1 = d1 / 2
r2 = d2 / 2
v1 = 4 / 3 * math.pi * r1 * r1 * r1
v2 = 4 / 3 * math.pi * r2 * r2 * r2
sumV = v1 + v2
print("正方体边长为:{:.2f}.".format(pow(sumV, 1 / 3)))
# 方法2
import math
a = eval(input())
b = eval(input())
c = 4 / 3 * 3.1415
print("正方体边长为:{:.2f}.".format(pow(c * pow((a / 2), 3) + c * pow((b / 2), 3), (1 / 3))))
注意:要判断一下输入的是否有17位
quanzhong = (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2)
# Z: [0,1,2,3,4,5,6,7,8,9,10]
# M: [1,0,X,9,8,7,6,5,4,3,2]
jiaoyanma = ('1','0','X','9','8','7','6','5','4','3','2')
sum = 0
sfz = input()
if sfz[:17].isdigit():
for i in range(17):
sum += int(sfz[i]) * quanzhong[i]
if sfz[-1]==jiaoyanma[sum % 11]:
print("正确")
else:
print("错误")
else:
print("错误")
题目
# 1
print("2018")
print("wo3 men2 yao4 ying2 !")
# 2
print("""2018\nwo3 men2 yao4 ying2 !""")
# 3
print("""2018
wo3 men2 yao4 ying2 !""")
# 1
print("I'm gonna WIN!")
print("I'm gonna WIN!")
print("I'm gonna WIN!")
# 2
print("""I'm gonna WIN!
I'm gonna WIN!
I'm gonna WIN!""")
a,b=map(int,input().split())
# print("a=%d,b=%d"%(b,a))
print("a={},b={}".format(b,a))
import math
r=float(input())
print("{:.6f}".format(r*3.14*r))
x,n=map(float,input().split())
print("y={:.6f}".format(x*n))
# while True:
# try:
# s=input()
# print(s.lower())
# except:
# break
import sys
for line in sys.stdin:
line=line.strip()
print(line.lower())
s1=input()
s2=input()
s=s1+s2
print(s)
【Python】字符串几个常用查找方法
s=input()
str=input()
if str.rfind(s)!=-1:
# print("index = %d"%(str.rfind(s)))
# print("index = {}".format(str.rfind(s)))
print("index = {}".format(str.rindex(s)))
else:
print("Not Found")
s1 = input()
s2 = '' #除去小写字母的字符串
s3 = '' #除去重复字母的字符串
for i in s1:
if ord('A') <= ord(i) <= ord('Z'):
s2 += i
mylist = list(set(s2)) #用set()函数对s2去重,存储为一个列表
#由于set()函数是无顺序去重,应调回原来顺序
mylist.sort(key = s2.index) #用s2的顺序排列列表
# print(s2)
for i in mylist: #将列表的值存为字符串
s3 += i
if s3 != '': #输出
print(s3)
else:
print("Not Found")
# 方法一
s = input()
a = "".join(list(filter(str.isdigit, s)))
print(int(a)) # 注意后面需要以int格式输出,不然就会输出000123等格式。
# 方法二
s = input()
result = list()
M = list(('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'))
for i in range(0, len(s)):
if s[i] in M:
result.append(s[i])
ans = "".join(result)
print(int(ans))
注意后面需要以int格式输出,不然就会输出000123等格式。
6 3
输出样例1:
6 + 3 = 9
6 - 3 = 3
6 * 3 = 18
6 / 3 = 2
输入样例2:
8 6
输出样例2:
8 + 6 = 14
8 - 6 = 2
8 * 6 = 48
8 / 6 = 1.33
# 方法一
a, b = map(float, input().split())
print("{:.0f} + {:.0f} = {:.0f}".format(a, b, a + b))
print("{:.0f} - {:.0f} = {:.0f}".format(a, b, a - b))
print("{:.0f} * {:.0f} = {:.0f}".format(a, b, a * b))
if a % b == 0:
print("{:.0f} / {:.0f} = {:.0f}".format(a, b, a / b))
else:
print("{:.0f} / {:.0f} = {:.2f}".format(a, b, a / b))
# 方法二
a, b = map(float, input().split())
print("%d + %d = %d" % (a, b, a + b))
print("%d - %d = %d" % (a, b, a - b))
print("%d * %d = %d" % (a, b, a * b))
if a % b == 0:
print("%d / %d = %.0f" % (a, b, a / b))
else:
print("%d / %d = %.2f" % (a, b, a / b))
# 方法1
s = input()
ans = list(filter(lambda x: x.isupper() and x not in ('A', 'E', 'I', 'O', 'U'), s))
print(len(ans))
# 方法2
s = input()
yuanyin = ['a', 'e', 'i', 'o', 'u']
ans = 0
for i in s:
if i.lower() not in yuanyin and ord('A') <= ord(i) <= ord('Z'):
ans += 1
print(ans)
# 方法3
s = input()
ans = ""
string = "AEIOU"
for i in range(len(s)):
if s[i] >= 'A' and s[i] <= 'Z' and s[i] not in string:
ans = ans + s[i]
print("{}".format(len(ans)))
s = input()
ans = ""
for i in s:
if ord('A') <= ord(i) <= ord('Z'):
ans += chr( 155 - ord(i) )
else:
ans += i
print(ans)
s = input()
s1 = ''
for ch in s:
if ch != '#':
s1 += ch
else:
break
s_sixteen = ''
index = 0
s_index = -1
for ch in s1:
if ord('0') <= ord(ch) <= ord('9') or ord('a') <= ord(ch.lower()) <= ord('f'):
s_sixteen += ch
if s_index == -1:
s_index = index
index += 1
fuhao = 1
for index in range(s_index):
if s1[index] == '-':
fuhao = -1
break
ans = 0
if s_sixteen != '':
ans = fuhao * int(s_sixteen, 16)
print(ans)
start_time,lost_min = map(int, input().split())
start_h = start_time // 100
start_min = start_time % 100
start_sum = start_h * 60 + start_min
now_sum = start_sum + lost_min
now_h = now_sum // 60
now_min = now_sum % 60
# print("%d%02d" %(now_h,now_min))
print("{:01d}{:02d}".format(now_h,now_min))
Python中的//是向下取整的意思
a//b,应该是对除以b的结果向负无穷方向取整后的数
举例:
5//2=2(2.5向负无穷方向取整为2),同时-5//2=-3(-2.5向负无穷方向取整为-3)
题目
a[::-1]是将数组所有元素逆置
a=input()
b=a[::-1]
if a==b:
print('yes')
else:
print('no')
a,b,c=map(int,input().split())
if a + b <= c or a + c <= b or c + b <= a:
print("NO")
else:
print("YES")
# 直接0.5次方
x = float(input())
ans = float(x ** 0.5)
print("{:.6f}".format(ans))
# 使用指对数袖珍计算器
import math
x = float(input())
if x == 0:
ans = 0
else:
ans = float(math.exp(0.5 * math.log(x)))
if (ans + 1) ** 2 <= x:
ans = ans + 1
print("{:.6f}".format(ans))
# 二分法
x = float(input())
low, high, ans = 0, x, -1
while low <= high:
mid = (low + high) // 2
if mid * mid <= x:
ans = mid
low = mid + 1
else:
high = mid - 1
print("{:.6f}".format(ans))
# 牛顿迭代法
x = float(input())
if x == 0:
ans = 0
else:
C, x0 = float(x), float(x)
while True:
xi = 0.5 * (x0 + C / x0)
if abs(x0 - xi) < 1e-7:
break
x0 = xi
ans = float(x0)
print("{:.6f}".format(ans))
x = float(input())
yes = 1
if 0 < x < 5:
y = x * x + 1
elif x == 0:
y = 0
elif -5 < x < 0:
y = -x + 4
else:
yes = 0
print("No meaning")
if yes == 1:
print("x={:.2f},y={:.2f}".format(x, y))
a, b, c, d = map(int, input().split())
list = [a, b, c, d]
number = ['A', 'B', 'C', 'D']
index = 0
for i in range(3):
if list[i] == list[i + 1]:
index = list[i]
for j in range(4):
if list[j] != index:
print("{} {}".format(number[j], list[j]))
guess = int(input())
if guess>38:
print("Too big!")
elif guess<38:
print("Too small!")
else:
print("Good Guess!")
'''
产生随机数 random.randint(start,end)
可以猜多次,直到才对了位置
如果猜错了给出提示
猜大了还是猜小了
'''
'''
# import random
# ran = random.randint(1,50)
# count = 0
# print('猜数字游戏:')
# while True:
# #进入猜数字环节 while循环
# guess = int(input('请输入您要猜的数字:'))
# count = count + 1
# if guess == ran:
# print('恭喜你猜对了')
# break
# elif guess > ran:
# print('猜大了')
# else :
# print('猜小了')
# print('你一共猜了%d'% count)
'''
while True:
try:
y,m=input().split()
y=int(y)
m=int(m)
list=[31,28,31,30,31,30,31,31,30,31,30,31]
if y%400==0 or y%4 == 0 and y%100!=0:
list[1]=29
print(list[m-1])
except:
break
ABC xyz 123 ?!
输出样例:
abcXYZ
letters:6, digits:3, spaces:3, others:2.
s = input()
num, char, space, other = 0, 0, 0, 0 #分别统计数字、字母、空格、其他字符个数
s1=""
for i in s:
#是否为数字
if i.isdigit():
num += 1
#是否为字母
elif i.isalpha():
char += 1
if "a"<= i <= "z":
s1+=(i.upper())
elif"A" <= i <= "Z" :
s1+=(i.lower())
elif i == ' ':
space += 1
else:
other += 1
print(s1)
print("letters:{}, digits:{}, spaces:{}, others:{}.".format(char, num, space, other))
m, s = map(str, input().split())
m = int(m)
price = 0
# 如果字符是y,说明选择加急;如果字符是n,说明不加急。
if s == "n":
if m <= 1000:
price = 8
if (m > 1000) and ((m % 500) > 0):
a = ((m - 1000) // 500) + 1
a = a * 4
price = 8 + a
if (m > 1000) and ((m % 500) == 0):
a = (m - 1000) // 500
a = a * 4
price = 8 + a
if s == "y":
if m <= 1000:
price = 13
if (m > 1000) and ((m % 500) > 0):
a = ((m - 1000) // 500) + 1
a = a * 4
price = 8 + a + 5
if (m > 1000) and ((m % 500) == 0):
a = (m - 1000) // 500
a = a * 4
price = 8 + a + 5
print(price)
# 方法一
a = input()
a = int(a)
s = []
n = ""
p = w = 1
zero = False
if len(str(a)) <= 9 and a >= 0:
count = len(str(a))
counts = len(str(a))
while a % 10 == 0 and a > 0:
a = a // 10
s = str(a)
for num, i in enumerate(s):
if num < len(s) - 1:
w = list(s)[num]
p = list(s)[num + 1]
if int(w) == 0 and int(p) == 0 and not count == 5:
n += ""
zero = True
elif int(w) == 0 and not int(p) == 0 and zero == False and num < len(s) - 1:
n += "a"
elif count == 5 and int(i) == 0:
n += "W"
elif count == 9 and zero == False:
n += chr(ord(i) + 49) + "Y"
elif count == 5 and zero == False:
n += chr(ord(i) + 49) + "W"
elif (count == 4 or count == 8) and zero == False:
n += chr(ord(i) + 49) + "Q"
elif (count == 3 or count == 7) and zero == False:
n += chr(ord(i) + 49) + "B"
elif (count == 2 or count == 6) and zero == False:
n += chr(ord(i) + 49) + "S"
elif zero:
n += "a"
w = p = 1
zero = False
else:
n += chr(ord(i) + 49)
count -= 1
if counts > 8 and int(list(s)[3]) == 0 and int(list(s)[2]) == 0 and int(list(s)[1]) == 0:
n = n.replace("W", "")
print(n)
'''
# 方法2
def fun(a): # a是一个由整数组成的数组
num = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
flag = 1 # 高位没有零
if len(a) == 4:
if a[0] == '0':
if a[1] == '0' and a[2] == '0' and a[3] == '0':
return ''
else:
qianwei = 'a'
flag = 0 # 高位已经有零
else:
qianwei = num[int(a[0])] + 'Q'
if a[1] == '0' and a[2] == '0' and a[3] == '0':
return qianwei
if a[1] == '0':
if flag == 0:
baiwei = ''
else:
baiwei = 'a'
flag = 0
else:
baiwei = num[int(a[1])] + 'B'
flag = 1
if a[2] == '0' and a[3] == '0':
return qianwei + baiwei
if a[2] == '0':
if flag == 0:
shiwei = ''
else:
shiwei = 'a'
flag = 0
else:
shiwei = num[int(a[2])] + 'S'
flag = 1
if a[2] == '0' and a[3] == '0':
return qianwei + baiwei + shiwei
if a[3] == '0':
gewei = ''
else:
gewei = num[int(a[3])]
flag = 1
return qianwei + baiwei + shiwei + gewei
elif len(a) == 3:
baiwei = num[int(a[0])] + 'B'
if a[1] == '0' and a[2] == '0':
return baiwei
elif a[1] == '0' and a[2] != '0':
return baiwei + 'a' + num[int(a[2])]
elif a[1] != '0' and a[2] == '0':
return baiwei + num[int(a[1])] + 'S'
else:
return baiwei + num[int(a[1])] + 'S' + num[int(a[2])]
elif len(a) == 2:
shiwei = num[int(a[0])] + 'S'
if a[1] == '0':
return shiwei
else:
return shiwei + num[int(a[1])]
else:
return num[int(a[0])]
n = input()
if len(n) <= 4:
print(fun(n))
# elif len(n) > 4 and len(n) <= 8:
elif 4 < len(n) <= 8:
n1 = n[len(n) - 4:]
n2 = n[:len(n) - 4]
print(fun(n2) + 'W' + fun(n1))
else:
n1 = n[len(n) - 4:]
n2 = n[len(n) - 8:len(n) - 4]
n3 = n[:len(n) - 8]
print(fun(n3) + 'Y' + fun(n2) + 'W' + fun(n1))
'''
x = int(input())
y = 0
if x >= 0:
y = x + 3
else:
y = x / 2
print("y={:.6f}".format(y))
import sys
for line in sys.stdin:
a = line.split()
a = [int(line) for line in a]
print(min(a))
a = int(input())
if 0 <= a < 60:
print("-1")
elif 60 <= a < 90:
print("0")
else:
print("1")
请编写程序,输入年份和月份,输出日历。
输入格式
第一行为整数n
后面有n行数据,每行包含两个整数y和m
其中:y为年份(1000≤y≤9999),m为月份(1≤m≤12)
输出格式
这些月的日历(参考输出样例)
输入样例1
1
2017 6
输入样例2
2
2020 5
2020 12
要求:每行末尾不得有多余的空格,日历末尾不得有多余的空行。
提示:公元1年1月1日为星期一。
import calendar
# print(calendar.weekday(1, 1, 1))
# print(calendar.weekday(2017,6,1))
calendar.setfirstweekday(calendar.SUNDAY)
num = int(input())
for i in range(num):
year, month = [int(x) for x in input().split()]
# print(year, month)
print(' ' * 5, end='')
print("%4d年" % year, end='')
print("%2d月"%month)
print()
print("日 一 二 三 四 五 六")
rili = calendar.month(year, month, w=2, l=1)
# print(rili)
rili_lst = rili.split('\n')
# print(rili_lst)
for i in range(2, len(rili_lst)):
if len(rili_lst[i]) > 0:
print(rili_lst[i])
python数组使用
n = input()
arr = input()
num = [int(n) for n in arr.split()]
# print(num)
print(max(num))
a, b, c = map(int, input().split())
flag = 0
for i in range(1, 101):
# print(i)
A = a + i
B = b + i
C = c + i
A %= 10
B %= 10
C %= 10
if A == 0 or B == 0 or C == 0:
continue
if A % 3 == 0 and B % 3 == 0 and C % 3 == 0:
if A != B and B != C and C != A:
flag = i
break
if flag==0:
print("so sad!")
else:
print(flag)
n = float(input())
i = 1
e = 0.0
jc = 1.0
while jc > n:
e = e + jc
j = 1
for k in range(1, i + 1):
j = j * k
jc = 1.0 / j
i += 1
print("e={:.6f}".format(e))
用常规做法,最后一个测试点会超时。
更高效的做法是:找出输出结果的规律,根据输入的n的值直接构造输出结果。列举如下:
n=7,结果是: no no yes no no no yes
n=11,结果是: no no yes no no no yes no no no yes
n=15,结果是: no no yes no no no yes no no no yes no no no yes
n=19的话,结果是多少?
# 6分答案
num = int(input())
flag = 0
for i in range(num):
if i < 2:
print("no ", end="")
elif i >= 2:
if i % 4 == 2:
print("yes ", end="")
else:
print("no ", end="")
# 6分答案 ------时间超时
num = int(input())
for i in range(num):
if i == 2 or (i - 2) % 4 == 0:
print('yes', end=' ')
else:
print('no', end=' ')
用了for循环 时间复杂度O(n)还是会时间超时
#满分答案
Fibonacci = "yes no no no "
num = int(input())
if num == 1:
print("no ", end="")
if num <= 2:
print("no no ", end="")
elif num > 2:
a = (num - 2)
print("no no ", end="")
x = a // 4
if a % 4 == 0:
print(Fibonacci*x)
if a % 4 == 1:
if x == 0:
print(Fibonacci[0:4])
else:
print(Fibonacci*x+Fibonacci[0:4])
if a % 4 == 2:
if x == 0:
print(Fibonacci[0:7])
else:
print(Fibonacci * x+Fibonacci[0:7])
if a % 4 == 3:
if x == 0:
print(Fibonacci[0:10])
else:
print(Fibonacci * x+Fibonacci[0:10])
# num = int(input())
# if num == 1:
# print("no ", end="")
# if num <= 2:
# print("no no ", end="")
# elif num > 2:
# a = (num - 2)
# print("no no ", end="")
# x = a // 4
# if a % 4 == 0:
# print("yes no no no " * x, end="")
# if a % 4 == 1:
# if x == 0:
# print("yes ", end="")
# else:
# print("yes no no no " * x, end="")
# print("yes ", end="")
# if a % 4 == 2:
# if x == 0:
# print("yes no ", end="")
# else:
# print("yes no no no " * x, end="")
# print("yes no ", end="")
# if a % 4 == 3:
# if x == 0:
# print("yes no no ", end="")
# else:
# print("yes no no no " * x, end="")
# print("yes no no ", end="")
# 方法2
n=input()
n=int(n)
a='no no yes no no no yes no '
b=[1,2,0,2,2,1,0,1]
x=n//8
def f(m):
for i in range(m):
if (b[i % 8]) == 0:
print("yes ", end="")
else:
print("no ", end="")
if x<0:
f(x)
else:
print(a*x,end='')
f(n%8)
n = int(input())
ans = 0
flag = 1
num = 1
for i in range(1, n + 1):
ans += flag * 1.0 / num
num += 3
flag = -flag
print("sum = {:.3f}".format(ans))
a = input().split() #在一行中输入多个数
i = int(a[0]) + 1 #确定第一个数字大小即是列表应有的长度
del a[i:] #删除多余输入的数字
del a[0] #删除第一个数字
#现在a中存储的即为要求序列
num = 0
MaxCount = 0
for i in a: #从第一个数字开始遍历(第一个循环)
count = 0
for j in a: #从第一个数字开始遍历(第二个循环)
if j == i: #如果两个数字相等,那么这个数字的数量加一
count += 1
if MaxCount < count: #判断数量最多的数字并存储
MaxCount = count
num = i
print("{} {}".format(num,MaxCount))
# print(num, end = ' ')
# print(MaxCount)
a = int(input())
n = 0
b = a + 4
for i in range(a, b):
for j in range(a, b):
if i == j:
continue
for k in range(a, b):
if k == i or k == j:
continue
n += 1
# 换行
if n < 6:
print(i * 100 + j * 10 + k, end=" ")
else:
n = 0
print(i * 100 + j * 10 + k)
也可以使用暴力解法 一共6种情况,直接输出
a = int(input())
if a==1:
print("""123 124 132 134 142 143
213 214 231 234 241 243
312 314 321 324 341 342
412 413 421 423 431 432""")
elif a==2:
print("""234 235 243 245 253 254
324 325 342 345 352 354
423 425 432 435 452 453
523 524 532 534 542 543""")
elif a==3:
print("""345 346 354 356 364 365
435 436 453 456 463 465
534 536 543 546 563 564
634 635 643 645 653 654""")
elif a==4:
print("""456 457 465 467 475 476
546 547 564 567 574 576
645 647 654 657 674 675
745 746 754 756 764 765""")
elif a==5:
print("""567 568 576 578 586 587
657 658 675 678 685 687
756 758 765 768 785 786
856 857 865 867 875 876
""")
elif a==6:
print("""678 679 687 689 697 698
768 769 786 789 796 798
867 869 876 879 896 897
967 968 976 978 986 987""")
n = int(input())
for i in range(1, n + 1):
for j in range(1, n - i + 1):
print(" ", end="")
for j in range(1, 2 * i - 1 + 1):
print("*", end="")
print("\n", end="")
for i in range(1, n - 1 + 1):
for j in range(1, i + 1):
print(" ", end="")
for j in range(1, 2 * n - 1 - 2 * i + 1):
print("*", end="")
print("\n", end="")
import math
n = int(input())
arr = input()
num = [int(n) for n in arr.split()]
sum = 0
avg = 0.0
x = 0.0
# for i in num:
# print(i)
for i in range(0, len(num)):
# print(num[i])
sum += num[i]
avg = sum * 1.0 / n
for i in range(0, len(num)):
x += (num[i] - avg) * (num[i] - avg)
result = math.sqrt(x / n)
print("{:.5f}".format(result))
不知道为什么 但是是错误的
# 0
a = [int(i) for i in input().split()]
n = a[0]
result = 0
for i in range(1, n + 1):
for j in range(1, n + 1):
if j==i:
continue
else:
result += a[i] * 10 +a[j]
print(result)
# 0
a = input().split()
sum = 0
sum2 = 0
for i in range(1, len(a)):
sum += int(a[i])
for i in range(1, len(a)):
sum2 += int(a[i]) * 10 * (len(a) - 2) + sum - int(a[i])
print(sum2)
终于答对了
# 10
import sys
num = list(map(int, sys.stdin.read().split()))
print(sum(num[1:] * (num[0] - 1) * 11))
第10题,输入有两行。输入样例改为:
3
2 5 8
# 10
n = int(input())
a = [int(i) for i in input().split()]
result = 0
for i in range(0, n ):
for j in range(0, n ):
if j==i:
continue
else:
result += a[i] * 10 +a[j]
print(result)
# 方法1
def isprime(n):
if n <= 1:
return False
for i in range(2, n):
if n % i == 0:
return False
return True
def f(n):
count = 0
s = 0
while count < 10 and n > 1:
if isprime(n):
count += 1
print(n, end=" ")
s += n
n -= 1
print()
return s
num = int(input())
print("sum = {}".format(f(num)))
# 方法2
import heapq
n = int(input())
sums = 0
nums = []
for i in range(2, n):
for j in range(2, i):
if i % j == 0:
break
else:
sums += i
nums.append(int(i))
maxnums = heapq.nlargest(10, nums)
total = sum(maxnums)
for i in maxnums:
print(i, end=' ')
print()
print("sum = {:.0f}".format(total))
count = 0
t = 0
n = int(input())
aa = {}
bb = {}
for a1 in range(1, 7):
for a2 in range(1, 7):
for a3 in range(1, 7):
for b1 in range(1, 7):
for b2 in range(1, 7):
for b3 in range(1, 7):
if ((a1 * 100 + a2 * 10 + a3 - b1 * 100 - b2 * 10 - b3 == n)
and a1 != a2 and a1 != a3 and a1 != b1 and a1 != b2
and a1 != b3 and a2 != a3 and a2 != b1 and a2 != b2
and a2 != b3 and a3 != b1 and a3 != b2 and a3 != b3
and b1 != b2 and b1 != b3 and b2 != b3):
aa[count] = a1 * 100 + a2 * 10 + a3
bb[count] = b1 * 100 + b2 * 10 + b3
count += 1
print(count)
for i in range(0, count):
print("{}-{}".format(aa[i], bb[i]))
# 9
dis = [] # 距离
height = [] # 高度
h, time = map(int, input().split())
for i in range(1, time + 1):
if i == 1:
dis.append(h)
else:
dis.append(2 * h)
h /= 2
height.append(h)
print("{:.1f} {:.1f}".format(sum(dis), height[-1]))
这里9分是因为:非零返回了
# 10
height, n = map(int, input().split())
sum = 0
h = 0
for i in range(0, n + 1):
if i == 0:
sum = 0
h = 0
if i == 1:
sum = height
h = height / 2.0
if i > 1:
sum = sum + h * 2
h = h / 2
print("{:.1f} {:.1f}".format(sum, h))
# 方法1
import math
a = list(float(x) for x in input().split())
b = list(float(x) for x in input().split())
while True:
if b[1] - b[0] <= 0.01:
break
f1 = a[0] * pow(b[0], 3) + a[1] * pow(b[0], 2) + a[2] * b[0] + a[3]
f2 = a[0] * pow(b[1], 3) + a[1] * pow(b[1], 2) + a[2] * b[1] + a[3]
if f1 == 0:
b[1] = b[0]
break
if f2 == 0:
b[0] = b[1]
break
if f1 * f2 < 0:
ans = (b[0] + b[1]) / 2
f3 = a[0] * pow(ans, 3) + a[1] * pow(ans, 2) + a[2] * ans + a[3]
if f3 == 0:
break
elif (f3 < 0 and f1 < 0) or (f3 > 0 and f1 > 0):
b[0] = ans
elif (f3 < 0 and f2 < 0) or (f3 > 0 and f2 > 0):
b[1] = ans
ans = (b[0] + b[1]) / 2
print("%.2f" % ans)
# 方法2
a3, a2, a1, a0 = map(float, input().split())
a, b = map(float, input().split())
def f(x):
res = a3 * x ** 3 + a2 * x ** 2 + a1 * x + a0
return res
# 区间端点是根
if f(a) == 0:
ans = a
elif f(b) == 0:
ans = b
else: # 判断f(a)f(b)的符号
while b - a >= 0.01:
if f((a + b) / 2) == 0:
ans = (a + b) / 2
break
elif f((a + b) / 2) * f(a) > 0:
a = (a + b) / 2
else:
b = (a + b) / 2
else:
ans = (a + b) / 2
print("%.2f" % ans)
N = int(input()) # 参赛的高校数 N (不超过100的正整数)
a = [int(i) for i in input().split()] # 第二行给出 N 个不超过10的正整数,其中第 i 个数对应第 i 所高校的参赛队伍数
b = sorted(a) # 将队伍数量由小到大排列
for i in range(N):
if N == 1:
print('#1')
start = 1
for i in range(a[0]):
print(*range(start, start + 19, 2))
start += 20
break
d_id = i + 1
x = N
num = 0
finishSchoolNum = 0
d1 = a[:i]
d2 = a[i + 1:]
jud = 0
print('#{}'.format(d_id))
for j in range(1, a[i] + 1):
if j > b[finishSchoolNum]:
if b[finishSchoolNum] in d1:
d_id -= d1.count(b[finishSchoolNum])
x -= b.count(b[finishSchoolNum])
finishSchoolNum += b.count(b[finishSchoolNum])
if x == 1:
x = 2
if b[-2] not in d2:
jud = 1
print(*range(num + d_id + jud, num + d_id + jud + x * 9 + 1, x))
num += x * 10
grade = list(map(int, input().split(" ")))
people = 0
sum = 0
n = 0
for i in grade:
if i < 0:
break
n += 1
sum += i
if i < 60:
people += 1
if n > 0:
average = "%.2f" % (sum / n)
print('Average =', average)
print('Count =', people)
else:
print("Average = 0.00")