在场上打表骗了40分,纪念一下失去的分数。
题意简介
我们需要根据给定的整数 和 生成特定格式的输出。
当 时,直接输出整数 ;
当 时,生成一个 C++ 程序,该程序在运行时会产生一个输出 的 C++ 程序,并且这个过程可以嵌套多层。
方法思路
由于 最大也只是 , 我们可以直接暴力
关键点在于正确处理字符串中的特殊字符(如双引号、反斜杠和换行符),确保生成的 C++ 程序语法正确。
由此,我们可以定义转换函数
change把'\'转化为'\\'
把'" '转化为'\"'
把换行符 \n → \\ncode
//自测能过
#include <bits/stdc++.h>
using namespace std;
string change(string s)
{
string t = "";
for (char c : s)
{
if (c == '\\') t += "\\\\";
else if (c == '"') t += "\\\"";
else if (c == '\n') t += "\\n";
else t += c;
}
return t;
}
int main()
{
int n, k;
cin >> n >> k;
if (n == 1) cout << k << endl;
else
{
string s = "#include <iostream>\nint main() { std::cout << " + to_string(k) + " << std::endl; }";
for (int i = 2; i < n; i++)
{
s = "#include <iostream>\nint main() { std::cout << \"" + change(s) + "\" << std::endl; }";
}
cout << s << endl;
}
return 0;
}