洛谷题目详解:P1000 超级玛丽游戏

本系列用于对著名信奥练习网站洛谷的题目详解(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
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值