IPSC Logo

Internet Problem Solving Contest

IPSC 2008

Problem C – Comparison Mysteries

For beginners in programming it often comes as a great surprise when numbers inside a computer refuse to behave the same way numbers in mathematics do. In this task, we will investigate two such cases.

The ideas needed to solve this task are neither platform-specific, nor language-specific. However, due to many subtle differences between various programming languages the best way to state this task was to pick a single language. In this case that language will be Java.

We will be using numeric variables only, and only the following six basic types:

For each of these types, the Scanner class has a corresponding method (e.g., nextByte for byte) that scans the next token of the input as the given type.

Easy data set

Obviously, in mathematics the only solution to x = -x is zero. Now consider the following Java code:

Java code template

import java.util.*;
public class C1 {
 public static void main(String args[]) {
 Scanner sc = new Scanner(System.in); sc.useLocale(Locale.US);
 ????? x = sc.?????();
 System.out.println( (x==-x) + " " + (x!=0) );
}
}

We would like you to show that there are cases when a non-zero number is equal to its own negation. Choose the type of the variable x and provide input to this program so that it outputs “true true”.

Submit your answer as a text file with two lines. In the first line, write the type of the variable x, in the second line a string that shall be used as the input for your program.

An example submission follows. This submission is syntactically correct, but it does not produce the desired output.

Your submission

short
47

The corresponding Java code

import java.util.*;
public class C1 {
 public static void main(String args[]) {
 Scanner sc = new Scanner(System.in); sc.useLocale(Locale.US);
 short x = sc.nextShort();
 System.out.println( (x==-x) + " " + (x!=0) );
}
}

Its output on your input

false true

Hard data set

An even more obvious fact is that whenever x = y and y = z, we must have x = z as well. Now consider the following Java code:

Java code template

import java.util.*;
public class C2 {
 public static void main(String args[]) {
 Scanner sc = new Scanner(System.in); sc.useLocale(Locale.US);
 ????? x = sc.?????();
 ????? y = sc.?????();
 ????? z = sc.?????();
 System.out.println( (x==y) + " " + (y==z) + " " + (x==z) );
}
}

We would like you to show that there are cases when equality is not transitive. Choose the types of the variables x, y, and z and provide input to this program so that it outputs “true true false”.

Submit your answer as a text file with six lines. In the first three lines, write the types of the variables x, y, and z. In the second three lines write the strings that shall be used as the input for your program.

An example submission follows. This submission is syntactically correct, but it does not produce the desired output.

Your submission

short
short
int
47
47
47

The corresponding Java code

import java.util.*;
public class C2 {
 public static void main(String args[]) {
 Scanner sc = new Scanner(System.in); sc.useLocale(Locale.US);
 short x = sc.nextShort();
 short y = sc.nextShort();
 int z = sc.nextInt();
 System.out.println( (x==y) + " " + (y==z) + " " + (x==z) );
}
}

Its output on your input

true true true

Applets

For easy experimentation we provide an applet for each of the subproblems, where you can set variable types and values, and let the applet produce the output for you.

Java documentation

This section is not strictly necessary to solve the problem. But in case someone feels the need to browse the Java documentation to check something out, we provide links to the online version of The Java Language Specification, Third Edition you might consider useful: