日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

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

生活随笔

當(dāng)前位置: 首頁(yè) >

面试的算法1(C语言)(整理)(组合数 字符串倒置 最大公共串)

發(fā)布時(shí)間:2025/10/17 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 面试的算法1(C语言)(整理)(组合数 字符串倒置 最大公共串) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/*求組合數(shù): 求n個(gè)數(shù)(1....n)中k個(gè)數(shù)的組合....如:combination(5,3)要求輸出:543,542,541,532,531,521,432,431,421,321, *///方法1 #include <stdio.h> #include<malloc.h> int *a,n,k;; int counts=0; void comb(int m,int k) { int i,j;for (i=m;i>=k;i--){ a[k]=i;if (k>1)comb(i-1,k-1);else{ counts++; for (j=a[0];j>0;j--)printf("%d ",a[j]);printf("\n");}} } void main() { printf("Please input n:");scanf("%d",&n);printf("Please input k:");scanf("%d",&k);a=(int*)malloc(sizeof(int)*(k+1));a[0]=k;comb(n,k);printf("All kinds is:%d\n",counts); }//方法2 #include<stdio.h> #include<malloc.h> int n,k,*a; void comb(int N,int K) {int i;int cnt=k-K;if(cnt<k){for(i=N;i>=k-cnt;i--){a[cnt]=i;comb(i-1,K-1);}}else{for(i=0;i<k;i++)printf("%d ",a[i]);printf("\n");} } void main() {printf("Please input n:");scanf("%d",&n);printf("Please input k:");scanf("%d",&k);a=(int*)malloc(sizeof(int)*k);comb(n,k); } /* 用指針的方法,將字符串“ABCD1234efgh”前后對(duì)調(diào)顯示 */ #include <stdio.h> #include <string> void main() {char str[]="ABCD1234efgh";printf("原字符串:%s\r\n",str);char temp;int length;length=strlen(str);char *p1,*p2;p1=str;p2=(str+length-1);int cnt=length/2;while(cnt--){temp=*p1;*p1=*p2;*p2=temp;p1++;p2--;}printf("倒置的字符串:%s\r\n",str); } /* 給定字符串A和B,輸出A和B中的最大公共子串。比如A="aocdfe" B="pmcdfa" 則輸出"cdf" */ #include <stdio.h> #include <stdlib.h> #include <string.h>char *commanstring(char shortstring[],char longstring[]) {int i,j;char *substring =(char*) malloc(256);if(strstr(longstring,shortstring)!=NULL)//shortstring完全在longstring中出現(xiàn)//strstr() 函數(shù)搜索一個(gè)字符串在另一個(gè)字符串中的第一次出現(xiàn)。找到所搜索的字符串,則該函數(shù)返回第一次匹配的字符串的地址;如果未找到所搜索的字符串,則返回NULL。//char *strstr(char *str1, const char *str2);//str1被查找目標(biāo) str2: 要查找對(duì)象<span style="white-space:pre"> </span>return shortstring;for(i=strlen(shortstring)-1;i>0;i--)//i復(fù)制的字符個(gè)數(shù){for(j=0;j<=strlen(shortstring)-i;j++)//j控制從shortstring第幾位開(kāi)始復(fù)制{memcpy(substring,&shortstring[j],i);//memcpy函數(shù)的功能是從源src所指的內(nèi)存地址的起始位置開(kāi)始拷貝n個(gè)字節(jié)到目標(biāo)dest所指的內(nèi)存地址的起始位置中。//void *memcpy(void *dest, const void *src, size_t n);substring[i]='\0';if(strstr(longstring,substring)!=NULL)return substring;}}return NULL; }void main(void) {char *str1 = "aocdfe";char *str2 = "pmcdfa";char *comman = NULL;if(strlen(str1)>strlen(str2))comman= commanstring(str2,str1);elsecomman = commanstring(str1,str2);printf("the longest comman string is:%s\n",comman); }


總結(jié)

以上是生活随笔為你收集整理的面试的算法1(C语言)(整理)(组合数 字符串倒置 最大公共串)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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