Determine the weight of the split fruit

Hi there I currently finding a question in vjudge interesting so I start to make some code about the input and output and it turns out well until I submit my code it started to tell my answer is wrong.

to be honest I don see how I get it wrong when i doing it perfectly ok.
here the question:
"One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.

Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight."
the input
"The first (and the only) input line contains integer number w (1 ≤ w ≤ 100) — the weight of the watermelon bought by the boys."
the output
"Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case."
Note:(For example, the boys can divide the watermelon into two parts of 2 and 6 kilos respectively (another variant — two parts of 4 and 4 kilos).)
Examples
Input
8
Output
YES

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

int main()
{

  int watermelon;
  char ans[] ="YES";
  char ans1[]="NO";
  int	i;
  scanf("%d", &watermelon );
  		if(watermelon % 2 == 0 )
  		{
			printf("%s", ans);
		}
		else
		{
			printf("%s", ans1);
		}
	
	return 0;
}


it says my answer wrong on test 5.
it is because my code don't have any range of the weight or something else?
I appreciate any help.
and anyone here have use vjudge before and how does the correct/wrong system work there?
Last edited on
try
 
 scanf("%i", &watermelon ); 
Last edited on
@rjphares thank for the reply but it still wrong.
> and anyone here have use vjudge before and how does the correct/wrong system work there?
All the online judge type services basically do

gcc prog.c
a.out < test.txt > result.txt
diff correct.txt result.txt
if [ $? -ne 0 ]; then
    echo test fail
fi


So if your result is off by even a single character, you fail.
It's vitally important that you read and comprehend the actual requirements. Visually approximate answers that any reasonable human reader would take as being correct don't necessarily count.

Like for example, you don't print a newline after YES or NO.
I think SalemC is right: you just need the newline.

By the way, this is a good example of C++'s handy ?: operator:
http://www.cplusplus.com/articles/1AUq5Di1/

1
2
3
4
5
6
7
8
9
#include <cstdio>

int main()
{
    int weight;
    std::scanf("%d", &weight);
    printf("%s\n",
           weight % 2 ? "NO" : "YES");
}

@salem, @dhayden thank you for replying.

I didn't know there this ?: operator guess I need to research more.

and @dhayden I modify my code after saw your code but it is still not working in vjudge.
does that mean I need to find an alternative way?

BTW, I thought printf("%s", weight % 2 ? "NO" : "YES") statement 2 should true and statement 3 be false but it reserve.

or is it work only with this operator like: odd on the left, even on the right.

is newline mean a new method or is the meaning for only ?:?
(sorry I still not used to computer English yet)

P.S: curious kill a cat, and I want to be one.
I didn't know there this ?: operator guess I need to research more.
I don't use it much, but it's very handy when you need it.

I modify my code after saw your code but it is still not working in vjudge.
does that mean I need to find an alternative way?

That's very strange. Please post your current code. Can you post the test cases from vjudge also? The problem seems very simple and you seem to have the right answer.

BTW, I thought printf("%s", weight % 2 ? "NO" : "YES") statement 2 should true and statement 3 be false but it reserve.

I just removed the unnecessary comparison to zero from your code. I could have written
printf("%s\n", weight % 2 == 0 ? "YES" : "NO");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main()
{
    int watermelon = 0 ;

    if( scanf( "%d", &watermelon ) == 1 )
    {
        if( watermelon%2 == 0 && watermelon > 2 ) puts( "YES" ) ;
        else puts( "NO" ) ;
    }

    // else input error
}
JLBorges, the OP says that the input is guaranteed to be between 1 and 100 so I think the check for watermelon>2 isn't necessary.
That's very strange. Please post your current code. Can you post the test cases from vjudge also? The problem seems very simple and you seem to have the right answer.

this is my current code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

int main()
{

  int watermelon_weight;
  char ans[] ="YES";
  char ans1[]="NO";
  scanf("%d", &watermelon_weight );
  printf("%s\n", watermelon_weight % 2 == 0 ? "%s", ans : "%s", ans1);
//  	for(int i = 0; i<watermelon_weight; i++)
//  	{
//	
//  			if(watermelon_weight % 2 == 0 )
//  			{
//				printf("%s", ans);
//			}
//			else
//			{
//				printf("%s", ans1);
//			}
//
//	}
	return 0;
}


just right you say it correct answer but the system still just it wrong even we change the line.

as for the test case i not sure what you mean but i will just post full question on the first post.
Last edited on
@JLBorges code work succesful in vjudge and it make me speechless at first. but after seeing your code I understand that if the watermelon split evenly. but i don understand why it must >2? when I try to remove the >2 the system say it wrong.

is it because 1 calculated even number in system ?

JLBorges, the OP says that the input is guaranteed to be between 1 and 100 so I think the check for watermelon>2 isn't necessary.

in fact, that >2 make the code get accepted in system.
Last edited on
> each of the two parts weighs even number of kilos
You can't split 2 such that each part is even.
Topic archived. No new replies allowed.