UVA Problem 12468 ( Zapping ) Solution

UVA Problem 12468 ( Zapping ) Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

Another easy problem. The problem mainly deals with changing channel in tv using only up and down button. So there are only 2 directions. We are to find the least key press from given channel (Input 1) to Channel (Input 2) in any direction up or down. The output should be the least number of key pressed to move from given channel to another given channel.

The program will keep running until it find two -1. It will mark the end of input. There will be two input per line. Both inputs will be a channel ranging within 0 to and including 99 ( 0<= a,b <=99 ).First we can easily see from 0 to 99 there are 100 channels. So the most key press either using up or down button will be 50. It is always same for this range.

For example, If input is:

10 90

here if we press the up button on remote it will take 80 press to reach but if we keep pressing down key it will only take 20 press. Why? because it is mentioned in the problem that we can reach to 0 from channel 99 also the inverse is true we can reach 99 from channel 0. So our answer should be 20 because the problems wants the least number of key presses.

Important: 

The most key press in any direction is 50 for range 0 to 99 ( try to think ).

Solving Technique:

We can easily find the distance ( or number of key press ) between two channels by subtracting one from another. Here I have used abs() absolute function so even if subtract the larger number from the smaller I’ll always get a positive value. Now i check if the subtraction result is smaller than 50 then it is the least number of key press in any direction. But if its greater than 50 then we are going the wrong direction. We subtract the result from the total number of channels ( here total 100 ) to find the least number of key presses in the opposite direction.


Input:

3 9
0 99
12 27
-1 -1

Output:

6
1
15

Code:

/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 */

#include<stdio.h>
#include<stdlib.h>

int main(){
    int a, b, i, s;
    while (scanf("%d%d", &a, &b) == 2 && (a != -1 && b != -1)){
        s = abs(a - b);

        if(s > 50)
            s = 100 - s;

        printf("%d\n", s);
    }
    return 0;
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s