对n个桶中不同颜色的砾石进行排序
算法思路,对顺序表进行排序操作。<BR>设计内容及分析过程:<BR><BR>在此设立三个指针分别为s,p和q,其中s指向线性表的L.elem[0],即第一个元素,p=s+1,即p指向线性表的第二个元素L.elem[1],q指向线性表的最后一个元素。<BR><BR>查找时,当指针p不等于q时,首先判断s所指向的元素是否为’a’,如果是,则s指针向后滑一个位置,同时p指针也向后滑一个位置。然后判断q所指向的元素是否为’c’,如果是,则q指针向前滑一个位置。<BR><BR>如果指针s所指向的元素为’c’时,则与指针q所指向的元素交换,指针s的位置不变,q指针向前滑一个位置,如果指针q所指向的元素为’a’时,则与指针s所指向的元素交换,指针q的位置不变,s指针向后滑一个位置,同时,p指针也向后滑一个位置。如此循环下去,直到指针p等于q时,退出循环。<BR><BR>该程序实现了对n个桶中不同颜色的砾石进行排序,而且在查找过程中对每粒砾石的颜色只观察了一次. <BR>提示:以上“a”、“c”表示不同的颜色。<BR><BR>帮忙给个源程序我研究<BR>回复:(jfo)对n个桶中不同颜色的砾石进行排序
<P>/*---------------------------------------------------------------------------<BR>File name: stones.cpp<BR>Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )<BR>Created on: 7/14/2007 00:35:35<BR>Environment: Windows XP Professional SP2 English +<BR> Visual Studio 2005 v8.0.50727.762</P><P><BR>Modification history:<BR>===========================================================================</P>
<P><BR>Problem statement:<BR>---------------------------------------------------------------------------<BR>对n个桶中不同颜色的砾石进行排序算法思路,对顺序表进行排序操作。<BR>设计内容及分析过程:</P>
<P>在此设立三个指针分别为s,p和q,其中s指向线性表的L.elem[0],即第一个元素,p=s+1,<BR>即p指向线性表的第二个元素L.elem[1],q指向线性表的最后一个元素。</P>
<P>查找时,当指针p不等于q时,首先判断s所指向的元素是否为’a’,如果是,则s指针向后<BR>滑一个位置,同时p指针也向后滑一个位置。然后判断q所指向的元素是否为’c’,如果是,<BR>则q指针向前滑一个位置。</P>
<P>如果指针s所指向的元素为’c’时,则与指针q所指向的元素交换,指针s的位置不变,q指针<BR>向前滑一个位置,如果指针q所指向的元素为’a’时,则与指针s所指向的元素交换,指针q<BR>的位置不变,s指针向后滑一个位置,同时,p指针也向后滑一个位置。如此循环下去,直到<BR>指针p等于q时,退出循环。</P>
<P>该程序实现了对n个桶中不同颜色的砾石进行排序,而且在查找过程中对每粒砾石的颜色只<BR>观察了一次. </P>
<P>提示:以上“a”、“c”表示不同的颜色。</P>
<P>帮忙给个源程序我研究</P>
<P><BR>Analysis:<BR>---------------------------------------------------------------------------<BR>Here is a modified verion of my code which handle 3 colors --- I wrote that<BR>code a few years ago. In your case, only two colors are required, so it is<BR>easier.</P>
<P>I am not using a list, instead I am using an array. You should be able to<BR>rewrite it for your own task.</P>
<P>I did not test boundary cases, such as the case all the colors are a.</P>
<P>Sample output:<BR>---------------------------------------------------------------------------</P>
<P>Please input a number n (n>0): 23<BR>The oringinal matrix is:<BR>c c a a a a c c a c a a c c c a c c a c a a a<BR>----------------------------------------------------------------------<BR>The arranged matrix is:<BR>a a a a a a a a a a a a c c c c c c c c c c c<BR>Press any key to continue . . .</P>
<P><BR>Reference:<BR>---------------------------------------------------------------------------</P>
<P>1. 对n个桶中不同颜色的砾石进行排序<BR><a href="http://bbs.bc-cn.net/viewthread.php?tid=155209" target="_blank" >http://bbs.bc-cn.net/viewthread.php?tid=155209</A></P>
<P><BR>*/</P>
<P>#include <iostream><BR>#include <algorithm><BR>#include <iterator><BR>#include <ctime><BR>using namespace std;</P>
<P>/** arrange the two colors so that color a sits on the left,<BR>color c sits on the right.</P>
<P>Time complexity is O(n), space complexity is O(1). Thus it is<BR>the best algorithm one could have.<BR>*/<BR>void arrangeAC(char*a, int n);</P>
<P>void swap(char& i, char& j);</P>
<P>// randome generator for an array consisting of colors a and c.<BR>void randomGenerator(char* arr, int size);</P>
<P><BR>int main(int argc, char** argv)<BR>{<BR> // prepare the array<BR> int kSize;<BR> cout<<"Please input a number n (n>0): ";<BR> cin>>kSize;</P>
<P> char *a = new char[kSize];<BR> randomGenerator(a, kSize);</P>
<P> cout<<"The oringinal matrix is:\n";<BR> copy(a, a+kSize, std::ostream_iterator<char>(cout, " "));<BR> cout<<endl;<BR> cout<<"----------------------------------------------------------------------\n";</P>
<P> arrangeAC(a, kSize);</P>
<P> cout<<"The arranged matrix is:\n";<BR> copy(a, a+kSize, std::ostream_iterator<char>(cout, " "));<BR> cout<<endl;</P>
<P> delete [] a;</P>
<P> return 0;<BR>}</P>
<P>void arrangeAC(char *a, int n)<BR>{<BR> /**<BR> These 3 variables are the 3 pointers of your case.<BR> */<BR> int aMarker = 0;<BR> int cMarker = n-1;<BR> int i = 1;</P>
<P> // on the left of a marker, all colors are a<BR> while ( a[aMarker] == 'a' )<BR> {<BR> ++aMarker;<BR> }</P>
<P> // on the right of c marker, all colors are c<BR> while ( a[cMarker] == 'c')<BR> {<BR> --cMarker;<BR> }</P>
<P> i = aMarker+1;<BR> while (i <= cMarker)<BR> {<BR> if(a[i] == 'a')<BR> { <BR> if( i > aMarker )<BR> {<BR> swap(a[i], a[aMarker]);<BR> ++aMarker;<BR> }<BR> else<BR> ++i;<BR> }<BR> else<BR> {<BR> swap(a[i], a[cMarker]);<BR> --cMarker;<BR> }<BR> }<BR>}</P>
<P>void swap(char& i, char& j)<BR>{<BR> char temp = i;<BR> i = j;<BR> j = temp;<BR>}</P>
<P>void randomGenerator(char* arr, int size)<BR>{<BR> srand(time(NULL));</P>
<P> for(int i=0; i<size; ++i)<BR> {<BR> arr[i] = ( (rand() | (rand()<<16) ) % 2 == 0 ? 'a' : 'c');<BR> }<BR>}<BR></P>
回复:(HJin)回复:(jfo)对n个桶中不同颜色的砾石...
thanks to you<BR>要是我哪天有这么厉害就好啦~~<BR>再次感谢楼上朋友给出的解<BR> 上面要求是用顺序表形式编辑程序,并不是用数组形式呀,哪位大虾能用顺序表形式表现出来不?页:
[1]
