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 ▽).
我是个超级电视迷,但是我不喜欢固定看一个频道,我经常在不同频道间转来转去。
我的狗咬坏了我的遥控器,现在数字键都不能用了,只剩两个按钮可以换频道,一个往上切一个频道(△ 按钮),一个往下切一个频道(▽ 按钮)。这样真的很烦,因为如果我要从频道3 换到频道9 我得按6 次△ 按钮。
我的电视有100 个频道,号码为0 到99。它们是循环的,也就是从99 台再按一下△ 就会回到第0 台。同理,从第0 台按一下▽ 就会回到99 台。
帮我写个程式,让我输入现在正在看的频道和我要切过去的频道,它便告诉我最少需要按几次按钮。
Input
输入含有多笔测资 (最多 200 笔)。
每笔测资含有两个整数a 与b 于一行。 a 是我现在看的频道,而b 则是我要切过去的频道(0 <= a, b <= 99)。
最后一行有两个 -1,代表输入结束。
Output
对于每笔测资,输出一个整数于一行— 也就是我最少要按几次按钮才能切到新频道。 (记住,我只有△ 和▽ 两个按钮可用)。
Sample input and output
standard input
|
standard output
|
3
9
0 99 12 27 -1 -1 |
6
1 15 |
解题思路:,因为电视频道数从99到0只要按一个键,所以计算两数的差判断是否大于51再判断从哪个方向按到要求的频道
#include<stdio.h> int main() {int a,b,t; while(scanf("%d%d",&a,&b)!=EOF){ if(a==-1&&b==-1)break; if(a>b){t=a;a=b;b=t;} if(b-a<51)printf("%d\n",b-a); else if(b-a>=51)printf("%d\n",100-b+a); } return 0; }