- 分享
代码块显示行号及高亮
- @ 2025-11-28 20:32:35
Markdown 版本
实际上,HydroOJ 也支持使用 Markdown 实现代码块的代码高亮及表行。格式:
```cpp|2,3|line-numbers
#include <iostream>
using namespace std;
int main()
{
return 0;
}
```
显示效果为:
#include <iostream>
using namespace std;
int main()
{
return 0;
}
具体信息请看下面 HTML 版本。
默认格式
<pre>
<code class="language-cpp">#include <iostream>
using namespace std;
int main()
{
return 0;
}</code>
</pre>
显示效果为:
#include <iostream>
using namespace std;
int main()
{
return 0;
}
注意头文件要改为 <文件名> ,否则会识别为 HTML 标签,然后隐藏。
可以等同为:
```cpp
#include <iostream>
using namespace std;
int main()
{
return 0;
}
```
自动改头文件
- 保存下面的代码;
- 在代码同目录下建立一个
code.in文件; - 在
code.in中放你要改格式的代码; - 运行代码;
- 此时该代码目录下会增加一个
code.out文件,这就是已经改格式后的代码。
#include <iostream>
using namespace std;
int main()
{
freopen("code.in", "r", stdin);
freopen("code.out", "w", stdout);
string line;
getline(cin, line);
while (getline(cin, line))
{
for (char ch : line)
{
if (ch == '<')cout << "<";
else if (ch == '>')cout << ">";
else cout << ch;
}
cout << "\n";
}
return 0;
}
增加行号、高亮
例如:
<code class="language-cpp">#include <iostream>
using namespace std;
int main()
{
return 0;
}</code>
</pre>
只需要把上面的第一行的 language-cpp ,格式改为:
<code class="language-语言ID|高亮行号,英文逗号隔开|line-numbers">
代码
</code>
例如:
<pre>
<code class="language-cpp|2,3|line-numbers">#include <iostream>
using namespace std;
int main()
{
return 0;
}</code>
</pre>
显示效果为:
#include <iostream>
using namespace std;
int main()
{
return 0;
}
1 条评论
-
gf24240 LV 7 MOD @ 2025-11-28 21:38:43已修改HTML 和 Markdown 显示区别
(上为 HTML,下为 Markdown)
#include <iostream> using namespace std; int main() { return 0; }#include <iostream> using namespace std; int main() { return 0; }
- 1