zoukankan      html  css  js  c++  java
  • 水题——A. Gabriel and Caterpillar

    A. Gabriel and Caterpillar
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height h1 cm from the ground. On the height h2 cm (h2 > h1) on the same tree hung an apple and the caterpillar was crawling to the apple.

    Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by a cm per hour by day and slips down by b cm per hour by night.

    In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.

    Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple.

    Input

    The first line contains two integers h1, h2 (1 ≤ h1 < h2 ≤ 105) — the heights of the position of the caterpillar and the apple in centimeters.

    The second line contains two integers a, b (1 ≤ a, b ≤ 105) — the distance the caterpillar goes up by day and slips down by night, in centimeters per hour.

    Output

    Print the only integer k — the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.

    If the caterpillar can't get the apple print the only integer  - 1.

    Examples
    input
    10 30
    2 1
    output
    1
    input
    10 13
    1 1
    output
    0
    input
    10 19
    1 2
    output
    -1
    input
    1 50
    5 4
    output
    1
    Note

    In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.

    Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day.

    题意:小明两点钟在树上看到一条毛毛虫在h1的位置,在h2的位置有一个苹果,现在知道毛毛虫白天每小时可以爬a cm,晚上可以爬b cm。白天指的是早上十点到晚上十点,其余则是晚上。小明每天下午两点都会来看一下,问第几天小明可以看到毛毛虫吃到了苹果。

    题解:对第0天先处理,因为在小明第一次看到毛毛虫的这一天毛毛虫白天只爬了八个小时。同时还要在第0天判断毛毛虫是否白天可以比晚上爬的多,不然就一定爬不上去啦。  之后就可以用while循环,因为小明每天都是下午两点钟去看的,所以先算白天前面四个小时,然后判断是不是吃到了苹果,再加上后面白天的八个小时,这时也要判断,因为毛毛虫吃到了苹果就不会下来了。同时还要加一个标记,判断毛毛虫是小明去之前吃到的苹果还是之后,因为之后吃到苹果的话天数要+1(第二天才能看到)。

     1 /*A*/
     2 #include<cstdio>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     int a,b;
     8     int x,y;
     9     while(scanf("%d%d",&a,&b)!=EOF)
    10     {
    11         scanf("%d%d",&x,&y);
    12         int temp=a;
    13         temp+=8*x;
    14         if(temp>=b)
    15         {
    16             printf("0
    ");
    17             continue;
    18         }
    19         temp-=12*y;
    20         if((temp+4*x)<=a)
    21         {
    22             printf("-1
    ");
    23             continue;
    24         }    
    25         //printf("%d
    ",temp);/**/
    26         
    27         int d=1;
    28         int flag=0; 
    29         while(1)
    30         {
    31             temp+=4*x;
    32             if(temp>=b)
    33             {
    34                 break;
    35                 flag=0;
    36             }
    37             temp+=8*x;
    38             if(temp>=b)
    39             {
    40                 break;
    41                 flag=1;
    42             }
    43             //printf("bai %d
    ",temp);/**/
    44             
    45             temp-=12*y;
    46             //printf("ye %d
    ",temp);/**/
    47             d++;
    48         }
    49         if(flag==0)
    50             printf("%d
    ",d);
    51         else
    52             printf("%d
    ",d--);
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    OpenGL中FrameBuffer使用
    每天进步一点点>结构化异常处理(SEH)
    js操作cookies
    [转]高性能网站优化与系统架构
    正则-匹配超链接地址及内容
    在c#.net中操作XML
    ActionScript 3 step by step (6) 元标记
    Facebook CEO:终极目标并非出售或上市
    ActionScript 3 step by step (3) 事件处理
    ActionScript 3 step by step (2) 使用Trace()跟踪输出
  • 原文地址:https://www.cnblogs.com/yepiaoling/p/5330229.html
Copyright © 2011-2022 走看看