ZeroJudge A518: Zapping

UVa 12468 – Zapping

I'm a huge TV fan, but I don't like sticking to one channel. I often switch between different channels.

My dog chewed up my remote control, and now the number buttons don't work. I'm left with only two buttons to change channels: one for going up (△ button) and one for going down (▽ button). It's really annoying because if I want to switch from channel 3 to channel 9, I have to press the △ button 6 times.

我的電視有 100 個頻道,號碼為 0 到 99。它們是循環的,也就是從 99 台再按一下 △ 就會回到第 0 台。同理,從第 0 台按一下 ▽ 就會回到 99 台。

Can you help me write a program that, given the current channel I'm watching and the channel I want to switch to, tells me the minimum number of button presses needed?

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The input consists of multiple test cases, terminated by EOF (End of File). Up to 200 test cases may be present.
Each test case consists of a single line containing two integers, a and b. a represents the current channel, and b represents the channel to switch to. Both a and b are non-negative integers not exceeding 99.
The last line consists of two -1 integers and should not be processed.
For each set of data, output an integer on a single line, representing the minimum number of button presses required to switch to the new channel. (Remember, I only have two buttons available: △ and ▽).
3 9
0 99
12 27
-1 -1
6
1
15

Thought Process

Calculate both the values of "△" and "▽", then compare them to find the smaller value and output that smaller value.

Sample Code-ZeroJudge A518: Zapping

#include <iostream>
using namespace std;

int main() {
    int a, b;
    while (cin >> a >> b)
    {
        if (a == -1 && b == -1) break;
        else
        {
            if (a > b) swap(a, b);
            int one = b - a;
            int two = a - b + 100;
            cout << min(one, two) << endl;
        }
    }
}

//ZeroJudge A518
//Dr. SeanXD

Comments