The Salary Day

Question: Every 8th is the most expected day for the staff at the POJ company, because they will receive their salaries which enable them to raise up their families. On the contrary, clerks at financial department are up to their eyes at the work. Simon, who works for the financial department, is recently racking his brains to solve a problem. If the salary of each worker is known, how many peices of notes do they need  in minimum so that the workers do not need to give the changes?

Here we suppose the salary of each worker is an integer, and there are 6 par values of notes in total: 1, 2, 5, 10, 50, 100.

Solution:

There are 3 possible situations in total:

1. The salary(we wil describe it as "w") equals to a certain par value of note. 

In this situation, we will simply add 1 to the sum(we will describe it as "x") of notes.

2."w" is bigger than a certain type of note but smaller than the other.

We can take w=23 for instance. 23 is greater than 10 but less than 50. We shall use some mathematical techniques to solve this situation, which will be exhibited in a paragraph of codes afterwards.

3."w" is greater than any par value of note.

The solution to this situation is as same as the 2nd situation. Now, let us get on with our mathermatical techniques!

int function(int w)
{
    int x=0,A[]={1,2,5,10,50,100};
    for (int i=0; i<=5; i++)
    {
        if (w==A[i])
        {
            x++;
            break;
        }
        else if ((i<5 && w>A[i] && w<A[i+1]) || (i==5 && w>A[i]))
        {
            for (int j=i; j>=0; j--)
                {
                    int m=w/A[j];
                    x=x+m;
                    w=w%A[j];
                }
        }
    }
    return x;
}

With this tailored codes, I believe you can solve the problem at once! Keep working on, my mate!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值