UVA12468 Zapping【水题】

I’m a big fan of watching TV. However, I don’t like to watch a single channel; I’m constantly zapping between different channels.
    My dog tried to eat my remote controller and unfortunately he partially destroyed it. The numeric buttons I used to press to quickly change channels are not working anymore. Now, I only have available two buttons to change channels: one to go up to the next channel (the △ button) and one to go down to the previous channel (the ▽ button). This is very annoying because if I’m watching channel 3 and want to change to channel 9 I have to press the △ button 6 times!
    My TV has 100 channels conveniently numbered 0 through 99. They are cyclic, in the sense that if I’m on channel 99 and press △ I’ll go to channel 0. Similarly, if I’m on channel 0 and press ▽ I’ll change to channel 99.
    I would like a program that, given the channel I’m currently watching and the channel I would like to change to, tells me the minimum number of button presses I need to reach that channel.
Input
The input contains several test cases (at most 200).
    Each test case is described by two integers a and b in a single line. a is the channel I’m currently watching and b is the channel I would like to go to (0 ≤ a, b ≤ 99).
    The last line of the input contains two ‘-1’s and should not be processed.
Output
For each test case, output a single integer on a single line — the minimum number of button presses needed to reach the new channel (Remember, the only two buttons I have available are △ and ▽).
Sample Input
3 9
0 99
12 27
-1 -1
Sample Output
6
1
15

问题链接UVA12468 Zapping
问题简述:(略)
问题分析
    电视频道号是0-99,共100个频道,其中0和99号频道相邻(即循环),遥控器只能按上下键。给定当前频道和目标频道,求最少要按几下遥控器?
    需要考虑按键有2个方向。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA12468 Zapping */

#include <bits/stdc++.h>

using namespace std;

const int N = 100;

int main()
{
    int a, b;
    while(~scanf("%d%d", &a, &b) && (a != -1 || b != -1)) {
        if(a > b) swap(a, b);
        printf("%d\n", min(b - a, a - b + N));
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值