CCF NOI1034 钞票兑换

问题链接CCF NOI1034 钞票兑换




时间限制: 1000 ms  空间限制: 262144 KB

题目描述

  将任意给定的整百元钞票,兑换成10元、20元、50元小钞票形式。输出兑换方案总数。

输入

  输入需要兑换的钞票总数n。

输出

  输出方案总数。

样例输入

100

样例输出

10

数据范围限制

  100<=n<=1000000




问题分析

  这是一个组合问题,可以用穷举法来解决

  根据输入的n,可以算出50元钞票的最多张数,然后假设50元钞票的张数为i,计算所有的组合。

  其实,假定50元的钞票有i张,那么这种情况下的方案数就能算出来了。如果全部都用试探法去计算,则会出现超时的情况。

  “钞票总数”的说法容易令人误解,说金额要好理解一些。

程序说明

  (略)

要点详解
  • 先用数学思考一下,然后再用程序的方法解决。



参考链接:(略)。

100分通过的C语言程序:

#include <stdio.h>

#define BILL50 50
#define BILL20 20
#define BILL10 10

int main(void)
{
    int n, count, i, end;

    scanf("%d", &n);

    count = 0;
    end = n / BILL50;
    for(i=0; i<=end; i++)
        count += (n - i * BILL50) / BILL20 + 1;

    printf("%d
", count);

    return 0;
}


80分LTE(超时)的C语言程序:

#include <stdio.h>

#define BILL50 50
#define BILL20 20
#define BILL10 10

int main(void)
{
    int n, count, i, j, end1, end2;

    scanf("%d", &n);

    count = 0;
    end1 = n / BILL50;
    for(i=0; i<=end1; i++) {
        if(i * BILL50 == n) {
            count++;
            continue;
        }
        end2 = (n - i * BILL50) / BILL20;
        for(j=0; j<=end2; j++) {
            if(i * BILL50 + j * BILL20 == n) {
                count++;
                continue;
            }
            if((n - i * BILL50 - j * BILL20) % BILL10 == 0)
                count++;
        }
    }

    printf("%d
", count);

    return 0;
}



原文地址:https://www.cnblogs.com/tigerisland/p/7563910.html