用字符串重复构造长度为 1、3、5 的三行字符三角形。
OJ: noi_openjudge
题目 ID: ch0101-08
难度:入门
标签:输出字符串python
日期: 2026-07-30 23:01
题意
给定一个字符,输出高为 3、底边长度为 5 的等腰字符三角形。三行字符数依次为 1、3、5,前导空格数依次为 2、1、0。
思路
Python 中 character * count 会把一个字符串重复 count 次,因此每行都可以直接写出“前导空格 + 重复后的字符”。
本题的图案大小固定,逐行 print 比循环更直接,也更容易检查每一行的空格数。
代码
Python代码
python
character = input()
print(f" {character}")
print(f" {character * 3}")
print(character * 5)C++代码
这里保留原有的 C++ 参考实现。
cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
char a;
cin >> a;
cout << " " << a << endl;
cout << " " << a << a << a << endl;
cout << a;
cout << a;
cout << a;
cout << a;
cout << a;
return 0;
}复杂度
输出字符数固定,时间复杂度为
总结
字符画题先列出每一行的“前导空格数”和“字符数”。Python 的字符串乘法适合把重复字符表达得清楚。