After Shawn sees the following picture, he decides to give up his career in IT and turn to sell fruits. Since Shawn is a lazy guy, he doesn’t like to do any extra work. When he starts selling fruits, he finds that he always needs to take changes for customers. He wants you to write a program to give the least number of changes. Shawn is also a strange guy, because he takes changes by 6,5,3,1.
Input
The first line of input is an integer T which indicates the sum of test case. Each of the following T lines contains an integer n(1≤n≤105), which is the total money Shawn need to give to the customer.
Output
For each test case, output "Case i: Result" in one line where i is the case number and Result is the least number of changes Shawn need to give customer.
Sample Input
3 4 10 19
Sample output
Case 1: 2 Case 2: 2 Case 3: 4
Hint
Case 1: 4=3+1 Case 2: 10=5+5 Case 3: 19=6+6+6+1
题意概述:找零钱问题,老生常谈了。题目中有4中面值的零钱,分别是:6,5,3,1 .要用找给客人的零钱数量最少。
解题思路:本来以为要用到动态规划,那块没学好,所以就偷了个懒,找了一下规律。11以下数量很容易得到,大于11是,用剩余面额减掉最大的,即6.小于等于11是,只需加上相应的数量即可。
源代码:
#include<iostream> using namespace std; int main() { int T,N; cin>>T; for(int i=1;i<=T;++i) { int min=0; cin>>N; while(N>11) { ++min; N-=6; } if(7<=N&&N<=11)min+=2; else if(5<=N&&N<=6)++min; else if(N==4)min+=2; else if(N==3||N==1)++min; else if(N==2)min+=2; cout<<"Case "<<i<<": "<<min<<endl; } return 0; }