一道经典的C++题,关于分钱的问题,适合新手阅读(黑客X档案论坛题目) [c#]...
?前幾天CSDN論壇的首頁,看到一則帖子,題目是:“一道經典的C++題,關于分錢的問題,適合新手閱讀(黑客X檔案論壇題目) ”,鏈接如下:http://blog.csdn.net/gisfarmer/archive/2009/02/08/3869236.aspx
題如下:
把一張面值為一元的紙幣,換成一分,二分,五分的硬幣,共有多少種換法?編程輸出每一種不同的算法。
這道題很簡單,是最簡單的C++題,用C也可以做。
?
看了原貼http://bbs.hackerxfiles.net/thread-98055-15-1.html和http://blog.csdn.net/gisfarmer/archive/2009/02/08/3869236.aspx
里面有很多答案,說實在的,有點看不下去了,因為總感受寫的代碼有點別扭,做的一些循環都像是寫死了的。。
比如:
我不知道z<20為什么要這么寫,為什么是20呢?
還有就是如果如果再加一個一毛,或者是五毛,求共有多少種換法,這又怎么寫呢?
又或者是分的不是一塊錢,分的是五塊錢,求共有多少種換法,又怎么寫呢?
。。。哎,我也是寫程序的,很怕業務的改變,所以在想,能不能寫一個通用點的方法,于是自己寫了一個如下,還請路過的指教一下(本人是菜鳥).
寫了兩個方法,一個需要輸出明細,一個不需要輸出明細,大家可以試著玩一下。。
?
c#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleStudy
{
??? class SumNumberDemo
??? {
??????? static void Main() {
??????????? Console.WriteLine("SumNumberDemo");
??????????? List<int> intList = new List<int>();
??????????? string reString =string.Empty;
??????????? int count = 0;
??????????? intList.Add(1);
??????????? intList.Add(2);
??????????? intList.Add(5);
??????????? //intList.Add(7);
??????????? //intList.Add(9);
??????????? //不需要輸出明細
??????????? SplitMoney(100, 0, intList, 0, ref count);
??????????? //輸出明細
??????????? //SplitMoney(10, 0, intList, 0, reString, ref count);
??????????? Console.WriteLine(string.Format("總共有{0}種分法",count));
??????? }
??????? /// <summary>
??????? /// 分錢方法
??????? /// </summary>
??????? /// <param name="pmMoney">被分的錢</param>
??????? /// <param name="pmMoneyNow">當前分法的錢的和</param>
??????? /// <param name="pmMoneyList">分錢列表</param>
??????? /// <param name="pmMoneyIndex">當前分錢索引</param>
??????? /// <param name="pmString">當前分法明細</param>
??????? /// <param name="pmSplitCount">分錢方法總數統計</param>
??????? static void SplitMoney(int pmMoney, int pmMoneyNow, List<int> pmMoneyList, int pmMoneyIndex, string pmString, ref int pmSplitCount)
??????? {
??????????? int snCount=pmMoneyList.Count;
??????????? if (pmMoneyIndex < pmMoneyList.Count)
??????????? {
??????????????? for (int i = 0; i * pmMoneyList[pmMoneyIndex] <= pmMoney; i++)
??????????????? {
??????????????????? //當前和統計
??????????????????? int snSumNow = i * pmMoneyList[pmMoneyIndex]+pmMoneyNow;
??????????????????? //--begin----這個字符串主要是為了輸出的,如果不要求輸出可以注釋掉----------------
??????????????????? string snString = string.Empty;
??????????????????? if (string.IsNullOrEmpty(pmString))
??????????????????? {
??????????????????????? snString = string.Format("{0}*{1}",i, pmMoneyList[pmMoneyIndex]);
??????????????????? }
??????????????????? else {
??????????????????????? snString = string.Format("{0}+{1}*{2}", pmString, i, pmMoneyList[pmMoneyIndex]);
??????????????????? }
??????????????????? //---end------------------
??????????????????? if (snSumNow == pmMoney)
??????????????????? {
??????????????????????? //------begin-------------
??????????????????????? for (int j = pmMoneyIndex+1; j < snCount;j++ )
??????????????????????? {
??????????????????????????? snString += string.Format("+0*{0}", pmMoneyList[j]);
??????????????????????? }
??????????????????????? //-------end-------------
??????????????????????? pmSplitCount++;
??????????????????????? Console.WriteLine(string.Format("---{0}={1}---",snString,pmMoney));
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? SplitMoney(pmMoney, snSumNow, pmMoneyList, pmMoneyIndex + 1, snString, ref pmSplitCount);
??????????????????? }
??????????????? }
??????????? }
??????? }
??????? /// <summary>
??????? /// 分錢方法
??????? /// </summary>
??????? /// <param name="pmMoney">被分的錢</param>
??????? /// <param name="pmNoneyNow">當前分法的錢的和</param>
??????? /// <param name="pmMoneyList">分錢列表</param>
??????? /// <param name="pmMoneyIndex">當前分錢索引</param>
??????? /// <param name="pmSplitCount">分錢方法總數統計</param>
??????? static void SplitMoney(int pmMoney, int pmNoneyNow, List<int> pmMoneyList, int pmMoneyIndex, ref int pmSplitCount)
??????? {
??????????? int snCount = pmMoneyList.Count;
???????????
??????????? if (pmMoneyIndex < pmMoneyList.Count)
??????????? {
??????????????? for (int i = 0; i * pmMoneyList[pmMoneyIndex] <= pmMoney; i++)
??????????????? {
??????????????????? //當前和統計
??????????????????? int snSumNow = i * pmMoneyList[pmMoneyIndex] + pmNoneyNow;
??????????????????? if (snSumNow == pmMoney)
??????????????????? {
??????????????????????? pmSplitCount++;
??????????????????? }
??????????????????? else
??????????????????? {
??????????????????????? SplitMoney(pmMoney, snSumNow, pmMoneyList, pmMoneyIndex + 1, ref pmSplitCount);
??????????????????? }
??????????????? }
??????????? }
??????? }
??? }
}
?
轉載于:https://www.cnblogs.com/bbqqqbq/archive/2009/02/10/1387337.html
總結
以上是生活随笔為你收集整理的一道经典的C++题,关于分钱的问题,适合新手阅读(黑客X档案论坛题目) [c#]...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 矩阵分析及应用(10章 - 张量分析)
- 下一篇: 微计算机与单片机原理及应用答案,电子科技