1 2 5--800

Source: Internet
Author: User

With 1 Yuan 2 yuan 5 yuan 10 yuan 20 yuan banknotes into 800 yuan of the method of counting, using dynamic programming.

The results didn't come out, just kept in the functions of each vector, debugging can see all the results.

Pros: Quick

Cons: Occupy Space in memory

Time-consuming test:

And for 200:0.0022153191s

And for 800:0.025958383s

And for 1600:0.062776931s

And for 3200:0.18964779s

Below is the detailed code, can work out the results, improve the space is very large, only the idea.

The algorithm is king, and the O (N2) function written by C is no faster than the O (Nlogn) function written by R.

voidCmfctestdlg::onbnclickedbegin () {updatedata (TRUE);    Large_integer BeginTime; QueryPerformanceCounter (&beginTime);std::vector<int> Vec_1_2 (m_ntotal+1, -1);  for(intI=0; i<=m_ntotal; i++) {Vec_1_2[i]=1+i/2;//use 1 Yuan 2 yuan note to combine 0 to m_ntotal the number of methods} std::vector<int>vec_1_2_5 (Vec_1_2.begin (), Vec_1_2.end ());  for(intI=5; i<=m_ntotal; i++)    {         for(intj=1; j<=i/5; J + +) {Vec_1_2_5[i]+ = vec_1_2[i-j*5]; Use 1 Yuan 2 Yuan 5 yuan note combined into 0 to m_ntotal the number of methods}} std::vector<int>vec_1_2_5_10 (Vec_1_2_5.begin (), Vec_1_2_5.end ());  for(intI=Ten; i<=m_ntotal; i++)    {         for(intj=1; j<=i/Ten; J + +) {Vec_1_2_5_10[i]+ = vec_1_2_5[i-j*Ten]; Using 1 Yuan 2 yuan 5 yuan 10 yuan banknotes combined into 0 to m_ntotal the number of methods}} std::vector<int>vec_1_2_5_10_20 (Vec_1_2_5_10.begin (), Vec_1_2_5_10.end ());  for(intI= -; i<=m_ntotal; i++)    {         for(intj=1; j<=i/ -; J + +) {Vec_1_2_5_10_20[i]+ = vec_1_2_5_10[i-j* -]; Using 1 Yuan 2 yuan 5 yuan 10 yuan 20 yuan banknotes combined into 0 to m_ntotal the number of methods}}
//.... If there are more than 50 yuan 100 yuan and so on, add it here and then, it is easy to expand
Large_integer EndTime; QueryPerformanceCounter (&endTime); Large_integer frequency; QueryPerformanceFrequency (&frequency); floatFtime = (Endtime.quadpart-begintime.quadpart)/(float) (frequency. QuadPart); Ftime=0;}

1 2 5--800

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.