Robert Eisele
Engineer, Systems Architect and DBA

# Codingame: Temperatures

Original Problem

## The Goal

In this exercise, you have to analyze records of temperature to find the closest to zero.

Sample temperatures
Here, -1 is the closest to 0.

## 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).

## Game Input

Your program must read the data from the standard input and write the result on the standard output.
Input

Line 1: N, the number of temperatures to analyze

Line 2: A string with the N temperatures expressed as integers ranging from -273 to 5526

Output
Display 0 (zero) if no temperatures are provided. Otherwise, display the temperature closest to 0.
Constraints
0 ≤ N < 10000

## C++ Solution

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

using namespace std;

int main() {
int N;
cin >> N;
cin.ignore();
string TEMPS;
getline(cin, TEMPS);

istringstream buf(TEMPS);

int tmp, min = 0;
for (int i = 0; i < N; ++i) {

buf >> tmp;

if (i == 0 || abs(tmp) < abs(min)) {
min = tmp;
} else if (abs(tmp) == abs(min)) {
min = abs(tmp);
}
}
cout << min << endl;
return 0;
}```

Go to overview

All images are copyright to Codingame