UESTC 1297 Bank 思维题

Bank

Time Limit: 1000/1000MS (Java/Others)     Memory Limit: 131072/131072KB (Java/Others)
Submit  Status

Given  X CNY (Chinese yuan), can you exchange it to exactly  Y CNY soon? The only way you have is to spend money at a vending machine 

once, which has an infinite number of copies of merchandise of all possible positive price.

However, you cannot make any assumptions about the way the vending machine will make change; it is only guaranteed that it will dispense 

banknotes/coins that sum to the amount of change required.

What’s the smallest total amount you have to spend in all case, to get a banknote/coin of  Y CNY?

Only  1 fen ( 0.01 CNY),  2 fen 5 fen 1 jiao ( 0.1 CNY),  2 jiao 5 jiao 1 yuan ( 1 CNY),  2 yuan 5 yuan 10 yuan 20 yuan 50 yuan 

and  100 yuan the vending machine will accept or return.

Input

The first line contains an integer  T , denoting the number of the test cases.

T100 ;

Each test case contains two number  X X and  Y Y.

X,Y{0.01,0.02,0.05,0.1,0.2,0.5,1,2,5,10,20,50,100}                    and  X>Y .

Output

For each test case, output a number denoting the answer.

Sample input and output

Sample Input Sample Output
2
0.05 0.01
0.1 0.01
0.02
0.07

Hint

只要找给你的钱能凑出Y就行

小数点后不要输出多余的位数,比如0.2不要输出0.20

对于任意X,假如你的答案是Z 那么对于X-Z的任意一种组合,都要能凑出Y

The sample input: If you spend 0.01,you may get 0.02 and 0.02,and can't get 0.01 anymore.

The answer is 0.02,in this case, you can get 0.01,0.01,0,01 or 0.01 0.02.

You can get 0.01 anyway.

Source

第十四届电子科技大学程序设计竞赛暨西南地区高校邀请赛 Contest Preliminary B - Bank

1297 Bank in Problems list

My Solution

当时这题挂了,一直找不出原因(┬_┬),对应x = 100,y = 0.01的情形我总结出的结论不对,本来以为y = 0.01时,100,10,1,0.1都是可以

当作x = 0.1处理的,然而并不是,当时确实应当都试试,反正其它地方找不出规律,555555……too young toosimple啊

现在知道了,如果按照原来的结论x = 100,y = 0.01,只花0.07的话,剩余99.93,但是这个数可以只用0.05和0.02凑出,

比如4个0.02和一个0.05就可以凑出0.13。

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
    //freopen("a.txt", "r", stdin);
    int T;
    double x, y;
    scanf("%d", &T);
    while(T--){
        scanf("%lf%lf", &x, &y);//cout<<x<<" "<<y<<endl;
        //!0.01
        if(y == 0.01){
            if(x == 0.02) printf("0.01\n");
            else printf("%.2f\n", x - 0.03);

        }
        //!0.1
        else if(y == 0.1){
            //cout<<"here"<<endl;
            if(x == 0.2) printf("0.01\n");
            else printf("%.2f\n", x-0.39);
        }

        //!1
        else if(y == 1){
            if(x == 2) printf("0.01\n");
            else printf("%.2f\n", x - 3.99);
        }
        //!10
        else if(y == 10){
            if(x == 20) printf("0.01\n");
            else printf("%.2f\n", x - 39.99);
        }
        else printf("0.01\n");
    }
    return 0;
}

Thank you all!


你可能感兴趣的:(ACM,ConTest,Preliminary,暨西南地区高校邀请赛)