面试的算法2(C语言)(整理)
生活随笔
收集整理的这篇文章主要介绍了
面试的算法2(C语言)(整理)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
<pre name="code" class="cpp"><pre name="code" class="cpp">/*
写一个函数,它的原形是int continumax(char *outputstr,char *intputstr)
功能:在字符串中找出连续最长的数字串,并把这个串的长度返回,并把这个最长数字串付给其中一个函数参数outputstr所指内存。例如:"abc123def123456eec123456789dd"的首地址传给intputstr后,函数将返回9,outputstr所指的值为123456789。
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//自己写的 把每个数字串都存下来再判断 对于这道题的要求 略显麻烦了 但如果要输出每个数字串 可在这个基础上改改就出来了
int FindMax_NumStr(char *outputstr,char *inputstr)
{char *in = inputstr,*out = outputstr;int numcnt=0;//记录数字串数 一共有多少次 int *count=(int*)malloc(10*sizeof(int));//存数字串的长度char *temp[10];//存数字串的地址memset(count,0,10*sizeof(int));int max=0;//下标 记录最长数字串int i;int flag=0;//flag 0:字母 1:数字if(*in>='0'&&*in<='9')flag=1;count--;//记录每个数字串以及长度while(*in!='\0'){while(*in>='0'&&*in<='9'){if(flag==0){count++;temp[numcnt]=in;numcnt++;flag=1;}if(flag==1){(*count)++;}in++;}//字母:in++;flag=0;}//比较count[],求最长数字串count=(count-numcnt+1);max=numcnt-1;int tempnum=count[numcnt-1];for(i=0;i<numcnt-1;i++)if(count[i]>tempnum){max=i;tempnum=count[i];}/**(temp[max]+tempnum)='\0';outputstr=temp[max];*///保存最长数字串for(i=0;i<tempnum;i++){*out=*temp[max];out++;temp[max]++;}*out='\0';return tempnum;
}void main(void)
{char input[]="abc123def123456eec123456789dd";char output[10];int maxlen;maxlen = FindMax_NumStr(output,input);printf("the str is :%s\n",output);printf("the maxlen is :%d\n",maxlen);
}
//答案的 每次都先作比较 只保存一个最长数字串 int FindMax_NumStr(char *outputstr,char *inputstr) {char *in = inputstr,*out = outputstr,*temp;char *final;int count = 0;int maxlen = 0;int i;while(*in!='\0'){if(*in > 47 && *in < 58){for(temp = in;*in> 47 && *in <58;in++)count++;}elsein++;if(maxlen < count){maxlen = count;count = 0;final = temp;}}for(i =0;i<maxlen;i++){*out = *final;out++;final++;}*out = '\0';return maxlen; } /* 求1000!的未尾有几个0;求出1->1000里,能被5整除的数的个数n1,能被25整除的数的个数n2,能被125整除的数的个数n3,能被625整除的数的个数n4.1000!末尾的零的个数=n1+n2+n3+n4;只要是末尾是5的数它乘以一个偶数就会出现一个0,而末尾是0的数乘以任何数也都会出现0 而末尾是0的如果是一个0肯定能被5整除,两个0肯定能被25整数,以此类推3个0就能被5的三次方整除,也就是1251000!就是1-1000数的相乘,能被5整除的所有数分别乘以一个偶数就会出现这些个的0,而例如100,既能被5整除,也能被25整除,所以就是两个01000,既能被5,25,也能被125整除,所以算三个0例如是10!=1*2*3*4*5*6*7*8*9*10,里面有两个数能被5整除,就是10和5,而 5随便乘以一个偶数就出现一个0,而10乘以其它数也会出现一个0,所以10!会有两个0 *///方法一: #include<stdio.h> #define NUM 1000void main() {int n1=0,n2=0,n3=0,n4=0;for(int i=5;i<=NUM;i=i+5){if((i>=5)&&(i%5==0))n1++;if((i>=25)&&(i%25==0))n2++;if((i>=125)&&(i%125==0))n3++;if((i>=625)&&(i%625==0))n4++;}printf("%d\r\n",n1+n2+n3+n4); }//方法二:递归 #include <stdio.h> #define NUM 1000int find5(int num) {int ret = 0;while(num%5==0){num/=5;ret++;}return ret; }void main(void) {int result = 0;int i;for(i=5;i<=NUM;i+=5)result +=find5(i);printf("the total zero number is %d\n",result); } /* 编写一个 C 函数,该函数在一个字符串中找到可能的最长的子字符串,且该字符串是由同一字符组成的。 */ //dfggggsrer455555frtrrrtvvvwwwwwwf445t56 #include <stdio.h> #include <string.h> #include <stdlib.h> #define NUM 10 //类似第一道的方法 每次比较 存出现次数最多的字符 char *find(char *str) {char finalch;//字符串字符char *final=(char*)malloc(NUM*sizeof(char));char ch;int i;int curcnt=0;int maxcnt=0;int flag=0;//第一个字符开始标志while(*str!='\0'){if(flag==0){ch=*str;curcnt++;flag=1;str++;}if(flag==1){while(*str==ch){curcnt++;str++;}flag=0;}if(curcnt>maxcnt){finalch=ch;maxcnt=curcnt;}curcnt=0;}for(i=0;i<maxcnt;i++){final[i]=finalch;}final[i]='\0';return final; }void main() {char str[]="dfggggsrer455555frtrrrtvvvwwwwwwf445t56";char *final;final=find(str);printf("最长子串为:%s\r\n",final); }
//答案的 每次都先作比较 只保存一个最长数字串 int FindMax_NumStr(char *outputstr,char *inputstr) {char *in = inputstr,*out = outputstr,*temp;char *final;int count = 0;int maxlen = 0;int i;while(*in!='\0'){if(*in > 47 && *in < 58){for(temp = in;*in> 47 && *in <58;in++)count++;}elsein++;if(maxlen < count){maxlen = count;count = 0;final = temp;}}for(i =0;i<maxlen;i++){*out = *final;out++;final++;}*out = '\0';return maxlen; } /* 求1000!的未尾有几个0;求出1->1000里,能被5整除的数的个数n1,能被25整除的数的个数n2,能被125整除的数的个数n3,能被625整除的数的个数n4.1000!末尾的零的个数=n1+n2+n3+n4;只要是末尾是5的数它乘以一个偶数就会出现一个0,而末尾是0的数乘以任何数也都会出现0 而末尾是0的如果是一个0肯定能被5整除,两个0肯定能被25整数,以此类推3个0就能被5的三次方整除,也就是1251000!就是1-1000数的相乘,能被5整除的所有数分别乘以一个偶数就会出现这些个的0,而例如100,既能被5整除,也能被25整除,所以就是两个01000,既能被5,25,也能被125整除,所以算三个0例如是10!=1*2*3*4*5*6*7*8*9*10,里面有两个数能被5整除,就是10和5,而 5随便乘以一个偶数就出现一个0,而10乘以其它数也会出现一个0,所以10!会有两个0 *///方法一: #include<stdio.h> #define NUM 1000void main() {int n1=0,n2=0,n3=0,n4=0;for(int i=5;i<=NUM;i=i+5){if((i>=5)&&(i%5==0))n1++;if((i>=25)&&(i%25==0))n2++;if((i>=125)&&(i%125==0))n3++;if((i>=625)&&(i%625==0))n4++;}printf("%d\r\n",n1+n2+n3+n4); }//方法二:递归 #include <stdio.h> #define NUM 1000int find5(int num) {int ret = 0;while(num%5==0){num/=5;ret++;}return ret; }void main(void) {int result = 0;int i;for(i=5;i<=NUM;i+=5)result +=find5(i);printf("the total zero number is %d\n",result); } /* 编写一个 C 函数,该函数在一个字符串中找到可能的最长的子字符串,且该字符串是由同一字符组成的。 */ //dfggggsrer455555frtrrrtvvvwwwwwwf445t56 #include <stdio.h> #include <string.h> #include <stdlib.h> #define NUM 10 //类似第一道的方法 每次比较 存出现次数最多的字符 char *find(char *str) {char finalch;//字符串字符char *final=(char*)malloc(NUM*sizeof(char));char ch;int i;int curcnt=0;int maxcnt=0;int flag=0;//第一个字符开始标志while(*str!='\0'){if(flag==0){ch=*str;curcnt++;flag=1;str++;}if(flag==1){while(*str==ch){curcnt++;str++;}flag=0;}if(curcnt>maxcnt){finalch=ch;maxcnt=curcnt;}curcnt=0;}for(i=0;i<maxcnt;i++){final[i]=finalch;}final[i]='\0';return final; }void main() {char str[]="dfggggsrer455555frtrrrtvvvwwwwwwf445t56";char *final;final=find(str);printf("最长子串为:%s\r\n",final); }
总结
以上是生活随笔为你收集整理的面试的算法2(C语言)(整理)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 面试的算法1(C语言)(整理)(组合数
- 下一篇: 面试中经常出现的算法1(整理)