TOJ 3976.Change(dp或找规律)

3 篇文章 0 订阅

题目链接:http://acm.tju.edu.cn/toj/showp3976.html



3976.    Change
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 530    Accepted Runs: 244



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



Source: TJU Team Selection 2013


 

找零钱问题,本以为要用dp,但貌似找规律也是一个办法,不过规律不好找,1~6比较特殊单独考虑,随后从7开始进入规律的循环了,自己推一下:从7开始每6个是一个循环,也就是加了个6,知道这些写起来就非常简单了。

 

#include <stdio.h>
int a[12]={1,2,1,2,1,1,2,2,2,2,2,2};
int main(){
	int t,n,sum;
	scanf("%d",&t);
	for(int i=0;i<t;i++){
		scanf("%d",&n);
		sum=0;
		while(n>12){
			n-=6;
			sum++;
		}
		sum+=a[n-1];
		printf("Case %d: %d\n",i+1,sum); 
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值