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!