Êý¾Ý½á¹¹£¨CÓïÑ԰棩¿ÎºóÏ°Ìâ´ð°¸ ÏÂÔر¾ÎÄ

(4)Ç뽫Ï㽶bananaÓù¤¾ß H( )¡ªHead( )£¬T( )¡ªTail( )´ÓLÖÐÈ¡³ö¡£ L=(apple,(orange,(strawberry,(banana)),peach),pear) ´ð°¸£ºH£¨H£¨T£¨H£¨T£¨H£¨T£¨L£©£©£©£©£©£©£©

3£®Ëã·¨Éè¼ÆÌâ

£¨1£©Ð´Ò»¸öË㷨ͳ¼ÆÔÚÊäÈë×Ö·û´®Öи÷¸ö²»Í¬×Ö·û³öÏÖµÄƵ¶È²¢½«½á¹û´æÈëÎļþ£¨×Ö·û´®ÖеĺϷ¨×Ö·ûΪA-ZÕâ26¸ö×ÖĸºÍ0-9Õâ10¸öÊý×Ö£©¡£

[ÌâÄ¿·ÖÎö] ÓÉÓÚ×Öĸ¹²26¸ö£¬¼ÓÉÏÊý×Ö·ûºÅ10¸ö¹²36¸ö£¬ËùÒÔÉèÒ»³¤36µÄÕûÐÍÊý×飬ǰ10¸ö·ÖÁ¿´æ·ÅÊý×Ö×Ö·û³öÏֵĴÎÊý£¬ÓàÏ´æ·Å×Öĸ³öÏֵĴÎÊý¡£´Ó×Ö·û´®ÖжÁ³öÊý×Ö×Ö·ûʱ£¬×Ö·ûµÄASCII´úÂëÖµ¼õÈ¥Êý×Ö×Ö·û ¡®0¡¯µÄASCII´úÂëÖµ£¬µÃ³öÆäÊýÖµ(0..9)£¬×ÖĸµÄASCII´úÂëÖµ¼õÈ¥×Ö·û¡®A¡¯µÄASCII´úÂëÖµ¼ÓÉÏ10£¬´æÈëÆäÊý×éµÄ¶ÔӦϱê·ÖÁ¿ÖС£ÓöÆäËü·ûºÅ²»×÷´¦Àí£¬Ö±ÖÁÊäÈë×Ö·û´®½áÊø¡£

[Ëã·¨ÃèÊö] void Count£¨£©

//ͳ¼ÆÊäÈë×Ö·û´®ÖÐÊý×Ö×Ö·ûºÍ×Öĸ×Ö·ûµÄ¸öÊý¡£ £ûint i£¬num[36]£» char ch£»

for£¨i£½0£»i<36£»i++£©num[i]£½£°£»// ³õʼ»¯

while£¨£¨ch£½getchar£¨£©£©!=¡®#¡¯£© //¡®#¡¯±íʾÊäÈë×Ö·û´®½áÊø¡£ if£¨¡®0¡¯<=ch<=¡®9¡¯£©£ûi=ch£­48;num[i]++£»£ý // Êý×Ö×Ö·û else if£¨¡®A¡¯<=ch<=¡®Z¡¯£©£ûi=ch-65+10;num[i]++£»£ý// ×Öĸ×Ö·û for£¨i=0£»i<10£»i++£© // Êä³öÊý×Ö×Ö·ûµÄ¸öÊý

£¨2£©Ð´Ò»¸öµÝ¹éËã·¨À´ÊµÏÖ×Ö·û´®ÄæÐò´æ´¢£¬ÒªÇó²»ÁíÉè´®´æ´¢¿Õ¼ä¡£

[ÌâÄ¿·ÖÎö]ʵÏÖ×Ö·û´®µÄÄæÖò¢²»ÄÑ£¬µ«±¾Ìâ¡°ÒªÇó²»ÁíÉè´®´æ´¢¿Õ¼ä¡±À´ÊµÏÖ×Ö·û´®ÄæÐò´æ´¢£¬¼´µÚÒ»¸öÊäÈëµÄ×Ö·û×îºó´æ´¢£¬×îºóÊäÈëµÄ×Ö·ûÏÈ´æ´¢£¬Ê¹Óõݹé¿ÉÈÝÒ××öµ½¡£

[Ëã·¨ÃèÊö]

void InvertStore(char A[]) //×Ö·û´®ÄæÐò´æ´¢µÄµÝ¹éËã·¨¡£ {char ch;

static int i = 0;//ÐèҪʹÓþ²Ì¬±äÁ¿ cin>>ch;

if (ch!= '.') //¹æ¶¨'.'ÊÇ×Ö·û´®ÊäÈë½áÊø±êÖ¾

XXIX

£ý

cout<<¡°Êý×Ö¡±<

for£¨i£½10£»i<36£»i++£©// Çó³ö×Öĸ×Ö·ûµÄ¸öÊý

}

{InvertStore(A);

A[i++] = ch;//×Ö·û´®ÄæÐò´æ´¢ }

A[i] = '\\0'; //×Ö·û´®½áβ±ê¼Ç

£¨3£©±àдËã·¨£¬ÊµÏÖÏÂÃ溯ÊýµÄ¹¦ÄÜ¡£º¯Êývoid insert(char*s,char*t,int pos)½«×Ö·û´®t²åÈëµ½×Ö·û´®sÖУ¬²åÈëλÖÃΪpos¡£¼ÙÉè·ÖÅä¸ø×Ö·û´®sµÄ¿Õ¼ä×ã¹»ÈÃ×Ö·û´®t²åÈë¡££¨ËµÃ÷£º²»µÃʹÓÃÈκο⺯Êý£©

[ÌâÄ¿·ÖÎö]±¾ÌâÊÇ×Ö·û´®µÄ²åÈëÎÊÌ⣬ҪÇóÔÚ×Ö·û´®sµÄposλÖ㬲åÈë×Ö·û´®t¡£Ê×ÏÈÓ¦²éÕÒ×Ö·û´®sµÄposλÖ㬽«µÚpos¸ö×Ö·ûµ½×Ö·û´®sβµÄ×Ó´®ÏòºóÒƶ¯×Ö·û´®tµÄ³¤¶È£¬È»ºó½«×Ö·û´®t¸´ÖƵ½×Ö·û´®sµÄµÚposλÖúó¡£

¶Ô²åÈëλÖÃposÒªÑéÖ¤ÆäºÏ·¨ÐÔ£¬Ð¡ÓÚ1»ò´óÓÚ´®sµÄ³¤¶È¾ùΪ·Ç·¨£¬ÒòÌâÄ¿¼ÙÉè¸ø×Ö·û´®sµÄ¿Õ¼ä×ã¹»´ó£¬¹Ê¶Ô²åÈë²»±ØÅÐÒç³ö¡£

[Ëã·¨ÃèÊö]

void insert(char *s,char *t,int pos) //½«×Ö·û´®t²åÈë×Ö·û´®sµÄµÚpos¸öλÖá£

{int i=1,x=0; char *p=s,*q=t; //p£¬q·Ö±ðΪ×Ö·û´®sºÍtµÄ¹¤×÷Ö¸Õë if(pos<1) {cout<<¡°pos²ÎÊýλÖ÷Ƿ¨¡±<

if(*p == '/0') { cout<

while(*p!= '/0') {p++; i++;} //²éµ½Î²Ê±£¬iΪ×Ö·û¡®\\0¡¯µÄϱ꣬pÒ²Ö¸Ïò¡®\\0¡¯¡£ while(*q!= '\\0') {q++; x++; } //²éÕÒ×Ö·û´®tµÄ³¤¶Èx£¬Ñ­»·½áÊøʱqÖ¸Ïò'\\0'¡£ for(j=i;j>=pos ;j--){*(p+x)=*p; p--;}//´®sµÄposºóµÄ×Ó´®ÓÒÒÆ£¬¿Õ³ö´®tµÄλÖá£

q--; //Ö¸Õëq»ØÍ˵½´®tµÄ×îºóÒ»¸ö×Ö·û

for(j=1;j<=x;j++) *p--=*q--; //½«t´®²åÈëµ½sµÄposλÖÃÉÏ

[Ëã·¨ÌÖÂÛ] ´®sµÄ½áÊø±ê¼Ç('\\0')Ò²ºóÒÆÁË£¬¶ø´®tµÄ½áβ±ê¼Ç²»Ó¦²åÈëµ½sÖС£

£¨4£©ÒÑÖª×Ö·û´®S1Öдæ·ÅÒ»¶ÎÓ¢ÎÄ£¬Ð´³öËã·¨format(s1,s2,s3,n),½«Æä°´¸ø¶¨µÄ³¤¶Èn¸ñʽ»¯³ÉÁ½¶Ë¶ÔÆëµÄ×Ö·û´®S2, Æä¶àÓàµÄ×Ö·ûËÍS3¡£

[ÌâÄ¿·ÖÎö]±¾ÌâÒªÇó×Ö·û´®s1²ð·Ö³É×Ö·û´®s2ºÍ×Ö·û´®s3£¬ÒªÇó×Ö·û´®s2¡°°´¸ø¶¨³¤¶Èn¸ñʽ»¯³ÉÁ½¶Ë¶ÔÆëµÄ×Ö·û´®¡±£¬¼´³¤¶ÈΪnÇÒÊ×β×Ö·û²»µÃΪ¿Õ¸ñ×Ö·û¡£Ëã·¨´Ó×óµ½ÓÒɨÃè×Ö·û´®s1£¬ÕÒµ½µÚÒ»¸ö·Ç¿Õ¸ñ×Ö·û£¬¼ÆÊýµ½n£¬µÚn¸ö¿½Èë×Ö·û´®s2µÄ×Ö·û²»µÃΪ¿Õ¸ñ£¬È»ºó½«ÓàÏÂ×Ö·û¸´ÖƵ½×Ö·û´®s3ÖС£

[Ëã·¨ÃèÊö]

XXX

void format (char *s1,*s2,*s3)

//½«×Ö·û´®s1²ð·Ö³É×Ö·û´®s2ºÍ×Ö·û´®s3£¬ÒªÇó×Ö·û´®s2Êdz¤nÇÒÁ½¶Ë¶ÔÆë {char *p=s1, *q=s2; int i=0;

}

£¨5£©Éè¶þάÊý×éa[1..m, 1..n] º¬ÓÐm*n ¸öÕûÊý¡£

¢Ù дһ¸öËã·¨ÅжÏaÖÐËùÓÐÔªËØÊÇ·ñ»¥²»Ïàͬ?Êä³öÏà¹ØÐÅÏ¢(yes/no)£» ¢Ú ÊÔ·ÖÎöËã·¨µÄʱ¼ä¸´ÔӶȡ£ ¢Ù

[ÌâÄ¿·ÖÎö]Åж϶þάÊý×éÖÐÔªËØÊÇ·ñ»¥²»Ïàͬ£¬Ö»ÓÐÖð¸ö±È½Ï,ÕÒµ½Ò»¶ÔÏàµÈµÄÔªËØ£¬¾Í¿É½áÂÛΪ²»ÊÇ»¥²»Ïàͬ¡£ÈçºÎ´ïµ½Ã¿¸öÔªËØͬÆäËüÔªËرȽÏÒ»´ÎÇÒÖ»Ò»´Î£¿ÔÚµ±Ç°ÐУ¬Ã¿¸öÔªËØҪͬ±¾ÐкóÃæµÄÔªËرȽÏÒ»´Î£¨ÏÂÃæµÚÒ»¸öÑ­»·¿ØÖƱäÁ¿pµÄforÑ­»·£©£¬È»ºóͬµÚi+1Ðм°ÒÔºó¸÷ÐÐÔªËرȽÏÒ»´Î£¬Õâ¾ÍÊÇÑ­»·¿ØÖƱäÁ¿kºÍpµÄ¶þ²ãforÑ­»·¡£

[Ëã·¨ÃèÊö]

int JudgEqual(ing a[m][n],int m,n)

//Åж϶þάÊý×éÖÐËùÓÐÔªËØÊÇ·ñ»¥²»Ïàͬ£¬ÈçÊÇ£¬·µ»Ø1£»·ñÔò£¬·µ»Ø0¡£ {for(i=0;i

{for(p=j+1;p

if(a[i][j]==a[i][p]) {cout<<¡°no¡±; return(0); } //Ö»ÒªÓÐÒ»¸öÏàͬµÄ£¬¾Í½áÂÛ²»ÊÇ»¥²»Ïàͬ

XXXI

while(*p!= '\\0' && *p== ' ') p++;//Â˵ôs1×ó¶Ë¿Õ¸ñ

if(*p== '\\0') {cout<<\×Ö·û´®s1Ϊ¿Õ´®»ò¿Õ¸ñ´®\} while( *p!='\\0' && i

if(*p =='\\0'){cout<<\×Ö·û´®s1ûÓÐ\¸öÓÐЧ×Ö·û\if(*(--q)==' ' ) //Èô×îºóÒ»¸ö×Ö·ûΪ¿Õ¸ñ£¬ÔòÐèÏòºóÕÒµ½µÚÒ»¸ö·Ç¿Õ¸ñ×Ö·û {p-- ; //pÖ¸ÕëÒ²ºóÍË

while(*p==' '&&*p!='\\0') p++;//Íùºó²éÕÒÒ»¸ö·Ç¿Õ¸ñ×Ö·û×÷´®s2µÄβ×Ö·û if(*p=='\\0')

{cout<<\´®Ã»ÓÐ\¸öÁ½¶Ë¶ÔÆëµÄ×Ö·û´®\ *q=*p; //×Ö·û´®s2×îºóÒ»¸ö·Ç¿Õ×Ö·û *(++q)='\\0'; //ÖÃs2×Ö·û´®½áÊø±ê¼Ç }

*q=s3;p++; //½«s1´®ÆäÓಿ·ÖËÍ×Ö·û´®s3¡£ while (*p!= '\\0') {*q=*p; q++; p++;} *q='\\0'; //Öô®s3½áÊø±ê¼Ç

for(k=i+1;k

if(a[i][j]==a[k][p]) { cout<<¡°no¡±; return(0); }

}// for(j=0;j

cout<<¡°yes¡±; return(1); //ÔªËØ»¥²»Ïàͬ }//Ëã·¨JudgEqual½áÊø

¢Ú¶þάÊý×éÖеÄÿһ¸öÔªËØͬÆäËüÔªËض¼±È½ÏÒ»´Î£¬Êý×éÖй²m*n¸öÔªËØ£¬µÚ1¸öÔªËØͬÆäËüm*n-1¸öÔªËرȽϣ¬µÚ2¸öÔªËØͬÆäËüm*n-2 ¸öÔªËرȽϣ¬??£¬µÚm*n-1¸öÔªËØͬ×îºóÒ»¸öÔªËØ(m*n)±È½ÏÒ»´Î,ËùÒÔÔÚÔªËØ»¥²»ÏàµÈʱ×ܵıȽϴÎÊýΪ (m*n-1)+(m*n-2)+?+2+1=£¨m*n£©(m*n-1)/2¡£ÔÚÓÐÏàͬԪËØʱ,¿ÉÄܵÚÒ»´Î±È½Ï¾ÍÏàͬ,Ò²¿ÉÄÜ×îºóÒ»´Î±È½ÏʱÏàͬ,ÉèÔÚ(m*n-1)¸öλÖÃÉϾù¿ÉÄÜÏàͬ,ÕâʱµÄƽ¾ù±È½Ï´ÎÊýԼΪ£¨m*n£©(m*n-1)/4£¬×ܵÄʱ¼ä¸´ÔÓ¶ÈÊÇO(n)¡£

(6)ÉèÈÎÒân¸öÕûÊý´æ·ÅÓÚÊý×éA(1:n)ÖУ¬ÊÔ±àдËã·¨£¬½«ËùÓÐÕýÊýÅÅÔÚËùÓиºÊýÇ°Ã棨ҪÇóËã·¨¸´ÔÓ¶ÈΪ0(n)£©¡£

[ÌâÄ¿·ÖÎö]±¾ÌâÊôÓÚÅÅÐòÎÊÌ⣬ֻÊÇÅųöÕý¸º£¬²»Åųö´óС¡£¿ÉÔÚÊý×éÊ×βÉèÁ½¸öÖ¸ÕëiºÍj£¬i×ÔСÖÁ´óËÑË÷µ½¸ºÊýÍ£Ö¹£¬j×Ô´óÖÁСËÑË÷µ½ÕýÊýÍ£Ö¹¡£È»ºóiºÍjËùÖ¸Êý¾Ý½»»»£¬¼ÌÐøÒÔÉϹý³Ì£¬Ö±µ½ i=jΪֹ¡£

[Ëã·¨ÃèÊö]

void Arrange(int A[],int n)

//n¸öÕûÊý´æÓÚÊý×éAÖУ¬±¾Ëã·¨½«Êý×éÖÐËùÓÐÕýÊýÅÅÔÚËùÓиºÊýµÄÇ°Ãæ {int i=0,j=n-1,x; //ÓÃÀàC±àд£¬Êý×éϱê´Ó0¿ªÊ¼ while(i

{while(i0) i++; while(i

if(i

}// while(i

[Ëã·¨ÌÖÂÛ]¶ÔÊý×éÖÐÔªËظ÷±È½ÏÒ»´Î£¬±È½Ï´ÎÊýΪn¡£×î¼ÑÇé¿ö(ÒÑÅźÃ,ÕýÊýÔÚÇ°,¸ºÊýÔÚºó)²»·¢Éú½»»»£¬×î²îÇé¿ö(¸ºÊý¾ùÔÚÕýÊýÇ°Ãæ)·¢Éún/2´Î½»»»¡£ÓÃÀàc±àд£¬Êý×é½çżÊÇ0..n-1¡£¿Õ¼ä¸´ÔÓ¶ÈΪO(1).

XXXII

4