Number steps
Time limit:1 second Memory limit:32768 KB
Starting from point (0, 0) on a plane, we have written all non-negative
Integers 0, 1, 2,... as shown in the figure. For example, 1, 2, and 3 has been
Written at points (1, 1), (2, 0), and (3, 1) respectively and this pattern has
Continued.
You are to write a program that reads the coordinates of a point (x, y), and
Writes the number (if any) that has been written at that point. (x, y)
Coordinates in the input are in the range 0... 5000.
Input
The first line of the input is N, the number of test
Cases for this problem. In each of the N following lines, there is X, and Y
Representing the coordinates (x, y) of a point.
Output
For each point in the input, write the number
Written at that point or write no number if there is none.
Sample Input
3
4 2
6 6
3 4
Sample output
6
12
No number
Answer: View code
Int Main ()
{
Int N, I;
Scanf ( " % D " , & N ); // N indicates the number of test data in the input file.
Int X, Y;
For (I = 0 ; I < N; I ++ ) // Process n test data in the input file
{
Scanf ( " % D " , & X, & Y ); // Each test data contains two integers.
Int Num; // Non-negative integer at the (x, y) coordinate point
If (Y ! = X && Y ! = X - 2 )
{
Num = - 1 ;
}
Else
{
If (Y = X && X % 2 = 0 )
{
Num = 2 * X;
}
Else If (Y = X && X % 2 ! = 0 )
{
Num = 2 * X - 1 ;
}
Else If (Y = X - 2 && X % 2 = 0 )
{
Num = 2 * X - 2 ;
}
Else
{
Num = 2 * X - 3 ;
}
}
If (Num = - 1 ) Printf ( " No number \ n " );
Else Printf ( " % D \ n " , Num );
}
Return 1 ;
}