奇怪的纸币

奇怪的纸币

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值