---
title: "做题练习--归并排序,穷举"
author: "holoblog"
date: "2007-12-13T11:38:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2011-11-11T11:23:32Z"
id: 1252038
url: "https://holoblog.iteye.com/blog/1252038"
---

最近比较闲，抽空练练爪子：

多路归并

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)voidMergeLines(int\*temp,int\*A,intleft,intright)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//其中temp是临时存储空间，A是真正的存放数据空间，left是数组的最左下标，right是数组的最右下标。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//先判断些特殊的边界条件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(right-left&lt;1)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(right-left==1)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(A\[right]&gt;A\[left])  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)swap(A\[right],A\[left]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intmid=(right-left)/2+left;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)MergeLines(&temp\[0],&A\[0],left,mid);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)MergeLines(&temp\[mid+1],&A\[mid+1],0,right-mid-1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//真正的归并开始（这里可以看做已经做到最后一步）  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intk=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)inti=0,j=mid+1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)while(i&lt;=mid&&j&lt;=right)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(A\[i]&gt;A\[j])  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)temp\[k++]=A\[i++];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)temp\[k++]=A\[j++];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(i&gt;mid)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)while(j&lt;=right)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)temp\[k++]=A\[j++];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)while(i&lt;=mid)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)temp\[k++]=A\[i++];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)//重新将值赋回A数组  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(i=0;i&lt;k;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)A\[i]=temp\[i];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

使用一个串中相同的字母再生成新串，打印所有的串。比如“abc”有“bac”“bca”....等等。

这段代码的想法是{a,b,c},拿到{a}，将{b,c}去递归。如果拿到{b},将{a,c}去递归。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)voidAll(stringpre,char\*str,intlen)//pre是前缀，str是真正的串，len是串的长度（除去'/0'）  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//这里注意str必须是一个new出来的字符串数组，不能是字符串常量数组。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)//开始的调用可以这样写All("",str,strlen(str));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(\*str=='/0')  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(len==1)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cout&lt;&lt;pre&lt;&lt;str\[0]&lt;&lt;endl;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;len;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(i==0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)All(pre+str\[0],&str\[1],len-1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(i!=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)swap(str\[i],str\[0]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)All(pre+str\[0],&str\[1],len-1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)swap(str\[i],str\[0]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

上面的函数可以进一步改写:这里的List就是{‘a’,'b'},currentIndex = 0;k = len -1,len = List的元素个数。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)voidAll(char\*List,intcurrentIndex,intk,intlen)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(currentIndex==k)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;len;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cout&lt;&lt;List\[i];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cout&lt;&lt;endl;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=currentIndex;i&lt;=k;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)swap(List\[currentIndex],List\[i]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)All(List,currentIndex+1,k,len);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)swap(List\[currentIndex],List\[i]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

现在有一张纸币，但是现在有一堆个数无限的硬币（1角，2角，5角），兑换硬币

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)voidpast(inti)//这里的i是最初的钱的价值（兑换成角，1元就是10角....）不过这里还是使用了穷举...:&lt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intu=0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(intj=0;j&lt;=i;j+=5)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(intx=0;x&lt;=i;x+=2)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(intc=0;c&lt;=i;c+=1)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(j+x+c==i)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cout&lt;&lt;"&lt;5,2,1&gt;"&lt;&lt;endl;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cout&lt;&lt;j/5&lt;&lt;","&lt;&lt;x/2&lt;&lt;","&lt;&lt;c&lt;&lt;endl;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)u++;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

不过写的代码自我感觉变量命名和函数命名不规范。这的确是今后要大大改进的地方。而且效率还很低。

因为硬币的穷举算法效率很低，所以用背包法重写代码：

如果输入的dest是10角，那么这里的srcSet应该含有10/1个1角，10/2个2角，10/5个5角。currentIndex是srcSet的下标，srcLen是srcSet的大小，subSet是符合条件的数据,num是subSet的集合中现含的元素个数。

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)intmatch(int\*srcSet,intcurrentIndex,intsrcLen,intdest,int\*subSet,intnum)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(currentIndex==srcLen)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)return-1;

if(dest-srcSet\[currentIndex] &lt; 0)  
return -1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(dest-srcSet\[currentIndex]==0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)subSet\[num++]=srcSet\[currentIndex];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cout&lt;&lt;num&lt;&lt;"numbermatchthedest,theyare:"&lt;&lt;endl;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)for(inti=0;i&lt;num;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cout&lt;&lt;subSet\[i]&lt;&lt;"";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)cout&lt;&lt;endl;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnnum;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)subSet\[num]=srcSet\[currentIndex];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)intnumber=match(srcSet,currentIndex+1,srcLen,dest-srcSet\[currentIndex],subSet,num+1);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(number&lt;=0)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnmatch(srcSet,currentIndex+1,srcLen,dest,subSet,num);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnmatch(srcSet,currentIndex+1,srcLen,dest,subSet,num);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

但是该算法仍然有的缺陷是最后返回的一定是-1。所以只好在程序运行过程中打印出结果。这里要求数组srcSet是从小到大排列的。虽然算法的复杂度没有降低，但是运行的时间可能会加快。但是在最差情况下，比上面的穷举效率还低。而且多使用了空间。感觉类似于迷宫问题。虽然可以用A\*但是最差情况下，比穷举还低。