/*
 用给定的几种钱币凑成某个钱数,一般而言有多种方式。
 * 例如:给定了6种钱币面值为2、5、10、20、50、100,用来凑15元,
 * 可以用5个2元、1个5元,或者3个5元,或者1个5元、1个10元,等等。
 * 显然,最少需要2个钱币才能凑成15元。 
 * 任务: 用户输入若干个互不相同的钱币面值和要凑成的钱数,编程计算,最少需要多少个钱币才能凑成用户给出的钱数
*/using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            bool b = true;
            int[] moneyArr = new int[1];    //用来存放用户输入的钱币
            List<int> moneyList = new List<int>();  //存放凑成钱数的每个钱币
            int moneySum = 0;   //保存用户要凑成的钱数
            Console.WriteLine("请输入若干个互不相同的钱币面值(各个面值之间用空格分开):");
            do
            {
                string mStr = Console.ReadLine();
                if (mStr.Trim().Length.Equals(0))   //判断用户是否输入了钱币
                    continue;                string[] moneyStr = mStr.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                b = false;
                moneyArr = new int[moneyStr.Length];
                for (int i = 0; i < moneyStr.Length; i++)
                {
                    foreach (char c in moneyStr[i].ToCharArray())   //检查用户输入的钱币每一个char是否正确,48->0,57->9
                    {
                        if (Convert.ToInt16(c) < 48 || Convert.ToInt16(c) > 57)
                        {
                            b = true;
                            Console.WriteLine("你的输入有误,请重新输入若干个互不相同的钱币面值(各个面值之间用空格分开):");
                            break;
                        }
                    }
                    if (b)
                        break;
                    moneyArr[i] = Convert.ToInt16(moneyStr[i]);
                }
            } while (b);    //当用户输入钱币全部正确的时候才跳出循环            for (int i = 0; i < moneyArr.Length; i++)   //正宗冒泡,小->大
            {
                for (int j = 0; j < moneyArr.Length - i - 1; j++)
                {
                    if (moneyArr[j] > moneyArr[j + 1])
                    {
                        int temp = moneyArr[j];
                        moneyArr[j] = moneyArr[j + 1];
                        moneyArr[j + 1] = temp;
                    }
                }
            }            Console.WriteLine("请输入将要凑成的钱数:");
            do
            {
                string money = Console.ReadLine();  //输入要凑成的钱数
                if (money.Trim().Length.Equals(0))  //判断用户是否输入了正确的钱数
                    continue;
                b = false;
                foreach (char c in money.Trim().ToCharArray())  //检查用户输入的钱币每一个char是否正确,48->0,57->9
                {
                    if (Convert.ToInt16(c) < 48 || Convert.ToInt16(c) > 57)
                    {
                        b = true;
                        Console.WriteLine("你的输入有误,请重新输入将要凑成的钱数:");
                        break;
                    }
                }
                if (!b)
                {
                    moneySum = Convert.ToInt16(money);
                    if (moneySum < moneyArr[0])     //判断要凑成的钱是否小于钱币中的最小值
                    {
                        b = true;
                        Console.WriteLine("你的输入的将要凑成的钱数小于组合币种的最小面值,请重新输入将要凑成的钱数:");
                    }
                }
            } while (b);    //用户输入正确的钱数就跳出循环
            for (int i = moneyArr.Length - 1; i >= 0; i--)
            {
                while (moneySum >= moneyArr[i])
                {
                    moneyList.Add(moneyArr[i]);
                    moneySum -= moneyArr[i];
                }
            }            if (moneySum > 0)
                Console.WriteLine("无法完整凑成需要的钱数,因为还有" + moneySum.ToString() + "元的零钱小于最小面值,无法凑出,最接近的方案如下:");
            Console.WriteLine("最少需要" + moneyList.Count.ToString() + "张钱币才能凑成。分别是:");
            for (int i = 0; i < moneyList.Count; i++)
            {
                Console.Write(moneyList[i].ToString() + " ");
            }
            Console.ReadKey(); 
        }
    }
}现在有一问题,按以上程序调试,当我输入79的时候,结果输出不能凑成此钱数,只能给我一个50,20,8,还差1凑不成!  但实际上,79可以由50,15,8,6凑成,该怎样修改此程序?