欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CodinGame: The Descent 反思

程序员文章站 2024-01-17 18:16:10
...

CodinGame 中的一款easy难度的编程游戏,第一次接触,玩的时候很多方面都有些不懂,像数据的输入。这是一篇随笔吧,记录,反思……

有兴趣可以去看看,可以学到蛮多编程知识的,其过程也是蛮有趣的~

The descent 星舰降落

CodinGame: The Descent 反思

The Goal

Destroy the mountains before your starship collides with one of them. For that, shoot the highest mountain on your path.
//在你的星舰和山碰撞之前,摧毁山峰。为此,你需要在你的路上轰击 最高 的山峰。 collide 碰撞

Rules

At the start of each game turn, you are given the height of the 8 mountains from left to right.
By the end of the game turn, you must fire on the highest mountain by outputting its index (from 0 to 7).
//游戏开始时,你会得到(输入)从左到右七座山的高度;在游戏中星舰转向时,你必须对最高峰开火(通过输出山峰的编号 0~7)。

Firing on a mountain will only destroy part of it, reducing its height. Your ship descends after each pass.
//轰击山,会摧毁山的一部分,减低它的高度。没过一次(单程)星舰的高度会下降。

原·源程序

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * The while loop represents the game.
 * Each iteration represents a turn of the game
 * where you are given inputs (the heights of the mountains)
 * and where you have to print an output (the index of the mountain to fire on)
 * The inputs you are given are automatically updated according to your last actions.
 **/
int main()
{
    // game loop
    while (1) {
        for (int i = 0; i < 8; i++) {
            int mountainH; // represents(表示) the height of one mountain.
            cin >> mountainH; cin.ignore ();
            }

        // Write an action using cout. DON'T FORGET THE "<< endl"
        // To debug: cerr << "Debug messages..." << endl;
        cout << "4" << endl; // The index of the mountain to fire on.
    }
}

五个关卡

Descending mountains //山的高度是递减的 descending: 递减

Game information:
Let’s destroy those mountains to secure our landing…

Height of mountain 0 : 9 //读法:0编号的山高度为9;以下相同
Height of mountain 1 : 8 //输入山的高度8;cin >> mountainH;
Height of mountain 2 : 7
Height of mountain 3 : 6
Height of mountain 4 : 5
Height of mountain 5 : 4
Height of mountain 6 : 3
Height of mountain 7 : 2

CodinGame: The Descent 反思

/**开始时,我是简单的看规律:

while (1) {
        for (int i = 0; i < 8; i++) {
            int mountainH; 
            cin >> mountainH; cin.ignore ();
                  cout << 9-mountainH << endl;  
                  //山的高度加编号等于9;
                  //但之后的关卡不能这样用,是要一个程序通过五个关卡的
            }

后来仔细一看,是要在输入的高度(数据)中找到最大高度,并输出对应的编号;
这个一开始是想使用数组完成,但发现并不是最简单的,且数据也是一直在输入,不太适用;
又想到用 i 作为山的编号,通过循环比较找到最高峰对应的 imax, 并输出;

    int imax = 0 ;   //最高山峰的编号
    while (1) {
        int max = 0 ;  
        //最高峰的高度,并初始化为0;
        //这个是需要注意的,因为后期输入的数值会变
        for (int i = 0; i < 8; i++) {
            int mountainH; 
            // represents the height of one mountain.
            cin >> mountainH; cin.ignore ();
            if (mountainH > max) {
                max = mountainH;
                imax = i;
            }
        }

*注:山在被轰击后,其高度值会变化,会表现第二次输入的数值中 **/

Scattered mountains

//山的高度是散乱的 scattered: 散乱的
Game information:
Let’s destroy those mountains to secure our landing…

Height of mountain 0 : 9
Height of mountain 1 : 8
Height of mountain 2 : 7
Height of mountain 3 : 3
Height of mountain 4 : 6
Height of mountain 5 : 5
Height of mountain 6 : 2
Height of mountain 7 : 4

Strong mountains 1

//坚硬的山,有些山需要多打几次才会倒
Game information:
Let’s destroy those mountains to secure our landing…

Height of mountain 0 : 0
Height of mountain 1 : 6
Height of mountain 2 : 7
Height of mountain 3 : 5
Height of mountain 4 : 0
Height of mountain 5 : 8
Height of mountain 6 : 1
Height of mountain 7 : 0

Strong mountains 2

//坚硬的山,有些山需要多打几次才会倒
Game information:
Let’s destroy those mountains to secure our landing…

Height of mountain 0 : 0
Height of mountain 1 : 6
Height of mountain 2 : 0
Height of mountain 3 : 5
Height of mountain 4 : 0
Height of mountain 5 : 8
Height of mountain 6 : 0
Height of mountain 7 : 6

One mountain

//只有一座山,需要一直攻击它
Game information:
Let’s destroy those mountains to secure our landing…

Height of mountain 0 : 0
Height of mountain 1 : 0
Height of mountain 2 : 0
Height of mountain 3 : 8
Height of mountain 4 : 0
Height of mountain 5 : 0
Height of mountain 6 : 0
Height of mountain 7 : 0

通关程序

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * The while loop represents the game.
 * Each iteration represents a turn of the game
 * where you are given inputs (the heights of the mountains)
 * and where you have to print an output (the index of the mountain to fire on)
 * The inputs you are given are automatically updated according to your last actions.
 **/
int main()
{
    int imax = 0 ;   //表示要射击山峰的编号
    // game loop
    while (1) {
        int max = 0 ;  //选出最高峰的高度
        for (int i = 0; i < 8; i++) {
            int mountainH; // represents the height of one mountain.
            cin >> mountainH; cin.ignore ();
            if (mountainH > max) {
                max = mountainH;
                imax = i;
            }
        }

        // Write an action using cout. DON'T FORGET THE "<< endl"
        cout << imax << endl;

        // To debug: cerr << "Debug messages..." << endl;
        cerr << "Debug messages... " <<endl;
       // cout << "4" << endl; // The index of the mountain to fire on.
    }
}

CodinGame: The Descent 反思
//你成功着陆!

相关标签: 编程 游戏