【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

相关推荐
deflag几秒前
第T1周:Tensorflow实现mnist手写数字识别
人工智能·python·机器学习·分类·tensorflow
大柏怎么被偷了3 分钟前
【C++算法】位运算
开发语言·c++·算法
程序猿方梓燚4 分钟前
C/C++实现植物大战僵尸(PVZ)(打地鼠版)
c语言·开发语言·c++·算法·游戏
CPP_ZhouXuyang5 分钟前
C语言——模拟实现strcpy
c语言·开发语言·数据结构·算法·程序员创富
闻缺陷则喜何志丹5 分钟前
【C++前后缀分解 动态规划】2100. 适合野炊的日子|1702
c++·算法·动态规划·力扣·前后缀分解·日子·适合
Zucker n11 分钟前
猫狗识别大模型——基于python语言
开发语言·python
逝去的秋风19 分钟前
【代码随想录训练营第42期 Day57打卡 - 图论Part7 - Prim算法与Kruskal算法
算法·图论·prim算法
QXH20000028 分钟前
数据结构—双向链表
c语言·数据结构·算法·链表
bluebonnet2728 分钟前
【Rust练习】15.match 和 if let
开发语言·后端·rust
yueqingll43 分钟前
020、二级Java选择题综合知识点(持续更新版)
java·开发语言