




已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
算法設(shè)計(jì)與分析作業(yè)作業(yè)一:問題:給定一組硬幣,找出若干的硬幣,使得他們的值最大,但不能選擇相鄰的硬幣代碼:CoinProblem.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。/給定一組硬幣,找出若干的硬幣,使得他們的值最大,但不能選擇相鄰的硬幣/#include stdafx.hconst int coinNum = 100;int coinValuecoinNum; /硬幣值數(shù)組int maxValue; /得到的硬幣最大值/位置選擇表struct nodeint totalValue;int prePos=-1; /前一個(gè)節(jié)點(diǎn)的位置,-1表示結(jié)束了;node coinPoscoinNum;void exchangeValue(int &a,int &b)int temp = a;a = b;b = temp;/產(chǎn)生一個(gè)有順序數(shù)組,隨機(jī)交換一組值,擾亂順序,得到一個(gè)隨機(jī)數(shù)組void makeCoinValue(int size,int maxValue)/產(chǎn)生1-max的數(shù)組int *temp = new intmaxValue;for (int i = 0; i maxValue; i+)*(temp + i) = i+1;srand(unsigned( time_t(NULL) );/交換多次int times = size;for (int j = 0; j times; j+)int pos1 = rand() % maxValue;int pos2 = rand() % maxValue;exchangeValue(*(temp + pos1), *(temp + pos2);for (int k = 0; k size; k+)coinValuek = *(temp + k);cout 硬幣的面值序列:;for (int k = 0; k size ; k+)cout coinValuek ;cout = 0)node tempMax = coinPospos;for (int j = pos; j = 0; j-)if (coinPosj.totalValue tempMax.totalValue)tempMax = coinPosj;pos = j;return pos;void printResult(int endPos)int currentPos = endPos;cout = 0)cout currentPos+1 : coinValuecurrentPos ;currentPos = coinPoscurrentPos.prePos; cout endl;cout 最大的和為: maxValueendl;void selectCoin()maxValue = 0; /當(dāng)前累計(jì)最大為int maxEndPos = 0; /當(dāng)前最大鏈的結(jié)束點(diǎn)for (int i = 0; i coinNum; i+) int maxPrePos = maxPreValue(i); if (maxPrePosmaxValue)maxValue = coinPosi.totalValue; /如果當(dāng)前值大于最大值,更新最大值和結(jié)束點(diǎn)maxEndPos = i;printResult(maxEndPos); int _tmain(int argc, _TCHAR* argv)makeCoinValue(10, 20);selectCoin();return 0;運(yùn)行結(jié)果:作業(yè)二:來自leetcode:/ CombinationSum.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。/* Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.Ensure that numbers within the set are sorted in ascending order.Example 1:Input : k = 3, n = 7Output :1, 2, 4Example 2 :Input : k = 3, n = 9Output :1, 2, 6, 1, 3, 5, 2, 3, 4*/#include stdafx.h/temp向量可以加引用也可以不加,加上引用則不會(huì)在遞歸的時(shí)候產(chǎn)生副本,降低的內(nèi)存的需求void combination(vectorvector & vvReslut, vector& temp, int k,int n)if (0=k & 0=n)vvReslut.push_back(temp);return;if (0k)for (int i = (temp.empty() ? 1:temp.back()+1); i = n/k; i+) /i的值不可能大于n/k,可以大大減少循環(huán)if (n - i 0) break; /n-i如果小于0,則不是遞增順序temp.push_back(i);combination(vvReslut, temp, k-1, n - i); /對(duì)k-1位的n-i進(jìn)行操作temp.pop_back(); vectorvector combinationSum3(int k, int n) vectorvector result;vector sol;combination(result, sol, k, n);return result;int _tmain(int argc, _TCHAR* argv)vectorvector vv = combinationSum3(3, 12);int length = vv.size();for (int i = 0; i length; i+)int vlength = vvi.size();for (int j = 0; j vlength; j+)cout vvij ;cout endl;return 0;運(yùn)行結(jié)果:作業(yè)三:來自leetcode:/ KthLargestElement.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。/* Find the kth largest element in an unsorted array.Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given3, 2, 1, 5, 6, 4 and k = 2, return 5.Note:You may assume k is always valid, 1 k arrays length.*/#include stdafx.hint findKthLargest(vector& nums, int k) priority_queue p; /把值放入優(yōu)先級(jí)隊(duì)列const int s(nums.size();for (int i = 0; i length;cin
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年證券登記、結(jié)算機(jī)構(gòu)服務(wù)項(xiàng)目規(guī)劃申請(qǐng)報(bào)告模板
- 2025年壓力容器檢驗(yàn)員資格考試試卷:壓力容器檢驗(yàn)員考試輔導(dǎo)教材試題
- 班級(jí)里的英雄事跡記人記事作文(8篇)
- 2025年北京市公安機(jī)關(guān)公務(wù)員錄用考試面試技巧試題
- 專業(yè)資格證書證明書專業(yè)資格證書名稱(6篇)
- 2025年電子商務(wù)師(職業(yè)資格精英)考試試卷
- 成長路上的一堂課議論文14篇
- 成長的足跡演講比賽演講稿類作文(13篇)
- 2025年初中化學(xué)九年級(jí)上冊(cè)期中測(cè)試卷化學(xué)實(shí)驗(yàn)原理分析
- 2025年貴金屬首飾與寶玉石檢測(cè)員(高級(jí))考試試卷實(shí)戰(zhàn)案例分析與解答
- 卓越績效評(píng)價(jià)準(zhǔn)則概述(專業(yè)性權(quán)威性實(shí)用性)
- GB/T 29894-2013木材鑒別方法通則
- GB 1886.20-2016食品安全國家標(biāo)準(zhǔn)食品添加劑氫氧化鈉
- 國資進(jìn)場(chǎng)交易工作流程講座
- 當(dāng)代法律英語翻譯全
- 制冷操作證培訓(xùn)教材制冷與空調(diào)設(shè)備運(yùn)行操作作業(yè)培訓(xùn)教程課件
- 湖南省長沙市望城區(qū)2020-2021學(xué)年八年級(jí)下學(xué)期期末考試歷史試卷
- 下承式鋼桁梁橋結(jié)構(gòu)設(shè)計(jì)及優(yōu)化 (跨度64m)
- DB32-T 2665-2014機(jī)動(dòng)車維修費(fèi)用結(jié)算規(guī)范-(高清現(xiàn)行)
- “麥語言”函數(shù)手冊(cè)
- 外協(xié)(外委)單位作業(yè)安全管理制度(附安全告知書)
評(píng)論
0/150
提交評(píng)論