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

文章目录



简单图形绘制

python 复制代码
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)

或者分三种(作者:SupercraftD)

python 复制代码
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")


判断


不推荐第一种写法

python 复制代码
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)

没必要这样写,观察结果

python 复制代码
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)


数字的个数

python 复制代码
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))


中间字符

python 复制代码
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]   )

END

相关推荐
我是唐青枫3 分钟前
Rust cargo 命令行工具使用教程
开发语言·后端·rust
BS_Li4 分钟前
八大排序算法
数据结构·算法·排序算法
脱脱克克7 分钟前
Python中的占位符pass用法
python
哪吒编程11 分钟前
突破亚马逊壁垒,Web Unlocker API 助您轻松获取数据
后端·python
烟锁池塘柳015 分钟前
【数学建模】(智能优化算法)天牛须算法(Beetle Antennae Search, BAS)详解与Python实现
算法·数学建模
思陌Ai算法定制16 分钟前
医学分割新标杆!双路径PGM-UNet:CNN+Mamba实现病灶毫厘级捕捉
人工智能·深度学习·神经网络·算法·机器学习·cnn
烟锁池塘柳016 分钟前
【数学建模】(智能优化算法)萤火虫算法(Firefly Algorithm)详解与实现
算法·数学建模
风掣长空23 分钟前
[leetcode]第445场周赛
数据结构·算法·leetcode
郜太素40 分钟前
决策树+泰坦尼克号生存案例
人工智能·算法·决策树·机器学习·数据挖掘·学习方法
这里有鱼汤44 分钟前
Python中的时间与日期处理:那些你可能不知道的小技巧
后端·python