a1,a2,b1,b2=map(int,input().split())
ss=0
sy=0if abs(a1-b1)==18:
ss=18+a2-1+b2-1
sy=18+19-a2+19-b2
if abs(a2-b2)==18:
ss=18+a1-1+b1-1
sy=18+19-a1+19-b1
if a1==b1:
ss=abs(a2-b2)
if a2==b2:
ss=abs(a1-b1)
if ss+sy==0:
ss=abs(a1-b1)+abs(a2-b2)
if ss>sy and sy!=0:
print(sy)
else:
print(ss)
章节4 循环语句for
编程求1+3+5+…+n
Python
n=int(input())
ans=0for i in range(1,n+1,2):
ans+=i
print(ans)
编程求解2+4+6+…+n
Python
n=int(input())
ans=0for i in range(2,n+1,2):
ans+=i
print(ans)
求100+97+……+4+1的值。
Python
ans=0for i in range(1,101,3):
ans+=i
print(ans)
分别求奇数和偶数和
Python
n=int(input())
a=[0,0]
for i in range(1,n+1):
x=int(input())
a[x%2]+=x
print(a[1])
print(a[0])
判奇偶求和
Python
n=int(input())
ans=0if n%2==1:
for i in range(2,n+1,2):
ans+=i
else:
for i in range(1,n+1):
if n%i==0:
ans+=i
print(ans)
编程求1+1/2+1/3+…+1/n
Python
n=int(input())
ans=0.0for i in range(1,n+1):
ans+=1/i
print("%.3f"%(ans))
求函数值
Python
import math
n,x=map(int,input().split())
ans=0.0
ans+=x
for i in range(1,n+1):
ans=math.sqrt(i+ans)
print("%.3f"%(ans))
同因查找
Python
for i in range(20,1001):
if i%2==0 and i%3==0 and i%7==0:
print(i)
求满足条件的整数个数
Python
n=int(input())
ans=0for i in range(1,n+1):
if i%3==2 and i%5==3 and i%7==2:
ans+=1print(ans)
打折优惠
Python
n=int(input())
ans=0.0
a=[int(i) for i in input().split()]
for i in a:ans+=i
if ans>100:
ans=100+0.9*(ans-100)
print("%.2f"%(ans))
选裙子
Python
n=int(input())
ans=0for i in input().split():ans+=int(i)
print((ans-1)%n+1)
自由落体
Python
m,n=map(int,input().split())
h=float(m)
dis=0.0for i in range(1,n+1):
dis+=h
h/=2
dis+=h
dis-=h
print("%.2f %.2f"%(h,dis))
咪咪的智慧
Python
n,t=map(int,input().split())
a=[int(i) for i in input().split()]
ans=0for i in a:
if i<t:
ans+=1print(ans)
寻找雷劈数
Python
for i in range(1000,10000):
a=i//100
b=i%100if (a+b)**2==i:
print(i)
判断素数
Python
import math
x=int(input())
if x<=1:
print("F")
exit(0)
for i in range(2,int(math.sqrt(x))+1):
if x%i==0:
print("F")
exit(0)
print("T")
二维矩阵数字的和
Python
n=int(input())
ans=0for i in range(1,n+1):
ans+=i*(2*i-1)
print(ans)
章节5 循环语句while
编程求1+3+5+…+n
Python
n=int(input())
ans=0for i in range(1,n+1,2):
ans+=i
print(ans)
编程求解2+4+6+…+n
Python
n=int(input())
ans=0for i in range(2,n+1,2):
ans+=i
print(ans)
角谷猜想
Python
n=int(input())
cnt=0while n!=1:
if n%2==0:n//=2else:n=n*3+1
cnt+=1print(cnt)
#print(173)for i in range(100,200):
if i%3==2 and i%5==3 and i%7==5:
print(i)
exit(0)
统计大写英文字母的个数
Python
x=input()
ans=0for i in x:
if 'A'<=i and i<='Z':
ans+=1print(ans)
统计字符的个数
Python
x=input()
cnt1=cnt2=cnt3=0for i in x:
if 'A'<=i and i<='Z':
cnt1+=1elif 'a'<=i and i<='z':
cnt2+=1elif '0'<=i and i<='9':
cnt3+=1print(cnt1,cnt2,cnt3)
数列求和2
Python
def p(x):
if x>=0:
print("%d+"%(x),end='')
else:
print("(%d)+"%(x),end='')
x1,x2,x3,xn=map(int,input().split())
d=x2-x1
ans=(x1+xn)*((xn-x1)//d+1)//2
p(x1)
p(x2)
p(x3)
if x3+d!=xn:
print("...+",end='')
if xn>=0:
print("%d"%(xn),end='')
else:
print("(%d)"%(xn),end='')
print("=%d"%(ans))
章节6 列表list
序列 统计
Python
n=int(input())
a=[]
for i in range(0,n):
x=int(input())
a.append(x)
m=int(input())
ans=0for i in a:
if i==m:
ans+=1print(ans)
序列 求平均年龄
Python
n=int(input())
s=0for i in range(0,n):
x=int(input())
s+=x
print("%.2f"%(s/n))
序列 逆序
Python
m=int(input())
a=[int(i) for i in input().split()]
a.reverse()
for i in a:
print(i,end=' ')
序列 输出奇数和偶数
Python
n=int(input())
a=[int(i) for i in input().split()]
odd=[]
even=[]
for i in a:
if i%2==0: even.append(i)
else: odd.append(i)
for i in odd:print(i,end=' ')
print()
for i in even:print(i,end=' ')
序列 最大数的位置
Python
n=int(input())
a=[int(i) for i in input().split()]
maxv=0for i in a:maxv=max(maxv,i)
for i in range(0,n):
if a[i]==maxv:
print(i+1)
序列 找找谁的身高超过全家的平均身高
Python
n=int(input())
a = [int(x) for x in input().split()]
ans=0.0for i in a:
ans+=i
ans=ans/n
print("Aver=%0.1f"%(ans))
for i in range(0,n):
if (a[i]>=ans):
print("%d:%d"%(i+1,a[i]),end=' ')
列表元素的移动
Python
n=int(input())
a = [int(i) for i in input().split()]
b=[]
x=int(input())
if (x>1):
b+=(a[0:x-1])
if (x<n):
b+=(a[x:])
b.append(a[x-1])
for i in b:
print(i,end=' ')
列表元素的插入
Python
n=int(input())
a=[int(i) for i in input().split()]
index=int(input())
obj=int(input())
a.insert(index-1,obj)
for i in a:
print(i,end=' ')
列表元素的删除
Python
n=int(input())
a=[int(i) for i in input().split()]
x=int(input())
for i in range(0,n):
if (i!=x-1):
print(a[i],end=' ')
列表 Fish学数学
Python
n=int(input())
a=[]
for i in range(1,n+1):
a.append(int(input()))
ans=0for i in range(0,n):
for j in range(i+1,n):
if (a[j]<a[i]):
ans+=1print(ans)
最大最小
Python
def s(x):
ret=0while (x):
ret+=x%10
x//=10return ret
l=int(input())
d=int(input())
x=int(input())
minv=10010
maxv=0for i in range(l,d+1):
if (s(i)==x):
minv=min(minv,i)
maxv=max(maxv,i)
print(minv)
print(maxv)
列表 倒数第二小整数
Python
n=int(input())
a=[int(i) for i in input().split()]
a.sort()
i=len(a)-1while (a[i]==a[i-1]):
i-=1print(a[i-1])
下载电影
Python
n,m=map(int,input().split())
a=[int(i) for i in input().split()]
s=0
ans=0.0for i in range(0,m):
s+=a[i]
ans=max(ans,s/m)
for i in range(m,n):
s=s+a[i]-a[i-m]
ans=max(ans,s/m)
print("%0.2f"%(ans))
赛马
Python
n=int(input())
a1=[int(i) for i in input().split()]
b1=[int(i) for i in input().split()]
x=0
y=0
z=0
a2=[int(i) for i in input().split()]
b2=[int(i) for i in input().split()]
for i in range(0,n):
a=a1[a2[i]-1]
b=b1[b2[i]-1]
if a>b:x+=1elif a==b:y+=1else: z+=1print(x,y,z)
草料开支
Python
n,q=map(int,input().split())
a=[0]*(n+1)
for i in range(1,n+1):
x=int(input())
a[i]=a[i-1]+x;for i in range(0,q):
s,e=map(int,input().split())
print(a[e]-a[s-1])
奶牛式乘法
Python
a,b=map(int,input().split())
ans=0
tmpa=a
while tmpa:
x=tmpa%10
tmpb=b
while tmpb:
y=tmpb%10
ans+=x*y
tmpb//=10
tmpa//=10print(ans)
完全图
Python
import math
def dis(x1,y1,x2,y2):
return math.sqrt((x2-x1)**2+(y2-y1)**2)
n=int(input())
x=[]
y=[]
for i in range(0,n):
xx,yy=map(int,input().split())
x.append(xx)
y.append(yy)
ans=0.0for i in range(0,n-1):
for j in range(i+1,n):
ans+=dis(x[i],y[i],x[j],y[j])
print("%.2f"%(ans))
前缀和
Python
n=int(input())
a=list(map(int,input().split()))
s=[0]
for i in range(0,n):
s.append(s[i]+a[i])
q=int(input())
for i in range(1,q+1):
l,r=map(int,input().split())
print(s[r]-s[l-1])
一年中的第几天
Python
def leap(year):
if (year%400==0):return 1if (year%100==0):return 0return year%4==0
a=[0,31,0,31,30,31,30,31,31,30,31,30,31]
y,m,d=map(int,input().split("-"))
if (leap(y)):a[2]=29else:a[2]=28
ans=d
for i in range(1,m):
ans+=a[i]
print(ans)
二维矩阵数字的和
Python
n=int(input())
ans=0for i in range(1,n+1):
ans+=i*(2*i-1)
print(ans)
求N个整数的平均数、众数和中位数
Python
n=int(input())
a=[int(i) for i in input().split()]
a=[0]+a
s=0for i in a:s+=i
print("%.2f"%(s/n),end=' ')
m={}
maxv=0
pos=0for i in a:
m[i]=m.get(i,0)+1if m[i]>maxv:
maxv=m[i]
pos=i
print(pos,end=' ')
a.sort()
if n%2==0:
print("%.1f"%((a[n//2]+a[n//2+1])/2))
else:
print("%.1f"%(a[n//2+1]))
章节7 字符串
统计数字字符个数
Python
s=input()
ans=0for i in s:
if i>='0' and i<='9':
ans+=1print(ans)
s=input()
ret=""for x in s:
if ord(x)>=ord('A') and ord(x)<=ord('Z'):
ret=ret+chr(ord(x)-ord('A')+ord('a'))
elif ord(x)>=ord('a') and ord(x)<=ord('z'):
ret=ret+chr(ord(x)-ord('a')+ord('A'))
else:
ret+=x
print(ret)
扫描识别
Python
s=input()
ret=""for x in s:
d=ord(x)
if d==ord('O'):ret+='0'elif d==ord('l'):ret+='1'elif d==ord('Z'):ret+='2'elif d==ord('S'):ret+='5'elif d==ord('b'):ret+='6'elif d==ord('B'):ret+='8'elif d==ord('q'):ret+='9'else:ret+=x
print(ret)
电报文字
Python
s=input()
ss=""for x in s:
if x.islower():
ss+=chr((ord(x)-ord('a')+1)%26+ord('a'))
else:
ss+=x
print(ss)
字符串压缩
Python
s=input()
n=len(s)
s+=" "
ss=""
i=0while i<n:
j=i
while s[j+1]==s[j]:j+=1
num=j-i+1if num==1:ss+=s[j]
else:
while num:
ss+=chr(num+ord('0'))
num//=10
ss+=s[j]
i=j+1print(ss)
破译邮件
Python
def deal(s):
i=0while i<len(s):
if s[i]=='-':i+=1elif s[i]=='#':
print(" ",end='')
i+=1elif s[i].isdigit():
tmp=0while i<len(s) and s[i].isdigit():
tmp=tmp*10+ord(s[i])-ord('0')
i+=1
tmp-=1print(chr(tmp+ord('A')),end='')
print()
n=int(input())
for _ in range(0,n):
s=input()
deal(s)
判断是否构成回文
Python
s=input()
s=s[0:(len(s)-1)]
ss=reversed(list(s))
if list(s)==list(ss):
print("TRUE")
else:
print("FALSE")
砳砳的图形
Python
s=[]
cnt=[]
for i in range(4):
s.append(input())
for i in range(30):
cnt.append(0)
for i in range(4):
for j in s[i]:
if not j.isupper():
continue
cnt[ord(j)-ord('A')]+=1for i in range(max(cnt),0,-1):
__buff=''for j in range(26):
if cnt[j]>=i:
print(__buff,end='')
print('*',end='')
__buff=''else:
__buff+=' '
__buff+=' 'print()
for i in range(26):
print(chr(i+ord('A')),end='')
if i!=25:
print(' ',end='')
奇偶位互换
Python
n=int(input())
for _ in range(0,n):
s=input()
ss=""for i in range(0,len(s)):
ss+=s[i^1]
print(ss)
章节8 字典dict
统计数字字符个数
Python
s=input()
ans=0for i in s:
if i>='0' and i<='9':
ans+=1print(ans)
火柴棒等式(简版)
Python
s=input()
d={'0':6,'1':2,'2':5,'3':5,'4':4,'5':5,'6':6,'7':3,'8':7,'9':6,'+':2,'=':2}
ans=0for i in s:
ans+=d[i]
print(ans)
扫描识别
Python
s=input()
ret=""for x in s:
d=ord(x)
if d==ord('O'):ret+='0'elif d==ord('l'):ret+='1'elif d==ord('Z'):ret+='2'elif d==ord('S'):ret+='5'elif d==ord('b'):ret+='6'elif d==ord('B'):ret+='8'elif d==ord('q'):ret+='9'else:ret+=x
print(ret)
小 X 与机器人 2 (sihuo)
Python
x,y=list(),list()
s1,s2,t=int(),int(),int()
x.append(-1000)
y.append(-1000)
for i in range(1,5):
a,b=map(int,input().split())
x.append(a)
y.append(b)
x.sort()
y.sort()
t=1if ((x[1]==x[2])and(x[3]==x[2])and(x[3]==x[4])or(y[1]==y[2])and(y[3]==y[2])and(y[3]==y[4])):
print('zhisi')
exit(0)
for i in range(1,4):
for j in range(i+1,5):
if (x[i]==x[j]):
s1+=1;if (y[i]==y[j]):
s2+=1;if ((s1==2)and(s2==2)):
print('fangsi')
exit(0)
if(s1==3):
for i in range(1,4):
if (y[i]==y[i+1]):
if (i==2):
print('dingsi')
exit(0)
else:
print('qusi')
exit(0)
if (s2==3):
for i in range(1,4):
if (x[i]==x[i+1]):
if (i==2):
print('dingsi')
exit(0)
else:
print('qusi')
exit(0)
if ((s1==1)or(s2==1)):
print('wansi')
章节9 自定义函数
求正整数2和n之间的完全数
Python
def perfect(x):
ans=1for i in range(2, x//2+1):
if x % i == 0:
ans += i
return ans == x
n= int(input())
for i in range(2, n+1):
if perfect(i):
print(i)
素数个数
Python
p = [0]*50010
n = int(input())
ans = 0for i in range(2,n+1):
if not p[i]:
ans += 1for j in range(i,n//i+1):
p[i * j] = 1print(ans)
def rev(x):
return x//10+x%10*10def prime(x):
for i in range(2,x):
if x%i==0:
return 0return 1for i in range(10,100):
if prime(i) and prime(rev(i)):
print(i)
回文三位素数
Python
def pal(x):
tmp=x
xx=0while tmp:
xx=xx*10+tmp%10
tmp//=10return xx==x
def prime(x):
for i in range(2,x):
if x%i==0:
return 0return 1for i in range(100,1000):
if pal(i) and prime(i):
print(i)
import math
n=int(input())
ans=0for i in range(0,n):
x=int(input())
ans+=math.ceil(x/70)
print(ans/10)
甲流病人初筛
Python
def isill(s,t,b):
if t>=37.5 and b:
print(s)
return 1return 0
n=int(input())
ans=0for i in range(0,n):
inp=input().split()
ans+=isill(inp[0],float(inp[1]),int(inp[2]))
print(ans)
两个自然数M和N的最小公倍数。
Python
def gcd(x,y):
if y==0: return x
else: return gcd(y,x%y)
n,m=map(int,input().split())
print(n*m//gcd(n,m))
输出m和n范围内的完全数(完美数)
Python
a=[6,28,496,8128]
m,n=map(int,input().split())
for i in a:
if m<=i and n>=i:
print(i,end=' ')
全体集合
Python
def dis(x1,y1,x2,y2):
return abs(x1-x2)+abs(y1-y2)
a=[0]*1010
b=[0]*1010
c=[0]*1010
d=[0]*1010
n,m=map(int,input().split())
for i in range(0,n):
a[i],b[i]=map(int,input().split())
for i in range(0,m):
c[i],d[i]=map(int,input().split())
for i in range(0,n):
minv=1926081719260817
minj=0for j in range(0,m):
dist=dis(a[i],b[i],c[j],d[j])
if dist<minv:
minv=dist
minj=j
print(minj+1)
小 X 与机器人 2 (sihuo)
Python
x,y=list(),list()
s1,s2,t=int(),int(),int()
x.append(-1000)
y.append(-1000)
for i in range(1,5):
a,b=map(int,input().split())
x.append(a)
y.append(b)
x.sort()
y.sort()
t=1if ((x[1]==x[2])and(x[3]==x[2])and(x[3]==x[4])or(y[1]==y[2])and(y[3]==y[2])and(y[3]==y[4])):
print('zhisi')
exit(0)
for i in range(1,4):
for j in range(i+1,5):
if (x[i]==x[j]):
s1+=1;if (y[i]==y[j]):
s2+=1;if ((s1==2)and(s2==2)):
print('fangsi')
exit(0)
if(s1==3):
for i in range(1,4):
if (y[i]==y[i+1]):
if (i==2):
print('dingsi')
exit(0)
else:
print('qusi')
exit(0)
if (s2==3):
for i in range(1,4):
if (x[i]==x[i+1]):
if (i==2):
print('dingsi')
exit(0)
else:
print('qusi')
exit(0)
if ((s1==1)or(s2==1)):
print('wansi')