Hi I need help in coding this game using C language since there’s no tutorial in youtube using the said language. Thanks
What help do you need? Please specify.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
/**
-
Auto-generated code below aims at helping you parse
-
the standard input according to the problem statement.
**/
int main()
{// a temperature expressed as an integer ranging from -273 to 5526
// the number of temperatures to analyse
int n,i,t[i],ans=5527,ab,def=0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
// a temperature expressed as an integer ranging from -273 to 5526
scanf("%d", &t[i]);
if(t[i]<ans)
{
ans=t[i];
}
else if(abs(t[i])==abs(ans) && t>ans)
{
ans=t[i];
}
}
if(n==0)
{
t[i]=0;
}
I only passed the first testcase. Also, i noticed that the if condition (t[i]<ans) can be also t[i]> ans or t[i]>5000 and it still passes the first test case. I do not know why is that. sorry still a beginner
Before pointing out the issues in your code, please note that you may format your code properly in this forum by using the </> button in the formatting toolbar.
Now, the issues in your code:
- You have initialised t[i]. But you should have waited until you know the number of temperatures n (after the first scanf). Then you can initialise t[n] (not t[i]) properly.
- In your first if, you are comparing t[i] with ans, but you should be comparing their absolute values instead.
- In your else if, you are comparing t and ans, but you should be comparing t[i] and ans instead.
- In your last if, you assign 0 to t[i]. You want to assign it to ans instead, right?
Instead of just fixing the code, please make sure you understand why the code should be fixed as described
Keep it up.
Thanks a lot , appreciate the help.
Am I the only one lost and don’t understand anything?
I couldn’t do anything, and it’s the 1st exercise, a little desperate.
If you’re a beginner and haven’t learned programming before, this website can prove too difficult for you, as a certain level of programming knowledge is a pre-requisite to solve the puzzles here. You may learn it elsewhere before coming back here to practise, though. Alternatively, you may google the relevant aspects while you try to solve the puzzles here.
If you’re just confused about how the puzzles on this website work, you may take a look at the explanation here: https://www.codingame.com/faq
[Don’t post the full solution.]
i am trying this puzzle using c++.I have not getting solution for this.
Hi , got a problem resolving this problem in PHP,
when i used $n to know if temperature have been sent , it work 1 time / 2.
![]()
Which cases have you failed, and what error messages have you got?
The “-273 alone” always wrong ( no error message ) , should i send my code ?
I don’t know what numbers that validator contains, but does your code work correctly if -273 is the only number given, or is one of the given numbers?
yeah , i think it should …
At first glance I thought this was more complex than it was, and then once done I was grateful for the reminder on trying to find the less complex solution, thank you!
Hi! The following program is evidently wrong (i.e. try (
[10,10] for temperatures), even though it gets 100%. I suggest adding validation case [10,10].
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
int main()
{
// the number of temperatures to analyse
int result=10000;
int n;
scanf("%d", &n);
if(!n) result=0;
for (int i = 0; i < n; i++) {
// a temperature expressed as an integer ranging from -273 to 5526
int t;
scanf("%d", &t);
if(abs(result)>abs(t)) result=t;
else if (abs(result)==abs(t)) result=-result*t/abs(t);
}
// Write an answer using printf(). DON'T FORGET THE TRAILING \n
// To debug: fprintf(stderr, "Debug messages...\n");
printf("%d\n",result);
return 0;
}
I can’t have the 100% because of “5526 Alone”. I don’t understand the problem…
Does your code work if the temperatures to compare consist of one or multiple instances of 5526 only?
Hello, I don’t understand the issue with “5526 alone” and “-273 alone”.
I’ve tested and these value should pass easily.
EDIT: Ok it doesn’t seem to depends on the second line input. It seems to be depending on the first line input.
Hi,
There is a misstake in the data with the “only negative numbers” condition, there is a positive 3 that bugs the tests. (expected 5 but got 3), see code below :
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int length = 0;
int max = 5527;
while (in.hasNext()) {
int n = in.nextInt();
System.err.println("n : " + n);
int nAbs = Math.abs(n);
System.err.println("nAbs : " + nAbs);
if (nAbs < max) {
max = nAbs;
}
length++;
}
System.out.println(max);
}
}
Output:
n : 3
nAbs : 3
n : -12
nAbs : 12
n : -5
nAbs : 5
n : -137
nAbs : 137
3 <=== (here result)
Failed
Found:
3
Expected:
-5
I also wonder if it is normal, for the test without temperatures, that “while (in.hasNext()) {}” condition is true?
Output :
n : 0
nAbs : 0
0