Other
sy132: 全排列I 中等
https://sunnywhy.com/sfbj/4/3/132
cpp
#include <iostream>
#include <vector>
using namespace std;
void permute(vector<bool>& valid, vector<int>& nums, int first, int n)
{
if (first == n + 1)
{
for (int i = 0; i < n; i++)
i < n - 1 ? cout << nums[i] << ' ' : cout << nums[i] << '\n';
return;
}
for (int i = 1; i <= n; i++)
if (valid[i])
{
valid[i] = false;
nums.push_back(i);
permute(valid, nums, first + 1, n);
valid[i] = true;
nums.pop_back();
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> nums;
vector<bool> valid(n + 1, true);
permute(valid, nums, 1, n);
return 0;
}sy578
cpp
#include <iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
string s;
cin >> n >> k >> s;
int l = 0, r = -1, cnt = 0;
int maxLen = 0;
while (r < n)
{
if (cnt <= k)
{
maxLen = max(maxLen, r - l + 1);
r++;
if (r < n && s[r] == '0')
cnt++;
}
else
{
if (s[l] == '0')
cnt--;
l++;
}
}
cout << maxLen << '\n';
return 0;
}画矩形
https://programming.pku.edu.cn/problem/7f89efad1537471fae528e9c88601ee6/
根据参数,画出矩形。
关于输入
输入由多行组成,每行四个参数:前两个参数为整数,依次代表矩形的高和宽(高不少于3行,宽不少于5行);第三个参数是一个字符,表示用来画图的矩形符号;第四个参数为1或0,0代表空心,1代表实心。 当用户输入0时表示输入结束。
关于输出
输出画出的图形
例子输入
6 5 * 1
7 7 @ 0
0例子输出
*****
*****
*****
*****
*****
*****
@@@@@@@
@ @
@ @
@ @
@ @
@ @
@@@@@@@提示信息
对于一个题里有多组测试数据的题目,可以读取一组测试数据后直接输出该组的运行结果,不必把多组测试数据储存起来后一起输出。
从标准输入读取多行,每行格式如 H W C F,当遇到单独一行 0 时结束。F 为 1 表示实心,0 表示空心。输出各个矩形,矩形之间不额外插入空行(与题目样例一致)。
cpp
import sys
def draw_rectangle(h, w, ch, filled):
# h >= 3, w >= 5(题目保证),ch 为单字符,filled 为 0/1
line_full = ch * w
if filled:
for _ in range(h):
print(line_full)
else:
print(line_full) # 第一行
middle = ch + ' ' * (w - 2) + ch
for _ in range(h - 2):
print(middle) # 中间行
print(line_full) # 最后一行
def main():
data = sys.stdin.read().splitlines()
for line in data:
s = line.strip()
if not s:
continue
if s == '0':
break
parts = s.split()
if len(parts) < 4:
# 忽略格式错误的行(也可抛错),这里跳过
continue
try:
h = int(parts[0])
w = int(parts[1])
ch = parts[2][0] if parts[2] else '#'
filled = int(parts[3]) != 0
except:
continue
draw_rectangle(h, w, ch, filled)
if __name__ == "__main__":
main()