字符串中找出连续最长的数字串
来源:互联网 发布:网络布线从入门到精通 编辑:程序博客网 时间:2024/05/30 04:13
/* 功能:在字符串中找出连续最长的数字串,并把这个串的长度返回
函数原型:
unsigned int Continumax(char** pOutputstr, char* intputstr)
输入参数:
char* intputstr 输入字符串
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串
pOutputstr 指向的内存应该在函数内用malloc函数申请,由调用处负责释放
返回值:
连续最长的数字串的长度
函数原型:
unsigned int Continumax(char** pOutputstr, char* intputstr)
输入参数:
char* intputstr 输入字符串
输出参数:
char** pOutputstr: 连续最长的数字串,如果连续最长的数字串的长度为0,应该返回空字符串
pOutputstr 指向的内存应该在函数内用malloc函数申请,由调用处负责释放
返回值:
连续最长的数字串的长度
*/
该代码AC通过
#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAX_LEN 20unsigned int Continumax(char** pOutputstr, char* inputstr){ unsigned int maxlen=0; unsigned int maxindex=0; unsigned int i,j; unsigned int t=0; *pOutputstr=(char *)malloc(sizeof(char)*20); if(inputstr==NULL) { memcpy(*pOutputstr,"",sizeof("")); return 0; } for(i=0;i<strlen(inputstr);) { unsigned int tmplen=0; if(inputstr[i]>='0' && inputstr[i]<='9') { tmplen++; for(j=i+1;j<strlen(inputstr);j++) { if(inputstr[j]>='0' && inputstr[j]<='9') { tmplen++; } else { break; } } if(tmplen>=maxlen) { maxlen=tmplen; maxindex=i; } i=i+tmplen+1; } else { i++; } } if(maxlen != 0) { for(i=maxindex;i<maxindex+maxlen;i++,t++) { (* pOutputstr)[t]=inputstr[i]; } (* pOutputstr)[t]='\0'; } else { memcpy(*pOutputstr,"",sizeof("")); } return maxlen;}
下面代码AC 9个测试用例,2个测试用例没有通过,待调试
unsigned int Continumax(char** pOutputstr, char* intputstr){ unsigned int startindex; unsigned int maxlen=0; unsigned int tmpstartindex=0; unsigned int Isnumstr=0; unsigned int IsContinuStr=0;//判断 unsigned int i; unsigned int tmplen=0; unsigned int t=0; if(intputstr==NULL) { memcpy(pOutputstr,"",sizeof("")); return 0; for(i=0;i<=strlen(intputstr);i++) { if(intputstr[i]>='0' && intputstr[i]<='9') { tmplen++; if(IsContinuStr==0) { IsContinuStr=1; tmpstartindex=i; } else continue; } else { IsContinuStr=0; if(tmplen>=maxlen) { maxlen=tmplen; startindex=tmpstartindex; tmplen=0; } } } if(maxlen!=0) { for(i=startindex;i<(startindex+maxlen);i++,t++) { (*pOutputstr)[t]=intputstr[i]; } (*pOutputstr)[t]='\0'; } else { memcpy(*pOutputstr,"",sizeof("")); } return maxlen;}int main(void){char *inputstr=(char *)malloc(sizeof(char )*MAX_LEN);char *outputstr=(char *)malloc(sizeof(char)*MAX_LEN);if(inputstr==NULL || outputstr==NULL){printf("Error\n");exit(-1);}gets(inputstr); printf("%d ", Continumax(&outputstr,inputstr));printf("%s\n",outputstr);free(inputstr);free(outputstr);system("pause");return 0;}
0 0
- 在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 25、在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串(25)
- 在字符串中找出连续最长的数字串
- 字符串中找出连续最长的数字串
- 25. 在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 在字符串中找出连续最长的数字串
- 黑马程序员-------代码实现1
- ZigZag Conversion
- JavaScript传各种方法值
- 我所理解的抽象
- Android 系统上抓取数据包方法
- 字符串中找出连续最长的数字串
- 机房重构 报表 “不能传泛型?”
- MySQL从5.5升级到5.6,TIMESTAMP的变化
- apt-get常用命令
- 2015河南ACM省赛 - 小记
- Centos 6.5安装git
- Jquery基础教程之-选择元素
- xts可扩展的时间序列
- CloudSimExample1-仿真步骤