当前位置:   article > 正文

【CodinGame】趣味算法(教学用) CLASH OF CODE -20240725

【CodinGame】趣味算法(教学用) CLASH OF CODE -20240725



简单图形绘制

在这里插入图片描述

import math
import sys

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

# length = 4
# c = "R"
length = int(input())
c = input()


for i in range(length):
    n_k = length - 1 - i
    print(" " * n_k + c)
    # 对称下半面 0 3
for i in range(length - 1):
    n_k = i + 1
    print(" " * n_k + c)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

或者分三种(作者:SupercraftD)

import math
import sys

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

# length = int(input())
# c = input()
length = 4
c = "R"

rows = []

# 这里row构建出来['   R', '  R', ' R']
for i in range(1, length):
    rows.append(" " * (length - i) + c)
# 然后解包为参数,设置参数之间以\n分隔,构成
#    R
#   R
#  R
print(*rows, sep="\n")
# 单独输出一个
# R
print(c)
# 倒序输出
#  R
#   R
#    R
print(*rows[::-1], sep="\n")

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

在这里插入图片描述



判断

在这里插入图片描述
在这里插入图片描述
不推荐第一种写法

import math
import sys

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

n = int(input())

for i in range(1, n + 1):
    if i % 3 == 0 and i % 5 == 0 and i % 4 == 0:
        print("FizzBuzzBar")

    if i % 3 == 0 and i % 4 == 0 and i % 5 != 0:
        print("FizzBar")
    if i % 5 == 0 and i % 4 == 0 and i % 3 != 0:
        print("BuzzBar")
    if i % 5 == 0 and i % 3 == 0 and i % 4 != 0:
        print("FizzBuzz")

    if i % 3 == 0 and i % 4 != 0 and i % 5 != 0:
        print("Fizz")
    if i % 5 == 0 and i % 3 != 0 and i % 4 != 0:
        print("Buzz")
    if i % 4 == 0 and i % 3 != 0 and i % 5 != 0:
        print("Bar")

    if i % 3 != 0 and i % 4 != 0 and i % 5 != 0:
        print(i)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

没必要这样写,观察结果

n = int(input())

for i in range(1, n + 1):
    output = ""
    if i % 3 == 0:
        output += "Fizz"
    if i % 5 == 0:
        output += "Buzz"
    if i % 4 == 0:
        output += "Bar"
    if output == "":
        output = str(i)
    
    print(output)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15


数字的个数

在这里插入图片描述

import sys
import math

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

n = int(input())
for i in range(n):
    row = input()
    # 检测里面的字符是数字的个数
    print(sum(c.isdigit() for c in row))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11


中间字符

在这里插入图片描述

import sys
import math

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

s = input().split()
if len(s) %2 != 0:
    print(s[len(s)//2])
else:
    # 012 345  6//2=3
    print( s[len(s)//2-1]+ s[len(s)//2]   )


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

END

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/902808
推荐阅读
相关标签
  

闽ICP备14008679号