Steps

uva846:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=787

题意:给你两个数x,y,x<=y,现在让你从x走向y,开始的第一次和最后一次,只能够移动一步,之后每一次走的步数和上次走的步数之和之差不超过1.问你到达y最小需要多少步。

题解:从x,y同时开始走,每次步数加一,如果走到两者刚好相遇则停止,如果剩余的距离不超过当前的步数,也可以停止,因为这剩余的距离可以在之前的某个时间段走完,相当于在之前走了一个水平的阶段。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int x,y;
 7 int main(){
 8     int cas;
 9     scanf("%d",&cas);
10     while(cas--){
11           scanf("%d%d",&x,&y);
12           int counts=0,step=1;
13           while(1){
14               if(x>=y){
15                 printf("%d\n",2*counts);
16                 break;
17               }
18               if(y-x<=step){
19                 printf("%d\n",2*counts+1);
20                 break;
21               }
22               else{
23                 x+=step;
24                 y-=step;
25                 counts++;
26                 step++;
27               }
28           }
29     }
30 }
View Code

 

posted on 2014-07-14 18:34  天依蓝  阅读(175)  评论(0编辑  收藏  举报

导航