目录
前言
xmuoj对应链接
xmuoj | C++与Python语法入门练习(By郭炜老师)http://xmuoj.com/contest/2
Pycharm安装
2020最新版本Pycharm安装、配置和使用指南_哔哩哔哩_bilibilihttps://www.bilibili.com/video/av625815765?from=search&seid=16107377616636361793&spm_id_from=333.337.0.02021年最新Pycharm开发环境搭建 Pycharm学习有这个就够了 Python3.9+Pycharm2021.1_哔哩哔哩_bilibilihttps://www.bilibili.com/video/av501455008?p=2&spm_id_from=pageDriver
代码
001 输出第二个整数
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 12日
"""
s = input()
list1 = s.split(' ')
print(list1[1])
002 字符菱形
用字符串填充无法通过,oj平台文本对照行末没有空格
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 12日
"""
c = input()
for i in [21, 13, 5, 13, 21]:
s1 = ' ' * (i//10)
s2 = c*(i % 10)
print(s1+s2)
003 打印ASCII码
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 24日
"""
c = input()
a = ord(c)
print(a)
004 打印字符
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 24日
"""
a = eval(input())
c = chr(a)
print(c)
005 整型数据类型存储空间大小
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 24日
"""
print("4 1")
006 浮点型数据类型存储空间大小
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
print("4 8")
007 对齐输出
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
s = input()
list1 = s.split(' ')
for i in list1:
print("%8s" % i, end=' ')
'''
1.C语言风格的格式化输出
2.%-8s用于输出左对齐字符(至少为8长度,不够用空格补全)
'''
008 输出保留12位小数的浮点数
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
s = input()
a = eval(s)
# a = round(a,12)
print("%.12f" % a)
'''
1.小数末尾的0会自动省略,round只用于四舍五入
2.pycharm文件重命名
(1)在左侧列表找到要修改的file
(2)右键点击该文件
(3)选择“重构”-“重命名”
'''
009 空格分隔输出
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
for i in range(2):
s = input()
print(s, end=' ')
for i in range(2):
s = input()
a = eval(s)
print("%.6f" % a, end=' ')
010 计算球的体积
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
Pi = 3.14
r = eval(input())
V = 4/3*Pi*pow(r, 3)
print("%.2f" % V)
'''
1.python自带圆周率常数,名字为pi
>>> import math
>>> math.pi
3.141592653589793
>>>
'''
011 大象喝水
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
Pi = 3.14159
s = input()
list1 = s.split(' ')
h = eval(list1[0])
r = eval(list1[1])
cup = Pi * pow(r, 2) * h
cnt = 0
while cnt * cup < 20000:
cnt += 1
print(cnt)
'''
1.pycharm快速标准化代码
(1)把鼠标光标放在报错波浪线上
(2)点击“重新格式化该文件”
'''
012 奇偶数判断
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
a = eval(input())
if a % 2:
print('odd')
else:
print('even')
013 求一元二次方程的根
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
from math import sqrt
s = input()
list1 = s.split(' ')
a = eval(list1[0])
b = eval(list1[1])
c = eval(list1[2])
drt = b*b-4*a*c
if drt >= 0:
x1 = (-b+sqrt(drt))/(2*a)
x2 = (-b-sqrt(drt))/(2*a)
print("x1=%.5f;x2=%.5f" % (x1, x2))
else:
drt = -drt
a1 = -b/(2*a)
b1 = sqrt(drt)/(2*a)
print("x1=%.5f+%.5fi;x2=%.5f-%.5fi" % (a1,b1,a1,b1))
014 点和正方形的关系
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
s = input()
list1 = s.split(' ')
x = eval(list1[0])
y = eval(list1[1])
if -1 <= x <= 1 and -1 <= y <= 1:
print('yes')
else:
print('no')
015 苹果和虫子2
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
list1 = input().split(' ')
n = eval(list1[0])
x = eval(list1[1])
y = eval(list1[2])
if y % x:
half = 1
else:
half = 0
ret = n - half - y // x
print(ret)
016 简单计算器
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
list1 = input().split(' ')
a = eval(list1[0])
b = eval(list1[1])
if list1[2] == '+':
ret = a + b
elif list1[2] == '-':
ret = a - b
elif list1[2] == '*':
ret = a * b
elif list1[2] == '/':
if b == 0:
print('Divided by zero!')
exit(0)
else:
ret = a / b
else:
print('Invalid operator!')
exit(0)
print(ret)
'''
1.python终止程序
exit(0)
'''
017 求整数的和与均值
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
n = eval(input())
sum = 0
for i in range(n):
sum += eval(input())
print("%d %.5f" % (sum, sum/n))
'''
1.python中int为长整型,范围非常大
'''
018 整数序列的元素最大跨度值
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
min = 1000
max = 0
n = eval(input())
list1 = input().split(' ')
for s in list1:
a = eval(s)
if a > max:
max = a
if a < min:
min = a
print(max-min)
019 奥运奖牌计数
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
n = eval(input())
a, b, c = 0, 0, 0
for i in range(n):
list1 = input().split(' ')
list2 = [int(i) for i in list1]
a += list2[0]
b += list2[1]
c += list2[2]
print("%d %d %d %d" % (a, b, c, a+b+c))
'''
1.列表推导式
'''
020 乘方计算
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
list1 = input().split(' ')
list2 = [int(i) for i in list1]
print(list2[0]**list2[1])
'''
1.python中幂运算: a**b
'''
021 鸡尾酒疗法
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
n = eval(input())
list1 = input().split(' ')
list1 = [int(i) for i in list1]
x = list1[1]/list1[0]
for i in range(n-1):
list1 = input().split(' ')
list1 = [int(i) for i in list1]
y = list1[1] / list1[0]
if x-y > 0.05:
print('worse')
elif y-x > 0.05:
print('better')
else:
print('same')
022 角谷猜想
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
n = eval(input())
while n != 1:
if n % 2:
print(n, end='')
n = n*3+1
print("*3+1=%d" % n)
else:
print(n, end='')
n //= 2
print("/2=%d" % n)
print('End')
023 正常血压
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
n = eval(input())
t, ret = 0, 0
for i in range(n):
list1 = input().split(' ')
a, b = eval(list1[0]), eval(list1[1])
if 140 >= a >= 90 >= b >= 60:
t += 1
if t > ret:
ret = t
else:
t = 0
print(ret)
024 数字反转
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
s = input()
if s[0] == '-':
sign = -1
s = s[1:]
else:
sign = 1
s = s[::-1]
ret = sign*int(s)
print(ret)
'''
1.不可用eval(s)
若s=‘038’,则eval(s)=038,为不符合格式的数字,不会自动省略前面的0
'''
025 求特殊自然数
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
def change(n, m):
s = ''
while n > 0:
a = n % m
n //= m
c = str(a)
s += c
return s
for i in range(9 * 9, 7 + 7 * 7 + 7 * 7 * 7+1):
s1, s2 = change(i, 7), change(i, 9)
if s1 == s2[::-1]:
print(i)
print(s2)
print(s1)
break
026 雇佣兵
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
list1 = input().split(' ')
m = eval(list1[0])
n = eval(list1[1])
x = eval(list1[2])
while x*n >= m:
if m % n:
half = 1
else:
half = 0
if x > m//n+half:
x -= m//n+half
else:
break
n += m//n
print(n)
027 数字统计
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
list1 = input().split(' ')
list1 = [int(i) for i in list1]
cnt = 0
for i in range(list1[0], list1[1]+1):
s = str(i)
cnt += s.count('2')
print(cnt)
028 与指定数字相同的数的个数
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
input()
list1 = input().split(' ')
c = input()
print(list1.count(c))
029 陶陶摘苹果
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
list1 = input().split(' ')
list1 = [int(i) for i in list1]
height = eval(input())
cnt = 0
for h in list1:
if height + 30 >= h:
cnt += 1
print(cnt)
030 年龄与疾病
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
n = eval(input())
ages = input().split(' ')
ages = [int(i) for i in ages]
cnt1, cnt2, cnt3, cnt4 = 0, 0, 0, 0
for age in ages:
if 0 < age <= 18:
cnt1 += 1
elif 19 <= age <= 35:
cnt2 += 1
elif 36 <= age <= 60:
cnt3 += 1
else:
cnt4 += 1
print("{:.2%}".format(cnt1/n))
print("{:.2%}".format(cnt2/n))
print("{:.2%}".format(cnt3/n))
print("{:.2%}".format(cnt4/n))
'''
1.python中字符串格式化 百分比输出用%
'''
031 校门外的树
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
list1 = input().split(' ')
list1 = [int(i) for i in list1]
length, n = list1[0], list1[1]
spacel = []
spacer = []
for i in range(n):
list1 = input().split(' ')
list1 = [int(i) for i in list1]
spacel.append(list1[0])
spacer.append(list1[1])
cnt = 0
for i in range(length+1):
p = 1
for j in range(n):
if spacel[j] <= i <= spacer[j]:
p = 0
break
if p:
cnt += 1
print(cnt)
032 计算鞍点
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
nums = [[], [], [], [], []]
for i in range(5):
list1 = input().split()
list1 = [eval(i) for i in list1]
nums[i] = list1
for i in range(5):
for j in range(5):
if nums[i][j] == max(nums[i]):
if nums[i][j] == min(nums[0][j], nums[1][j], nums[2][j], nums[3][j], nums[4][j]):
print('%d %d %d' % (i+1, j+1, nums[i][j]))
exit(0)
print('not found')
'''
1.str.split()用法 默认去除空格(不论个数)
'''
033 图像模糊处理
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
import copy
list1 = input().split()
list1 = [int(i) for i in list1]
n, m = list1[0], list1[1]
nums = []
for i in range(n):
list1 = input().split()
list1 = [int(i) for i in list1]
nums.append(list1)
ret = copy.deepcopy(nums)
for i in range(1, n - 1):
for j in range(1, m - 1):
ret[i][j] = round((nums[i][j] + nums[i - 1][j] + nums[i + 1][j] + nums[i][j - 1] + nums[i][j + 1]) / 5)
for i in range(n):
for j in range(m-1):
print(ret[i][j], end=' ')
print(ret[i][m-1])
'''
1.列表的拷贝(参考C站博客)
https://blog.csdn.net/qq_24502469/article/details/104185122?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164588833716781685399483%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=164588833716781685399483&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-104185122.pc_search_result_control_group&utm_term=python%E5%88%97%E8%A1%A8%E5%A4%8D%E5%88%B6&spm=1018.2226.3001.4187
'''
034 矩阵转置
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
list1 = input().split()
list1 = [int(i) for i in list1]
n, m = list1[0], list1[1]
nums = []
for i in range(n):
list1 = input().split()
list1 = [int(i) for i in list1]
nums.append(list1)
for j in range(m):
for i in range(n-1):
print(nums[i][j], end=' ')
print(nums[n-1][j])
035 Pell数列
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
def pell(k):
a = [1, 2]
for i in range(2, k):
a.append(a[i - 1] * 2 + a[i - 2])
return a[k - 1] % 32767
n = eval(input())
for i in range(n):
k = eval(input())
print(pell(k))
036 求最大公约数问题
# -*- coding:utf-8 -*-
"""
Written by Baiye959
2022年 02月 26日
"""
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)
list1 = input().split()
a, b = eval(list1[0]), eval(list1[1])
if a < b:
t = a
a = b
b = t
print(gcd(a, b))