奇怪的纸币
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
大家都知道人民币的面值有1元,2元,5元。这是因为1、2、5三个都是质数,可以合理地组合成其他数字。其中除了8和9需要3个数字才能组合成功外, 10以内的其他数字都可以由1、2、5中的1个或者2个组合。另外,人民币因为配备了10,所以10-2=8,10-1=9,这就完美解决了8和9的问题。由此一来,10以内所有的数字都在2张人民币以内就可以得到解决。
小明忽然想到1、5、7也同样都是质数,那么用这些面值的纸币组成某个数最小需要多少张纸币呢?
Input
一个数字 n(1 <= n <= 100000)
Output
一个数字,代表最少需要多少张面值 1 或 5 或 7 的纸币构成。
Sample Input
10
Sample Output
2
Hint
Source
7989
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int t1;
int n;
int t2=0;
int a[100001];
void dfs()
{
queue<int>q;
q.push(7);
q.push(5);
q.push(1);
int x=0;
while(!q.empty())
{
int u=q.front();
q.pop();
while(x<n)
{
x+=u;
t1++;
if(x>n)
{
t1--;
x-=u;
break;
}
}
if(x==n)
return;
}
}
void ddfs()
{
queue<int>q;
q.push(5);
q.push(1);
int x=0;
while(!q.empty())
{
int u=q.front();
q.pop();
while(x<n)
{
int p=n-x;
if(p%7==0)
{
t2+=p/7;
x=n;
break;
}
x+=u;
t2++;
if(x>n)
{
t2--;
x-=u;
break;
}
}
if(x==n)
return;
}
}
int main()
{
cin>>n;
t1=0;
t2=0;
dfs();
ddfs();
if(t1>t2)
printf("%d\n",t2);
else printf("%d\n",t1);
return 0;
}