SZU : A18 (Climb Well)

本文介绍了一道经典的算法题目——青蛙爬井问题。通过给定的白天上升高度、夜间下滑高度及井深,计算青蛙爬出井口所需的天数。文章提供了一段C语言实现的代码,并对特殊情况进行了说明。

Judge Info

  • Memory Limit: 32768KB
  • Case Time Limit: 10000MS
  • Time Limit: 10000MS
  • Judger: Number Only Judger

Description

One day frog Frank fall into a deep well, the well is very deep. Everyday Frank try to climb up, at day time, he can get himself A\, feet up, but when he fall sleep at night , he slipped B\,feet down. The well is L feet deep.

Task

Now, it is your turn. Frank is asking you to find out, how many days needed for him to get out from the well. A day has two part, day time and night time. Frank must get higher than the well, or it is not count as get out.

Input

The first line of input contains T(1 \leq T \leq 100), the number of test cases. There is one line for each test case, contains three integer numbers A,B,L(0 \leq A,B,L \leq 2^{31}-1) as describe above.

Output

For each test case, print a line contains the solution, how many days need for Frank to get out from the well.If there is no way out, please output −1.

Sample Input

2
2 1 2
1 1 2

Sample Output

2
-1

 

被误解的思路: 

本以为 A 2 B 1 L 2  一天就可以爬过去,原来 刚好 A = L 是过不去井口的。所以要 > 井口才可以出的去。

 

 

代码:

 1 #include<stdio.h>
 2  
 3 int main()
 4 {
 5     int A,B,L;
 6     int t;
 7     scanf("%d",&t);
 8     while(t>0)
 9     {
10         scanf("%d%d%d",&A,&B,&L);
11         if(A<=B)
12         {
13             if(A<=L)
14                 printf("-1\n");
15             else
16                 printf("1\n");
17         }
18         else
19         {
20             if(A>L)
21                 printf("1\n");
22             else
23                 printf("%d\n",(int)((L-A)/(A-B))+2);
24         }
25         t--;
26     }
27  
28  
29 return 0;
30 }

 

 

 

 

 

 

转载于:https://www.cnblogs.com/firstrate/p/3176457.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值