Pages

Place in a Class


Place in a Class



Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numbered from 1 to m starting from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture).
The organizers numbered all the working places from 1 to 2nm. The places are numbered by lanes (i. e. all the places of the first lane go first, then all the places of the second lane, and so on), in a lane the places are numbered starting from the nearest to the blackboard (i. e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right.

The picture illustrates the first and the second samples.

Santa Clause knows that his place has number k. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right!
   

Program:

import java.util.Scanner;

public class PlaceInClass {
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter n : ");
                int n = sc.nextInt();
                System.out.print("Enter m : ");
                int m = sc.nextInt();
                System.out.print("Enter k : ");
                int k = sc.nextInt();

                System.out.println(findPlaceInClass(n, m, k));
}

public static String findPlaceInClass(int n, int m, int k){
int count = -1;
char position = (k%2!=0) ? 'L' : 'R';
int lane = 0, desk = 0;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
count = count + 2;
if(count == k || count+1 == k){
lane = i;
desk = j;
break;
}
}
}
return lane+" "+desk+" "+position;
}
}




Output:


Enter n : 2

Enter m : 4

Enter k : 4

Place : 1    2    R







No comments:

Post a Comment