【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

相关推荐
Ratten21 分钟前
【Python 实战】---- 实现一个可选择、配置操作的批量文件上传工具(四)配置管理界面和逻辑实现
python
Ratten24 分钟前
【Python 实战】---- 实现一个可选择、配置操作的批量文件上传工具(五)打包成 exe 应用
python
跟橙姐学代码29 分钟前
写 Python 函数别再死抠参数了,这招让代码瞬间灵活
前端·python
CHEN5_0232 分钟前
【Java基础常见辨析】重载与重写,深拷贝与浅拷贝,抽象类与普通类
java·开发语言
Despacito0o1 小时前
C语言基础:变量与进制详解
java·c语言·开发语言
kyle~1 小时前
OpenCV---特征检测算法(ORB,Oriented FAST and Rotated BRIEF)
人工智能·opencv·算法
初学小刘1 小时前
决策树:机器学习中的强大工具
算法·决策树·机器学习
nightunderblackcat1 小时前
进阶向:人物关系三元组,解锁人物关系网络的钥匙
开发语言·python·开源·php
山顶风景独好1 小时前
【Leetcode】随笔
数据结构·算法·leetcode
站大爷IP1 小时前
Pandas与NumPy:Python数据处理的双剑合璧
python