Quick Change

Quick Change

时间: 1ms        内存:64M

描述:

J.P. Flathead's Grocery Store hires cheap labor to man the checkout stations. The people he hires (usually high school kids) often make mistakes making change for the customers. Flathead, who's a bit of a tightwad, figures he loses more money from these mistakes than he makes; that is, the employees tend to give more change to the customers than they should get. Flathead wants you to write a program that calculates the number of quarters ($0.25), dimes ($0.10), nickels ($0.05) and pennies ($0.01) that the customer should get back. Flathead always wants to give the customer's change in coins if the amount due back is $5.00 or under. He also wants to give the customers back the smallest total number of coins. For example, if the change due back is $1.24, the customer should receive 4 quarters, 2 dimes, 0 nickels, and 4 pennies.

输入:

The first line of input contains an integer N which is the number of datasets that follow. Each dataset consists of a single line containing a single integer which is the change due in cents, C, (1 <= C <= 500).

输出:

For each dataset, print out the dataset number, a space, and the string: Q QUARTER(S), D DIME(S), n NICKEL(S), P PENNY(S) Where Q is he number of quarters, D is the number of dimes, n is the number of nickels and P is the number of pennies.

示例输入:

3
124
25
194

示例输出:

1 4 QUARTER(S), 2 DIME(S), 0 NICKEL(S), 4 PENNY(S)
2 1 QUARTER(S), 0 DIME(S), 0 NICKEL(S), 0 PENNY(S)
3 7 QUARTER(S), 1 DIME(S), 1 NICKEL(S), 4 PENNY(S)

提示:

参考答案(内存最优[748]):

#include"stdio.h"
int main()
{
    int n,m,a,b,c,d;
    int k;
    scanf("%d",&n);
    for(k=1; k<=n; k++)
    {
        scanf("%d",&m);
        a=m/25;
        b=(m-a*25)/10;
        c=(m-a*25-b*10)/5;
        d=m%5;
        printf("%d %d QUARTER(S), %d DIME(S), %d NICKEL(S), %d PENNY(S)\n",k,a,b,c,d);
    }
    return 0;
}

参考答案(时间最优[0]):

#include <stdio.h>
int main()
{
    int T,n,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        printf("%d %d QUARTER(S), %d DIME(S), %d NICKEL(S), %d PENNY(S)\n",cas++,n/25,n%25/10,n%25%10/5,n%25%10%5);
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注