CSP历年第一题100分--python

这篇博客汇总了从2019年至2022年CSP考试中每年第一题的Python解题思路和代码,包括202206-1、202209-1等,详细解析了各个题目,是Python编程学习者提升解题能力的宝贵资料。
摘要由CSDN通过智能技术生成

202206-1

import math
import decimal
def calu(lst, len):
    a_sum=0
    d_sum=0
    for i in range(0,len):
        a_sum+=lst[i]
    average=a_sum/len
    for i in range(0,len):
        d_sum+=pow((lst[i]-average),2)
    d=d_sum/len
    return average,d
n=int(input())
data=input()
lst=data.split(' ')
lst=[float(x) for x in lst]
return_data=calu(lst,n)
average=return_data[0]
d=return_data[1]
sqrt_d=decimal.Decimal(f'{math.sqrt(d)}')
for i in range(0,n):
   result=(lst[i]-average)/math.sqrt(d)
   print(result,end='\n')

202209-1

n_m=input()
n_m=n_m.split(' ')
n_m=[int(x) for x in n_m]
n=n_m[0]
m=n_m[1]
a_data=input()
a=a_data.split(' ')
a=[int(x) for x in a]
c=[1 for x in range(n+1)]
for i in range(1,n+1):
    c[i]=c[i-1]*a[i-1]
b=[0 for x in range(n+1)]
b[0]=int(m%c[1]/c[0])
for i in range(1,n+1):
    b[i]=int((m%c[i]-m%c[i-1])/c[i-1])
for i in range(1,n+1):
    print(b[i],end=' ')

202203-1

n, k = map(int, input().split())

a, b = [], []
vis = [0 for i in range(n + 1)]
vis[0] = 1
ans = 0
for i in range(k):
    x, y = map(int, input().split())        #当前的这个值输入之后,每个值是什么,对应的vis的下标就是什么
    if not vis[y]:      #也就是说,当前这个y值是vis的下标,为0,表示没有被初始化
        ans += 1
    vis[x] = 1  #当前这一组值的左值被初始化了,更新vis的左值为1
print(ans)

202112-1

n_N=input()
n_N=n_N.split(' ')
n_N=[int(k) for k in n_N]
n=n_N[0]
N=n_N[1]

a=input()
a=a.split(' ')
a=[int(k) for k in a]
a.insert(0,0)
c=0
sum=0
j=0
for i in range(n+1):
    if i==n:
        sum+=j*(N-a[i])
    else:
        sum+=j*(a[i+1]-a[i])
    j+=1
print(sum)

202109-1

n=int(input())
b_str=input().split(' ')
b=[int(i) for i in b_str]
a_max=[0 for i in range(n)]
a_min=[0 for i in range(n)]
a_max[0]=b[0]
a_min[0]=b[0]
for i in range(n-1):
    if b[i+1]==b[i]:
        a_max[i+1]=b[i+1]
        a_min[i+1]=0
    elif b[i+1]>b[i]:
        a_max[i+1]=b[i+1]
        a_min[i+1]=b[i+1]
print(sum(a_max))
print(sum(a_min))

202012-1

n = int(input())
sum=0
for i in range(n):
    w_s = input()
    w_s = w_s.split(' ')
    w_s = [int(k) for k in w_s]
    w = w_s[0]
    s = w_s[1]
    sum=sum+w*s
if sum>0:
    print(sum)
else:
    print('0')

202009-1

n_X_Y=input().split(' ')
n_X_Y=[int(k) for k in n_X_Y]
n=n_X_Y[0]
X=n_X_Y[1]
Y=n_X_Y[2]
x=[]
y=[]
dist={}
for i in range(n):
    x_y = input().split(' ')
    x_y = [int(k) for k in x_y]
    a = x_y[0]
    b = x_y[1]
    distance=(a-X)*(a-X)+(b-Y)*(b-Y)
    dist[i]=distance
dist_tuple=[(distance,i) for i,distance in dist.items()]
dist_tuple_sorted=sorted(dist_tuple,reverse=False)
print(dist_tuple_sorted[0][1]+1)
print(dist_tuple_sorted[1][1]+1)
print(dist_tuple_sorted[2][1]+1)

202006-1

n_m=input().split(' ')
n_m=[int(i) for i in n_m]
n=n_m[0]
m=n_m[1]
x_y_type_dic= {}
a_b_c_list=[]
for i in range(n):
    x_y_type = input().split(' ')
    x_y_type = [i for i in x_y_type]
    x = int(x_y_type[0])
    y = int(x_y_type[1])
    type = x_y_type[2]
    x_y_type_dic[(x,y)]=type
for i in range(m):
    a_b_c=input().split(' ')
    a_b_c = [i for i in a_b_c]
    a = int(a_b_c[0])
    b = int(a_b_c[1])
    c = int(a_b_c[2])
    a_b_c_list.append((a,b,c))
judge1=[]
judge2=[]
for a,b,c in a_b_c_list:
    for i,type in x_y_type_dic.items():
        x=i[0]
        y=i[1]
        if a+b*x+c*y>0:
            if type=='A':
                judge1.append(1)
            elif type=='B':
                judge1.append(2)
        else:
            if type=='A':
                judge2.append(1)
            elif type=='B':
                judge2.append(2)
    judge1 = set(judge1)
    judge2 = set(judge2)
    if len(judge1) == 1 and len(judge2) == 1:
        print('Yes')
    else:
        print('No')
    judge1 = []
    judge2 = []

201912-1

n=int(input())
a=b=c=d=0
number=1
i=1
while(i<n+1):
    include=str(number).find('7')
    if int(number)%7!=0 and include==-1:
        number += 1
    else:
        i = i - 1
        if int(number) % 4 == 1:
            a += 1
        elif int(number) % 4 == 2:
            b += 1
        elif int(number) % 4 == 3:
            c += 1
        elif int(number) % 4 == 0:
            d += 1
        number += 1
    i=i+1
print(a)
print(b)
print(c)
print(d)

201909-1

n,m=map(int,input().split(' '))
tree = {}
sum_total=0
for i in range(1,n+1):
    row=input()
    col=[int(c) for c in row.split(' ')]
    tree[i]=-(sum(col)-col[0])
    for k in range(len(col)):
        sum_total+=col[k]
tree_tuple=[(p,k) for k,p in tree.items()]
tree_tuple_sorted=sorted(tree_tuple,reverse=True)
for p,k in tree_tuple:
    if tree_tuple_sorted[0][0] == p:
        print(sum_total, k, tree_tuple_sorted[0][0])
        break
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

春有百花巨小蟹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值