欢迎来到天天文库
浏览记录
ID:35863632
大小:25.50 KB
页数:6页
时间:2019-04-21
《2016年c语言面试算法题(附答案)》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、精品文档2016年C语言面试算法题(附答案)1.求组合数:求n个数(1....n)中k个数的组合....如:combination(5,3)要求输出:543,542,541,532,531,521,432,431,421,321,/*求组合数:求n个数(1....n)中k个数的组合....如:combination(5,3)要求输出:543,542,541,532,531,521,432,431,421,321,*/#include#includeintpop(int*);intpush(int);voidcombinat
2、ion(int,int);intstack[3]={0};inttop=-1;intmain(){intn,m;n2016全新精品资料-全新公文范文-全程指导写作–独家原创6/6精品文档=;m=;combination(n,m);printf(“”);}voidcombination(intm,intn){inttemp=m;push(temp);while(1){if(1==temp){if(pop(&temp)&&stack[0]==n)break;}elseif(push(--temp)){printf(“%d%
3、d%d”,stack[0],stack[1],stack[2]);pop(&temp);}2016全新精品资料-全新公文范文-全程指导写作–独家原创6/6精品文档}}intpush(inti){stack[++top]=i;if(topreturn0;elsereturn1;}intpop(int*i){*i=stack[top--];if(top>=0)return0;elsereturn1;}.、用指针的方法,将字符串“ABCD1234efgh”前后对调显示#include#includeintmain()2016
4、全新精品资料-全新公文范文-全程指导写作–独家原创6/6精品文档{charstr[]=“ABCD123efgh”;intlength=strlen(str);char*p1=str;char*p=str+length-1;while(p1{charc=*p1;*p1=*p2;*p=c;++p1;--p2;}printf(“strnowis%s”,str);return1;}.给定字符串A和B,输出A和B中的最大公共子串。比如A=“aocdfe”B=“pmcdfa”则输出”cdf”#include#include#inc
5、ludechar*commanstring(charshortstring[],charlongstring[2016全新精品资料-全新公文范文-全程指导写作–独家原创6/6精品文档]){inti,j;char*substring=malloc(256);if(strstr(longstring,shortstring)!=NULL)returnshortstring;for(i=strlen(shortstring)-1;i>0;i--){for(j=0;j{memcpy(substring,&shortstring[j
6、],i);substring[i]=‘ ’;if(strstr(longstring,substring)!=NULL)returnsubstring;}}returnNULL;}voidmain(void){char*str1=“aocdfe”;char*str=“pmcdfa”;char*comman=NULL;2016全新精品资料-全新公文范文-全程指导写作–独家原创6/6精品文档if(strlen(str1)>strlen(str2))comman=commanstring(str2,str1);elsecomm
7、an=commanstring(str1,str2);printf(“thelongestcommanstringis:%s”,comman);}2016全新精品资料-全新公文范文-全程指导写作–独家原创6/6
此文档下载收益归作者所有