#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();
int min=5600;
int index=-1;
for (int i = 0; i < n; i++) {
int t; // a temperature expressed as an integer ranging from -273 to 5526
cin >> t; cin.ignore();
if(abs(t)<abs(min))
{
min=t;
index=i;
//cout<<min<<endl;
}
}
// Write an answer using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;
//cout << "result" << endl;
cout<<index <<endl;
cout<<min<<endl;
}
here is my code in codeblocks. it works well while negative in codinggame. i am not sure whatâs wrong.
Your code does not work for cases when a negative minimum appears before a positive minimum of the same magnitude, e.g. for the case of -5, 5, your code will output -5 instead of the expected answer of 5.
Also, your code will output both index and min to the standard stream. Anything extra in the standard output will be treated as wrong. As can be seen in the comments near the end of your code, you should use cerr instead, e.g.
cerr << index << endl; // for debugging
cout << min << endl; // for answering
btw you can format your code properly by using the </> button on the formatting toolbar in this forum
@Lainea I think it should be System.out.println(0), not â0â. Now, what is your condition to print 0? In my case, I put all my temperatures in an int array. If the array has length ==0 then it means no temperatures were provided.
I also tried to do the job with an array, same problem. I enter into the if. I write 0 â nothing is expected. I write ââ â â0â is expected. I write â0â â nothing is expectedâŠ
I passed all the others tests
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = in.nextInt();
}
if (array.length == 0) {
System.err.println("rentre ici");
System.out.println(0);
}
Thank you for your offer ! But Iâm sorry, I donât see how I can message you :
âNeed to have a direct personal conversation with someone, outside the normal conversational flow? Message them by selecting their avatar and using the message button.â
From this page, I click on your avatar, I access to your user page⊠I never see this message button
Having a difficult time trying to get this solution to work, can anyone point me in the correct direction?
import java.util.*;
import java.io.*;
import java.math.*;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int result = 0;
int n = in.nextInt(); // the number of temperatures to analyse
int[] array = new int[n]; //new int[n];
if (n <= 0) {
result = 0;
}
for (int i = 0; i < n; i++) {
int t = in.nextInt(); // a temperature expressed as an integer ranging from -273 to 5526
array[i] = t;
}
for (int j = 0; j < array.length; j++) {
result = array[0];
if (Math.abs(array[j]) < Math.abs(result)) {
result = array[j];
}
if (Math.abs(array[j]) == Math.abs(result) && array[j] > 0) {
result = array[j];
}
//System.err.println("Debug messages..." + result);
}
// Write an answer using System.out.println()
System.out.println(result);
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int result = 0;
ArrayList<Integer> temperatures = new ArrayList<>();
if (n > 0 && n < 10000) {
result = in.nextInt();
for (int i = 0; i < n-1; i++) {
int t = in.nextInt();
temperatures.add(t);
if (t >= 0)
result = Math.min(t, result);
else
result = Math.max(t, result);
}
if (result < 0) {
for(int item : temperatures)
if (item == Math.abs(result))
result = item;
}
}
System.out.println(result);
}
n = int(input()) # the number of temperatures to analyse
temps = [int(i) for i in input().split()]
if len(temps)!=0:
abs_temps = [abs(i) for i in temps]
abs_minimum = min(abs_temps)
indices = [i for i,x in enumerate(abs_temps) if x == abs_minimum]
if len(indices)>1:
element_filter = [i for i in indices if temps[i]>0]
if not element_filter:
result = temps[indices[0]]
else:
result = temps[element_filter[0]]
else:
result = temps[indices[0]]
else:
result = 0
# Write an answer using print
# To debug: print("Debug messages...", file=sys.stderr, flush=True)
print(result)
My solution feels messy with multiple branches. Is there a better solution without using numpy?
Instead of reading all the temperatures at one go and storing them in a list, you may read the temperatures one by one and update your answer as required. You may try using no lists at all.
If you use a list you can also use the min function with a custom sorting key.
I give you an example.
Say you have a list of (r, g, b) color tuples.
You want the one that maximizes the sum r+g+b and, as a tie breaker, you want to favor those where r > g.
Youâd write something like that: max(colors, key=lambda c: (sum(c), c[0] > c[1]))