日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

中兴面试题 01背包问题

發(fā)布時(shí)間:2024/8/1 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 中兴面试题 01背包问题 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

背包問(wèn)題就是包要滿(mǎn),但是01背包允許包不滿(mǎn)

?

一,題目:輸入兩個(gè)整數(shù) n 和 m,從數(shù)列1,2,3.......n 中隨意取幾個(gè)數(shù),使其和等于 m ,要求將其中所有的可能組合列出來(lái)。

二,解釋:比如輸入m=4 ?n=4 則輸出為:4

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1+3 ? 而2+2不正確,因?yàn)橹貜?fù)輸出數(shù)字了

? ? ? ? ? ? ? ? ? 中心思想:1)如果1+2+3+……+n<m 則不存在這個(gè)數(shù)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2)如果m<n 則應(yīng)該讓n=m //因?yàn)閙--->n之間的數(shù)都已經(jīng)大于m了 沒(méi)必要再計(jì)算了

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3)如果m=n 輸出n

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 4)如果m>n ? 遞歸循環(huán)

? ? ? ? ? ? ? ? ? 源碼采用原型:0-1背包問(wèn)題

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?參考博客:http://blog.csdn.net/tianshuai11/article/details/7025464

三,源碼:(類(lèi)似源碼五)

#include<list>

[html] view plaincopyprint?
  • #include<iostream>????
  • using?namespace?std;????
  • ????
  • list<int>?list1;????
  • void?find_factor(int?sum,?int?n)?????
  • {????
  • ????//?遞歸出口????
  • ????if(n?<=?0?||?sum?<=?0)????
  • ????????return;????
  • ????????
  • ????//?輸出找到的結(jié)果????
  • ????if(sum?==?n)????
  • ????{????
  • ????????//?反轉(zhuǎn)list????
  • ????????list1.reverse();????
  • ????????for(list<int>::iterator?iter?=?list1.begin();?iter?!=?list1.end();?iter++)????
  • ????????????cout?<<?*iter?<<?"?+?";????
  • ????????cout?<<?n?<<?endl;????
  • ????????list1.reverse();????????
  • ????}????
  • ????????
  • ????list1.push_front(n);??????//典型的01背包問(wèn)題????
  • ????find_factor(sum-n,?n-1);???//放n,n-1個(gè)數(shù)填滿(mǎn)sum-n????
  • ????list1.pop_front();????
  • ????find_factor(sum,?n-1);?????//不放n,n-1個(gè)數(shù)填滿(mǎn)sum?????
  • }????
  • ????
  • int?main()????
  • {????
  • ????int?sum,?n;????
  • ????cout?<<?"請(qǐng)輸入你要等于多少的數(shù)值sum:"?<<?endl;????
  • ????cin?>>?sum;????
  • ????cout?<<?"請(qǐng)輸入你要從1.....n數(shù)列中取值的n:"?<<?endl;????
  • ????cin?>>?n;????
  • ????cout?<<?"所有可能的序列,如下:"?<<?endl;????
  • ????find_factor(sum,n);????
  • ????return?0;????
  • }????
  • #include<iostream> using namespace std; list<int> list1; void find_factor(int sum, int n) { // 遞歸出口 if(n <= 0 || sum <= 0) return; // 輸出找到的結(jié)果 if(sum == n) { // 反轉(zhuǎn)list list1.reverse(); for(list<int>::iterator iter = list1.begin(); iter != list1.end(); iter++) cout << *iter << " + "; cout << n << endl; list1.reverse(); } list1.push_front(n); //典型的01背包問(wèn)題 find_factor(sum-n, n-1); //放n,n-1個(gè)數(shù)填滿(mǎn)sum-n list1.pop_front(); find_factor(sum, n-1); //不放n,n-1個(gè)數(shù)填滿(mǎn)sum } int main() { int sum, n; cout << "請(qǐng)輸入你要等于多少的數(shù)值sum:" << endl; cin >> sum; cout << "請(qǐng)輸入你要從1.....n數(shù)列中取值的n:" << endl; cin >> n; cout << "所有可能的序列,如下:" << endl; find_factor(sum,n); return 0; }


    總結(jié)

    以上是生活随笔為你收集整理的中兴面试题 01背包问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。