A classic C ++ question. It is suitable for beginners to read about the issue of money sharding (The question of the black guest XFile Forum) [C #]

Source: Internet
Author: User

On the homepage of the csdn forum a few days ago, I saw a post entitled "A Classic C ++ question. It is suitable for beginners to read about the issue of money sharing (The question of the black guest XFile Forum)". The link is as follows: http://blog.csdn.net/gisfarmer/archive/2009/02/08/3869236.aspx

Question:
How many ways can I change a banknote with a nominal value of one dollar to a cent, two or five cent coin? Program each algorithm.

This question is very simple. It is the simplest C ++ question and can be done with C.

 

View Original posts

There are a lot of answers in it. To be honest, it's a bit difficult, because the code is a bit awkward, and some loops are like writing dead ..

For example:

  1. IntX = 0, y = 0, Z = 0, n = 0;
  2. IntCount = 0;
  3. For (Z = 1; Z <20; Z ++)
  4. {
  5. For (y = 1; y <= (100-z * 5)/2; y ++)

I don't know why z <20 is written in this way. Why is it 20?

There is also how to write it if I add one more hair or five hairs and find the total number of replicas?

Or it's not a dollar. It's five yuan. How can I change the total amount of money?

... Well, I am also writing programs and afraid of business changes. So I am wondering if I can write a general method. So I wrote the following, please kindly advise me (I am a cainiao ).

I wrote two methods, one for output details, and the other for output details. You can try again ..

 

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 );
// No output details
Splitmoney (100, 0, intlist, 0, ref count );
// Output details
// Splitmoney (10, 0, intlist, 0, restring, ref count );
Console. writeline (string. Format ("{0} methods in total", count ));
}

/// <Summary>
/// Method of money sharding
/// </Summary>
/// <Param name = "pmmoney"> allocated money </param>
/// <Param name = "pmmoneynow"> sum of the Current billing method </param>
/// <Param name = "pmmoneylist"> cent list </param>
/// <Param name = "pmmoneyindex"> current cent index </param>
/// <Param name = "pmstring"> details of the current method </param>
/// <Param name = "pmsplitcount"> total number of money-sharing methods </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 ++)
{
// Current and statistics
Int snsumnow = I * pmmoneylist [pmmoneyindex] + pmmoneynow;

// -- Begin ---- This string is mainly for output. If the output is not required, comment out ----------------
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>
/// Method of money sharding
/// </Summary>
/// <Param name = "pmmoney"> allocated money </param>
/// <Param name = "pmnoneynow"> sum of the Current billing method </param>
/// <Param name = "pmmoneylist"> cent list </param>
/// <Param name = "pmmoneyindex"> current cent index </param>
/// <Param name = "pmsplitcount"> total number of money-sharing methods </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 ++)
{
// Current and statistics
Int snsumnow = I * pmmoneylist [pmmoneyindex] + pmnoneynow;
If (snsumnow = pmmoney)
{
Pmsplitcount ++;
}
Else
{
Splitmoney (pmmoney, snsumnow, pmmoneylist, pmmoneyindex + 1, ref pmsplitcount );
}
}
}

}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.