zoukankan      html  css  js  c++  java
  • Poor Warehouse Keeper

    Poor Warehouse Keeper

    http://acm.hdu.edu.cn/showproblem.php?pid=4803

    Jenny is a warehouse keeper. He writes down the entry records everyday. The record is shown on a screen, as follow:


    There are only two buttons on the screen. Pressing the button in the first line once increases the number on the first line by 1. The cost per unit remains untouched. For the screen above, after the button in the first line is pressed, the screen will be:


    The exact total price is 7.5, but on the screen, only the integral part 7 is shown.
    Pressing the button in the second line once increases the number on the second line by 1. The number in the first line remains untouched. For the screen above, after the button in the second line is pressed, the screen will be:


    Remember the exact total price is 8.5, but on the screen, only the integral part 8 is shown. 
    A new record will be like the following:


    At that moment, the total price is exact 1.0.
    Jenny expects a final screen in form of:


    Where x and y are previously given.
    What’s the minimal number of pressing of buttons Jenny needs to achieve his goal?

     
    Input
    There are several (about 50, 000) test cases, please process till EOF.
    Each test case contains one line with two integers x(1 <= x <= 10) and y(1 <= y <= 109) separated by a single space - the expected number shown on the screen in the end.
     
    Output
    For each test case, print the minimal number of pressing of the buttons, or “-1”(without quotes) if there’s no way to achieve his goal.
     
    Sample Input
    1 1
    3 8
    9 31
     
    Sample Output
    0
    5
    11
    Hint
    For the second test case, one way to achieve is: (1, 1) -> (1, 2) -> (2, 4) -> (2, 5) -> (3, 7.5) -> (3, 8.5)
     
    Source

    直接贪心模拟即可

     1 #include<iostream>
     2 #include<cmath>
     3 #include<vector>
     4 #include<cstring>
     5 #include<string>
     6 #include<algorithm>
     7 #include<cstdio>
     8 #include<map>
     9 #include<queue>
    10 #define maxn 200005
    11 #define mem(a,b) memset(a,b,sizeof(a))
    12 typedef long long ll;
    13 using namespace std;
    14 
    15 
    16 int main(){
    17     double a,b,x,y,tmp;
    18     while(cin>>x>>y){
    19         a=1,b=1;
    20         int step=0;
    21         if(x>y){
    22             cout<<-1<<endl;
    23         }
    24         else{
    25             double danjia=(y+0.9999)/x;
    26             double danjia_double=danjia;
    27             int tmp;
    28             while(a<x&&b<y){
    29                 if(danjia_double-b-1>=0.000001){
    30                     tmp=int(danjia_double-b);
    31                     step+=tmp;
    32                     b+=tmp;
    33 
    34                 }
    35                 else{
    36                     step++;
    37                     b+=b/a;
    38                     a++;
    39                     danjia_double=a*danjia;
    40                 }
    41               //  cout<<a<<" "<<b<<" "<<danjia_double<<" "<<step<<endl;
    42             }
    43            if((danjia_double-b+0.00001)>0){
    44                 step+=danjia_double-b;
    45             }
    46             cout<<step<<endl;
    47         }
    48     }
    49 }
    View Code
  • 相关阅读:
    OpenCV_Python —— (6)图像色彩空间
    OpenCV_Python —— (5)图像模糊/平滑/滤波
    Java 14 祭出增强版 switch,真香!!
    推荐 9 个 爱不释手的 JSON 工具!
    从 0 开始手写一个 Mybatis 框架,三步搞定!
    Java常用的几个Json库,性能强势对比!
    Oracle JDK 和 OpenJDK 有什么区别?
    极客时间-左耳听风-程序员攻略-UI/UX设计
    OpenCV导向滤波(引导滤波)实现(Guided Filter)代码,以及使用颜色先验算法去雾
    python面向对象小练习
  • 原文地址:https://www.cnblogs.com/Fighting-sh/p/9774711.html
Copyright © 2011-2022 走看看