zoukankan      html  css  js  c++  java
  • Python练习——Codeforces题目

    A. Way Too Long Words

    链接:http://codeforces.com/contest/71/problem/A

    题意:将长度超过10的字符串进行缩写,缩写规则是保留头尾两字符,中间部分用它的长度来代替。

    代码:

    n = int(input())
    while n > 0:
        n -= 1
        s = input()
        if len(s) <= 10:
            print(s)
        else:
            print(s[0] + str(len(s) - 2) + s[-1])
    

    B. Next Round

    链接:http://codeforces.com/contest/158/problem/A

    题意:以非递增的顺序给出(n)个选手的得分,求得分(ge)(k)个选手且得分为正整数的选手数量。

    代码:新学会使用列表存取数据

    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    cnt = 0
    for i in range(len(a)):
        if a[i] >= a[k - 1] and a[i] > 0:
            cnt += 1
        else:
            break
    print(cnt)
    

    C. String Task

    链接:http://codeforces.com/contest/118/problem/A

    题意:给出一个字符串,将其中的"A", "O", "Y", "E", "U", "I"删除,将剩余字母变为小写,在每个字母前加一个"."。

    代码:Amazing!

    1. input.lower():将输入的字符串中的大写字母转化为小写。

    2. .join(): 连接字符串数组。将字符串、元组、列表中的元素以指定的字符(分隔符)连接生成一个新的字符串。语法:str.join(sequence),返回通过指定字符连接序列中元素后生成的新字符串。

      str = '-'
      seq = ('a', 'b', 'c')
      print(str.join(seq))
      
    3. not in:判断是否不在集合中。

    print(''.join('.' + i for i in input().lower() if i not in 'aoyeui'))
    

    D. Team

    链接:http://codeforces.com/contest/231/problem/A

    题意:(n)组数字,每组有(3)个,只包含(0)(1)。求出现数字(ge2)(1)的数字组数。

    代码:

    n = int(input())
    res = 0
    for _ in range(n):
        c = list(map(int, input().split()))
        t = 0
        for i in c:
            if i == 1:
                t += 1
        if t >= 2:
            res += 1
    print(res)
    

    E. Domino piling

    链接:http://codeforces.com/contest/50/problem/A

    题意:在一个(N imes M)的棋盘上放大小为(2 imes 1)的牌,牌可以横着放,但不允许有重叠。问这个棋盘上最多可以放多少牌。

    代码:

    n, m = map(int, input().split())
    print(n * m // 2)
    

    F. Bit++

    链接:http://codeforces.com/contest/282/problem/A

    题意:给出(n)个长度为(3)的表达式,表达式中只包含++--x,初始时(x)值为(0),求经过(n)个操作后结果是多少。

    代码:

    res = 0
    n = int(input())
    for i in range(n):
        s = input()
        if '+' in s:
            res += 1
        else:
            res -= 1
    print(res)
    

    G. Petya and Strings

    链接:http://codeforces.com/contest/112/problem/A

    题意:给出两个字符串,忽略他们的大小进行比较。

    代码:

    a = input().lower()
    b = input().lower()
    if a > b:
        print(1)
    elif a < b:
        print(-1)
    else:
        print(0)
    

    H. Football

    链接:http://codeforces.com/contest/96/problem/A

    题意:检查给出的字符串中是否包含连续的(7)(0)(7)(1)

    代码:

    t = input()
    print(['YES', 'NO'][7 * '0' not in t and 7 * '1' not in t])
    

    I. Helpful Maths

    链接:http://codeforces.com/contest/339/problem/A

    题意:给出一个只包含数字1、2、3和+号的表达式,将它重新表示为数字升序的加法。

    代码:input()[::2]:以增量为(2)读取数据。

    print('+'.join(sorted(input()[::2])))
    

    J. Beautiful Matrix

    链接:http://codeforces.com/contest/263/problem/A

    题意:在一个(5 imes 5)的矩阵中有(24)(0)(1)(1),每次操作都可以将(1)与周围相邻的(0)交换位置,问最少经过多少次交换可以使(1)到中间。

    代码:

    for i in range(5):
        t = input().split()
        for j in range(5):
            if t[j] == '1':
                print(abs(i - 2) + abs(j - 2))
    

    K. Word Capitalization

    链接:http://codeforces.com/contest/281/problem/A

    题意:将给出的字母首字母变为大写。

    代码:

    t = input()
    print(t[0].upper() + t[1:])
    

    L. Stones on the Table

    链接:http://codeforces.com/contest/266/problem/A

    题意:找出字符串总相邻字符相同的个数。

    代码:

    n = int(input())
    t = input()
    res = 0
    for i in range(n - 1):
        if t[i] == t[i + 1]:
            res += 1
    print(res)
    

    M. Boy or Girl

    链接:http://codeforces.com/contest/236/problem/A

    题意:判断字符串中不同单词的数量是偶数还是奇数。

    代码:set去重。

    print('IGNORE HIM!' if len(set(input())) % 2 else 'CHAT WITH HER!')
    

    N. Young Physicist

    链接:http://codeforces.com/contest/69/problem/A

    题意:判断(n)个三元组的每一元的和是否都是(0)

    代码:太棒了,学到好多!

    1. any():用于判断给定的可迭代参数iterable是否全部为False,如果有一个为True,则返回True。语法:any(iterable),返回TrueFalse

      print(any(['a', 'b', 'c', 'd']))
      print(any(['a', 'b', '', 'd']))
      print(any([0, '', False]))
      print(any(('a', 'b', 'c', 'd')))
      print(any(('a', 'b', '', 'd')))
      print(any((0, '', False)))
      print(any([]))
      
    2. zip():用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。如果各个迭代器的的元素个数不一致,则返回列表长度与最短的对象相同,利用*号操作符可以将元组解压为列表。语法:zip([iterable, ...]),返回元组列表。

      a = [1, 2, 3]
      b = [4, 5, 6]
      c = [4, 5, 6, 7, 8]
      zipped1 = zip(a, b)	# [(1, 4), (2, 5), (3, 6)] 打包为元组的列表
      zipped2 = zip(a, c)	# [(1, 4), (2, 5), (3, 6)] 元素个数与最短的列表一致
      zip(*zipped1)	# [(1, 2, 3), (4, 5, 6)] *zip可理解为解压,返回二维矩阵式
      
    3. map():根据提供的函数对指定序列做映射,第一个参数function以参数序列中的每一个元素调用function函数,返回包含每次function函数返回值的新列表。语法:map(function, iterable, ...),返回迭代器。

      map(int, input().split())
      map(lambda x: x ** 2, [1, 2, 3, 4, 5])	# 使用lambda匿名函数计算列表各元素的平方
      
    print('NO' if any(map(sum, zip(*[map(int, input().split()) for i in range(int(input()))]))) else 'YES')
    

    O. Chat room

    链接:http://codeforces.com/contest/58/problem/A

    题意:判断给出的字符串是否可以通过删除部分字符得到hello

    代码:

    1. iter():用来生成迭代器。语法:iter(object[, sentinel]),返回迭代器对象。
    2. all():用于判定给定的可迭代参数iterable中的所有元素是否否为True,如果是返回True,否则返回False。元素除了0NoneFalse外都算True。语法:all(iterable),返回TrueFalse
    s = iter(input())
    print('YES' if all([c in s for c in 'hello']) else 'NO')
    

    P. Lucky Division

    链接:http://codeforces.com/contest/122/problem/A

    题意:判断(n)能否被只包含(4)(7)构成的数字整除。

    代码:

    n = int(input())
    print('YES' if any(n % i == 0 for i in [4, 7, 47, 74, 444, 447, 474, 477, 744, 747, 774, 777]) else 'NO')
    

    Q. Taxi

    链接:http://codeforces.com/contest/158/problem/B

    题意:一辆车最多乘(4)人,现有(n)队人,每队人有(1/2/3/4)个人,同队的人要坐同一辆车,同一辆车可以同时坐多队人。问最少需要多少辆车。

    代码:count():用于统计字符串里某个字符出现的次数。语法:str.count(sub, start = 0, end = len(string)),返回子字符串在字符串中出现的次数。

    input()
    a, b, c, d = map(input().count, ('1', '2', '3', '4'))
    print(c + d + (b * 2 + max(0, a - c) + 3) // 4)
    

    R. Little Artem

    链接:https://codeforces.com/contest/1333/problem/A

    题意:在一个(n imes m)的画板上,将一部分块涂黑,另一部分涂白,要求至少有一个边与白块相邻的黑块个数恰好比至少有一个边与黑块相邻的白块的个数多一个。

    代码:

    t = int(input())
    for _ in range(t):
        n, m = map(int, input().split())
        print('W', end = '')
        print('B' * (m - 1))
        for i in range(n - 1):
            print('B' * m)
    

    S. Erasing Zeroes

    链接:https://codeforces.com/contest/1303/problem/A

    题意:删除一个(01)字符串中最少个数的(0),使得(1)连续。

    代码:strip():用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。语法“str.strip([chars]),只可删除开头或是结尾的字符,不能删除中间部分的字符。

    for i in range(int(input())):
        print(input().strip('0').count('0'))
    
  • 相关阅读:
    JQuery 判断某个属性是否存在 hasAttr
    微信支付开发-Senparc.Weixin.MP详解
    c# 两个数组比较,将重复部分去掉,返回不重复部分
    String.Format数字格式化输出 {0:N2} {0:D2} {0:C2
    asp.net 时间比较,常用于在某段时间进行操作
    关于C#正则表达式MatchCollection类的总结,正则表达式的应用
    开发错误11:Configuration with name ‘default’ not found
    Android新旧版本Notification
    Okio 1.9简单入门
    Android  PNG透明图片转JPG格式背景变黑
  • 原文地址:https://www.cnblogs.com/songjy11611/p/13687765.html
Copyright © 2011-2022 走看看