蓝桥杯软件类竞赛--Python的常用操作示例

算法教材:《算法竞赛入门到进阶》 清华大学出版社
网购:京东 当当   作者签名书(有发票):点我
有建议请加QQ 群:567554289


  参加蓝桥杯Python组比赛,初学者常见的问题是对输入和输出的处理有疑问,还有常用的排序、队列等不熟。本文总结有关的操作。

1. for循环

  例1: i i i的范围是4~(20190325-1)

for i in range(4,20190325):

  参考:https://blog.csdn.net/weixin_43914593/article/details/112979520
  
  例2: i i i的范围是0~(len(num)-1)

for i in range(len(num)):

  参考:https://blog.csdn.net/weixin_43914593/article/details/112560449

2. sort()和sorted()函数

  sort()对一个数组排序,例:

n, s = map(int,input().split())
a = list(map(int,input().split()))
a.sort()

  参考:https://blog.csdn.net/weixin_43914593/article/details/112930544

  sorted()对结构体排序,例:

a = sorted(a, key=lambda a: a[0])

  参考:https://blog.csdn.net/weixin_43914593/article/details/112979653

3. 队列

  例:

扫描二维码关注公众号,回复: 13727351 查看本文章
from queue import *
q = Queue()              
q.put((x,y))    #用队列直接处理结构体
q.empty():            
q.get() 

  参考:https://blog.csdn.net/weixin_43914593/article/details/112851771

4. 数组赋值

  例1:把一维数组赋值为0

flag = [0 for i in range(n+1)]

  参考:https://blog.csdn.net/weixin_43914593/article/details/112979653

  例2:把二维数组赋值为0

r = [[0] * 3 for i in range(k)]

  参考:https://blog.csdn.net/weixin_43914593/article/details/112909051

  例3:把S[i]赋值为i

N=1000002
s=[]   
for i in range(N):  
    s.append(i)

  参考:https://blog.csdn.net/weixin_43914593/article/details/112979670

5. 输入

5.1 在一行中输入多个整数

  例1:第一行包含一个整数N,第二行包含N 个整数

n = int(input())
a = input().split(" ")
int(a[i])     #使用时要转换

#或者这样写:
a=[int(i) for i in input().split()]

  参考:https://blog.csdn.net/weixin_43914593/article/details/112979634

  例2:输入一行多个整数

num=[int(i) for i in input().split()]

  参考:https://blog.csdn.net/weixin_43914593/article/details/112560449

5.2 二维数组的输入

  例:第一行包含3 个整数N、M 和T,后面M 行每行包含两个整数ts 和id。

first = input()
n, m, T = [int(i) for i in first.split()]

a = []       #这是二维数组
for i in range(m):
    a.append([int(i) for i in input().split()])

  参考:https://blog.csdn.net/weixin_43914593/article/details/112979653

5.3 输入用非空格字符隔开的数字

  例:
  输入第一行为一个正整数T,表示输入数据组数。
  每组数据包含两行,第一行为去程的 起降 时间,第二行为回程的 起降 时间。
  起降时间的格式如下
  h1:m1:s1 h2:m2:s2
  h1:m1:s1 h3:m3:s3 (+1)
  h1:m1:s1 h4:m4:s4 (+2)

def get_time():
     line = str(input()).split(' ')   #一行字符串,以空格分开,分别读取     
     h1=int(line[0][0:2])     #处理字符串中的数字
     m1=int(line[0][3:5])
     s1=int(line[0][6:8])
     
     h2=int(line[1][0:2])
     m2=int(line[1][3:5])
     s2=int(line[1][6:8])

     day = 0 
     if(len(line)==3):    #line中有3个元素,最后一个是day
          day = int(line[2][2])

     S = h1*3600 + m1*60 + s1
     E = h2*3600 + m2*60 + s2
     return E - S + day*24*3600
 
n = int(input())
for i in range(n):
    ans = (get_time()+ get_time())/2
    hh = int(ans/3600)
    mm = int(ans/60%60)
    ss = int(ans%60)
    print("{:0>2d}:{:0>2d}:{:0>2d}".format(hh,mm,ss))

  参考:https://blog.csdn.net/weixin_43914593/article/details/112728088

5.4 用map输入多个数字

  例1:
  第一行包括 4 个正整数 A, B, C, m;
  第二行包含 A × B × C 个整数,其中第 ((i − 1)×B + (j − 1)) × C + (k − 1)+1 个数为 d(i, j, k);
  第 3 到第 m + 2 行中,第 (t − 2) 行包含 7 个正整数 lat, rat, lbt, rbt, lct, rct, ht。

A,B,C,m = map(int,input().split())

ship=[]
for i in range(A):
    sublist=[]
    for j in range(B):
        sublist.append([0]*C)
    ship.append(sublist)
    
life=list(map(int,input().split()))

  参考:https://blog.csdn.net/weixin_43914593/article/details/112761182

  例2:第一行包括 2 个正整数 n, K。第二行 n 个正整数,代表给定的 n 个数。

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

  参考:https://blog.csdn.net/weixin_43914593/article/details/112909051

  例3: 第一行包含两个整数 n、S;第二行包含 n 个非负整数 a1, …, an。

n, s = map(int,input().split())
a = list(map(int,input().split()))

  参考:https://blog.csdn.net/weixin_43914593/article/details/112930544

5.5 输入字符

  例: 输入一个由“x()|”组成的字符串。

s = input()
if s[pos] == '(':
elif s[pos] == 'x':

  参考:https://blog.csdn.net/weixin_43914593/article/details/112363933

5.6 换行输入

  例1:
  第一行包含一个整数N。(1 <= N <= 100)
  以下N行每行包含一个整数Ai。(1 <= Ai <= 100)

n=int(input())
numlist=[]
for i in range(n):
    numlist.append(int(input()))

  参考:https://blog.csdn.net/weixin_43914593/article/details/112405425

  例2:
  第一行包含两个整数N和K。(1 <= N, K <= 100000)
  以下N行每行包含两个整数Hi和Wi。(1 <= Hi, Wi <= 100000)

n,k = map(int,input().split())
w = []
h = []
for i in range(n):
    a,b = map(int,input().split())
    w.append(a)
    h.append(b)

  参考:https://blog.csdn.net/weixin_43914593/article/details/112424722

5.7 未明确说明终止的输入

  有时题目没有明确说明什么时候输入终止,例如“存在多组测试数据,每组测试数据一行包含一个正整数n(1<=n<=10000)。”
  解决方法:for n in sys.stdin

import sys
for n in sys.stdin:   
    n = int(n)
    n1 = int(n**0.5)
    s = 0
    for a in range(1, n1+1):
        for b in range(a, n1+1):
            if n - a**2 - 2*(b**2) >= 0:
                c = int(pow((n - a**2 - b**2 ), 0.5))
                if a**2 + b**2 + c**2 == n:
                    s = 1
                    print("{} {} {}".format(a, b, c))
 
    if s == 0:
        print("No Solution")

  参考:http://oj.ecustacm.cn/problem.php?id=1250
  这题的c++代码是:

#include <iostream>
#include <cmath> 
using namespace std;
bool flag;
int main(){
    
    
    int n;
    while(cin>>n){
    
    
     int m = sqrt(n)+1;
     for(int i=1;i<=m;i++){
    
    
        for(int j=i;j<=m;j++){
    
    
            for(int k=j;k<=m;k++){
    
    
                if(i*i+j*j+k*k==n){
    
    
                    flag=true;
                    cout<<i<<' '<<j<<' '<<k<<endl;
                }
            }
        }
     }
     if(!flag){
    
    
        cout<<"No Solution"<<endl;
     }
     flag=false;
   }
   return 0;
}

6. 带格式输出

  例1: 输出四舍五入保留 4 位小数。

print('{:.4f}'.format(sqrt(sum/(n))))

  参考:https://blog.csdn.net/weixin_43914593/article/details/112930544
  
  例2:
  对于每一组数据输出一行一个时间hh:mm:ss,表示飞行时间为hh小时mm分ss秒。
  注意,当时间为一位数时,要补齐前导零。如三小时四分五秒应写为03:04:05。

print("{:0>2d}:{:0>2d}:{:0>2d}".format(hh,mm,ss))

  参考:https://blog.csdn.net/weixin_43914593/article/details/112728088

猜你喜欢

转载自blog.csdn.net/weixin_43914593/article/details/115022186
今日推荐