【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

相关推荐
Zevalin爱灰灰3 分钟前
MATLAB GUI界面设计 第六章——常用库中的其它组件
开发语言·ui·matlab
冰糖猕猴桃10 分钟前
【Python】进阶 - 数据结构与算法
开发语言·数据结构·python·算法·时间复杂度、空间复杂度·树、二叉树·堆、图
天水幼麟14 分钟前
python学习笔记(深度学习)
笔记·python·学习
巴里巴气17 分钟前
安装GPU版本的Pytorch
人工智能·pytorch·python
lifallen24 分钟前
Paimon vs. HBase:全链路开销对比
java·大数据·数据结构·数据库·算法·flink·hbase
wt_cs38 分钟前
银行回单ocr api集成解析-图像文字识别-文字识别技术
开发语言·python
_WndProc1 小时前
【Python】Flask网页
开发语言·python·flask
互联网搬砖老肖1 小时前
Python 中如何使用 Conda 管理版本和创建 Django 项目
python·django·conda
测试者家园1 小时前
基于DeepSeek和crewAI构建测试用例脚本生成器
人工智能·python·测试用例·智能体·智能化测试·crewai
liujing102329291 小时前
Day04_刷题niuke20250703
java·开发语言·算法