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

CodinGame: Temperatures 反思

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

Temperatures 温度
CodinGame: Temperatures 反思

WHAT WILL I LEARN? 我能学到什么?
Conditions,Loops,Arrays 选择结构(if 语句)、循环结构(for/ while 语句)、数组

-Solving this puzzle validates(确认) that the loop concept is understood and that you can compare a list of values.
(解决这个难题可以验证你对循环的概念是否理解,你可以通过比较一个列表的变量/值)
-This puzzle is also a playground to experiment the concept of lambdas(λ, 匿名函数) in different programming languages. It’s also an opportunity to discover functional programming.
(它是一个用不同编程语言的实验的游乐场,也是一个发现函数编程的机会)


The Goal
In this exercise, you have to analyze records of temperature to find the closest to zero.
CodinGame: Temperatures 反思

Rules
Write a program that prints the temperature closest to 0 among input data. If two numbers are equally close to zero, positive integer has to be considered closest to zero (for instance, if the temperatures are -5 and 5, then display 5).

原程序

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

using namespace std;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
int main()
{
    int n; // the number of temperatures to analyse
    cin >> n; cin.ignore();
    string temps; // the n temperatures expressed as integers ranging from -273 to 5526
    getline(cin, temps);

    // Write an action using cout. DON'T FORGET THE "<< endl"
    // To debug: cerr << "Debug messages..." << endl;

    cout << "result" << endl;
}

关卡

CodinGame: Temperatures 反思

01 Simple test case 简单的考验

Standard Output Stream:

Input messages are n = 5 temps = 1 -2 -8 4 5
output 1
有n个数,数列为 temps (string)

02 Only negative numbers 只有负数(零下)

Standard Output Stream:

Input messages are n = 3 temps = -12 -5 -137
output -5

03 Choose the right temperature 选出正确的温度

Standard Output Stream:

Input messages are n = 6 temps = 42 -5 12 21 5 24
output 5

04 Choose the right temperature 2 选出正确的温度2

Standard Output Stream:

Input messages are n = 6 temps = 42 5 12 21 -5 24
output 5

05 Complex test case 复杂的考验

Standard Output Stream:

Input messages are n = 10 temps = -5 -4 -2 12 -40 4 2 18 11 5
output 2

06 No temperature 没有温度

Standard Output Stream:

Input messages are n = 0 temps = (none)
output 0

过关思路

很明显,这是一个比大小的问题,要求的是:数列中绝对值最小的数
难点就在于:如何将string数组(字符串数组)转化为int数组(整型数组)
我们知道:

temps = “1 2 3”; <=> temps[5] = {“1”, ” “, “2”, ” “, “3”};

所以一开始我就在想:如何跳掉空格,将纯数字储存到数组array[ ]中

int closest = 5526; //温度的范围:-273 ~ 5526
for (int i = 0; i < 3*n; i++) //想到一个数有:符号+数字+空格(**想当然**3*n)
{
int ascii = temps[i];
if (ascii > 47 && ascii < 58) //0 ~ 9 (ascii码 048 ~ 057)
{
if (closest > temps[i]) closest = temps[i];
}
}

上面虽然没有使用数组,但也是这个思路,但我忘了
string型是一个数字一个空间,而不是一个数一个空间……
所以,它最多读取都是一位数正项数列
虽然也想过拼接在一起,符号、多位数字分别储存,但这样步骤十分麻烦,而且容易出错

后来,仔细的查阅了资料“C++ string转化为int数组”

c++ string转化为int数组
Split a string in C++
CodinGame: Temperatures 反思

//就是这个:
#include <iostream>
#include <sstream>   //字符串流,常用与格式转换
#include <string>

int main()
{
    std::string s("Somewhere down the road");
    std::istringstream iss(s);

    do
    {
        std::string sub;        //可以用int数组,得提前定义
     iss >> sub;             //将值流向sub
       std::cout << "Substring: " << sub << std::endl;
  } while (iss);
}
/*
Output: 
Substring: Somewhere
Substring: down
Substring: the
Substring: road */

上面模板可以改成适合题目的:

int n = 5;                        //the number of temperatures
string temps = "1 -2 -8 4 5";
istringstream out(temps);
int array[n];
for (int i= 0; i < n; i++) {
     out >> array[i];
     cout << array[i] << " ";
}

Output: 1 -2 -8 4 5

通关程序

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

using namespace std;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
int main()
{
    int n, p, q, closest = 5526; // the number of temperatures to analyse
    cin >> n; cin.ignore();
    string temps; 
    // the n temperatures expressed as integers ranging from -273 to 5526
    getline(cin, temps);

    istringstream out (temps);
    int temp[n];

    if ( n == 0) cout << "0" << endl;
    else {
        for (int i = 0; i < n; i++) {
        out >> temp[i];

        if (temp[i] < 0) p = 0 - temp[i];
        else p = temp[i];                 //绝对值

        if (closest < 0) q = -closest;
        else q = closest;                 //绝对值

        if (q > p) closest = temp[i];
        else if (q == p) closest = q;
        }

    // Write an action using cout. DON'T FORGET THE "<< endl"
    cout << closest << endl;
    }
    // To debug: cerr << "Debug messages..." << endl;
    cerr << "Debug messages are " << n << " " << temps << endl;

}
带函数function版
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>

using namespace std;

int main()
{
    int n;
    cin >> n; cin.ignore();
    string temps; 
    getline(cin, temps);

    int output(int, string);

    cout << output(n,temps) << endl;
}
int output(int num, string temperatures)
{
    istringstream out (temperatures);
    int temp[num], closest = 5526, q, p;

    if ( num == 0) return 0;
    else {
        for (int i = 0; i < num; i++) {

            out >> temp[i];

            if (temp[i] < 0) p = 0 - temp[i];
            else p = temp[i];

            if (closest < 0) q = -closest;
            else q = closest;

            if (q > p) closest = temp[i];
            else if (q == p) closest = q;
        }
        return closest;
    }
}

CodinGame: Temperatures 反思
//成功

CodinGame: Temperatures 反思

//题目全部解决后;后期提交游戏时,出现小问题(07)……好像是因为,如果两个最接近0的数都为负数,但我的程序确实只会输出正数……

试着改了下:

if ( num == 0) return 0;
    else {
        for (int i = 0; i < num; i++) {

            out >> temp[i];

            if (temp[i] < 0) p = 0 - temp[i];
            else p = temp[i];

            if (closest < 0) q = -closest;
            else q = closest;

            if (q > p) closest = temp[i];
            //加上了temp[i]与closest正负的判定
            else if (q == p&& temp[i]*closest < 0) closest = q;
            else if (q == p&& temp[i]*closest > 0) closest = -q;
        }
        return closest;

//这时候,原来问题解决了,但又出现新的问题(03)……
//巧合是,我将修改的第二行去掉,运行成功了
            if (q > p) closest = temp[i];
            //只保留下面这一句
            else if (q == p&& temp[i]*closest < 0) closest = q;

CodinGame: Temperatures 反思