C語言中拆分人民幣面值的題

目前人民幣共有以下幾種面值(不包括角和分):
1,2,5,10,20,50,100元。
編寫一個函數計算金額“X”需要多少張面值爲n元的紙幣。在主函數中輸入一個金額值,調用該函數,計算組成該金額最少需要多少張紙幣。

#include "stdio.h"
main()
{
    long money,a,b,c;
    printf("input number of money :\n");
    scanf("%ld",&money);
    a=money/100;     /* 求面值100$的張數a   */
    b=money%100/10;  /* 求面值十位上的數字b */
    c=money%10;      /* 求面值個位上的數字c */
    printf("%ld$ -> ",money);  /* 輸出標題頭 */
    if(a>0)
        printf("%ld 100$\t",a);
    if(b>=5)
    {
        printf("1 50$\t");
        if((b-5)%2==0)
            printf("%ld 20$\t",(c-5)/2);
        else if(c-5>1)
            printf("1 20$\t1 10$\t");
        else
            printf("1 10$\t");
    }
    else
    {
        if(b%2==0)
            printf("%ld 20$\t",c/2);
        else if(b>1)
            printf("1 20$\t1 10$\t");
        else
            printf("1 10$\t");
    }
    if(c>=5)
    {
        printf("1 5$\t");
        if((c-5)%2==0)
            printf("%ld 2$\t",(c-5)/2);
        else if(c-5>1)
            printf("1 2$\t1 1$\t");
        else
            printf("1 1$\t");
    }
    else
    {
        if(c%2==0)
            printf("%ld 2$\t",c/2);
        else if(c>1)
            printf("1 2$\t1 1$\t");
        else
            printf("1 1$\t");
    }
    printf("\n");
    getch();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章