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