本系列用于对著名信奥练习网站洛谷的题目详解(P系列)。本文作为本系列第一篇文章,简单介绍一道入门级题目,即P1000。
一、原题
超级玛丽是一个非常经典的游戏。请你用字符画的形式输出超级玛丽中的一个场景。
********
************
####....#.
#..###.....##....
###.......###### ### ###
........... #...# #...#
##*####### #.#.# #.#.#
####*******###### #.#.# #.#.#
...#***.****.*###.... #...# #...#
....**********##..... ### ###
....**** *****....
#### ####
###### ######
##############################################################
#...#......#.##...#......#.##...#......#.##------------------#
###########################################------------------#
#..#....#....##..#....#....##..#....#....#####################
########################################## #----------#
#.....#......##.....#......##.....#......# #----------#
########################################## #----------#
#.#..#....#..##.#..#....#..##.#..#....#..# #----------#
########################################## ############
二、题解
本题考查基本的输出语句。在C++中,常用的输出语句有putchar(),cout和printf()三种。其中,putchar()语句仅适合单个字符的输出,在本题中应用起来较为麻烦。推荐使用cin和printf()进行输出。
这里使用cout进行演示,如下是cout的使用语句。
cout << " ********" << endl; //endl表示换行
cout << " ************" << endl;
cout << " ####....#." << endl;
cout << " #..###.....##...." << endl;
cout << " ###.......###### ### ###" << endl;
cout << " ........... #...# #...#" << endl;
cout << " ##*####### #.#.# #.#.#" << endl;
cout << " ####*******###### #.#.# #.#.#" << endl;
cout << " ...#***.****.*###.... #...# #...#" << endl;
cout << " ....**********##..... ### ###" << endl;
cout << " ....**** *****...." << endl;
cout << " #### ####" << endl;
cout << " ###### ######" << endl;
cout << "##############################################################" << endl;
cout << "#...#......#.##...#......#.##...#......#.##------------------#" << endl;
cout << "###########################################------------------#" << endl;
cout << "#..#....#....##..#....#....##..#....#....#####################" << endl;
cout << "########################################## #----------#" << endl;
cout << "#.....#......##.....#......##.....#......# #----------#" << endl;
cout << "########################################## #----------#" << endl;
cout << "#.#..#....#..##.#..#....#..##.#..#....#..# #----------#" << endl;
cout << "########################################## ############" << endl;
注意,调用cout时应当引用头文件<iostream>(即 Input & Output STREAM),并注明命名空间为标准命名空间std。
#include <iostream>
using namespace std;
使用printf()函数输出时,无需注明命名空间,使用<cstdio>(即C STanDard Input & Output)头文件。
#include <cstdio>
C++中的一切程序都应当写在整型函数main()中,main()函数的返回值应当调整为0,个别编译器能自动调整。
int main()
{
// 主程序
return 0; //返回0
}
三、完整解答
#include <iostream> //导入iostream(输入输出流)头文件
using namespace std; //用std(标准)命名空间
int main()
{
cout << " ********" << endl; //endl表示换行
cout << " ************" << endl;
cout << " ####....#." << endl;
cout << " #..###.....##...." << endl;
cout << " ###.......###### ### ###" << endl;
cout << " ........... #...# #...#" << endl;
cout << " ##*####### #.#.# #.#.#" << endl;
cout << " ####*******###### #.#.# #.#.#" << endl;
cout << " ...#***.****.*###.... #...# #...#" << endl;
cout << " ....**********##..... ### ###" << endl;
cout << " ....**** *****...." << endl;
cout << " #### ####" << endl;
cout << " ###### ######" << endl;
cout << "##############################################################" << endl;
cout << "#...#......#.##...#......#.##...#......#.##------------------#" << endl;
cout << "###########################################------------------#" << endl;
cout << "#..#....#....##..#....#....##..#....#....#####################" << endl;
cout << "########################################## #----------#" << endl;
cout << "#.....#......##.....#......##.....#......# #----------#" << endl;
cout << "########################################## #----------#" << endl;
cout << "#.#..#....#..##.#..#....#..##.#..#....#..# #----------#" << endl;
cout << "########################################## ############" << endl;
return 0; //main函数最后返回值为0
}