2010百度笔试题求最长数字串的

flyinghawl 2010-09-26 07:33:09
原题如下:

/****

1·int maxcontinunum(const char *inputstr,char * outputstr)

编写一段程序实现该函数,实现返回一个以“\0”结束的字符串中最长的数字串的长度,并把该数字子串的首地址赋给outputstr。不能使用任何库函数或已经存在的函数,如strlen。

例如:在字符串“abc123abcdef12345abcdefgh123456789”中,把该字符串的首地址赋给inputstr,返回9,outputstr指向字符串“123456789”的首地址。



***/


因为事先没有拿到真正的题目,只是在描述中我就写了一个代码,编译通过了,不过和原题的要求不符合,当然了,可以改正一下,代码如下:请各位指教,哈哈。



#include <iostream>

using namespace std;

char* cz(const char* str, int len){

int start = 0, zcc = 0, sy = 0;
char * temp;

cout << "cz被调用。" << endl;

for (int k = 0; k < len; k++)
{
char ch;



ch = str[k];

if (ch >= '0' && ch <= '9')
{
sy++;

if (((k >= 1) && ((str[k-1] <= '0' || str[k-1] >= '9'))) || k ==0)
{
start = k;
sy = 1;
}
}else{

if (zcc < sy)
{
zcc = sy;
temp = (char*)(str + start);
sy = 0;
}

}

}

for(char* ls = temp; ls < zcc+temp; ls++)
cout<< *ls;

cout << endl;

cout << "数字串的长度为:" << zcc << endl;


return temp;


}


int main(){

char test[50] = "abc123deeg89m+_345678bnm367";
char* s;

s = cz(test, 27);

cout << "s的开始地址为" << &s << endl;

return 0;


}



...全文
211 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Csuxiaowu 2010-09-26
  • 打赏
  • 举报
回复
#include <iostream>
#include <cstring>
using namespace std;
int maxcontinunum(const char *inputstr,char * outputstr)
{
const char *pFirst=inputstr;
const char *p=inputstr;
int index=0;
int ilastcount=0;
//int icurrentcount=0;
while(*p!='\0')
{
cout<<"----------"<<endl;
int icurrentcount=0;
while((*p!='\0')&&(*p>='A')&&(*p<='z'))
{
// ++index;
++p;
}
while((*p>='0')&&(*p<='9')&&(*p!='\0'))
{
++p;
++icurrentcount;
}
if(icurrentcount>=ilastcount)
{

ilastcount=icurrentcount;
index=p-icurrentcount-pFirst;
}
// ++p;
}
char *pT=const_cast<char*>(pFirst+index);
//strncpy(outputstr,pT,ilastcount);
for(int i=0;i<ilastcount;i++)
*outputstr++=*pT++;

outputstr[ilastcount]='\0';
return ilastcount;
}
int main()
{
char *p=new char[32];
memset(p,0,32);
maxcontinunum("2222222222222222222222222222f122f1215ffa1111111111111111111d21034313fda131321321",p);
cout<<p<<endl;
return 0;
}
liutengfeigo 2010-09-26
  • 打赏
  • 举报
回复
int maxcontinunum(const char *inputstr,char ** outputstr)
·int maxcontinunum(const char *inputstr,char * outputstr)
ayw215 2010-09-26
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <tchar.h>
#include <cctype>
using namespace std;


int maxcontinunum(const char *inputstr,char ** outputstr)
{
int ret = 0, maxindex = 0;
*outputstr = NULL;
char* ptr = (char*)inputstr;
char* maxptr = NULL;
bool bNotindigit = true;
while (*ptr)
{
if (isdigit(*ptr))
{
if(bNotindigit)
{
*outputstr = ptr;
ret = 1;
bNotindigit = false;
}
else
ret++;
}
else{
bNotindigit = true;
ret>maxindex ? (maxindex = ret,maxptr = *outputstr):(NULL);
}
ptr++;
}
ret>maxindex ? (maxindex = ret,maxptr = *outputstr):(NULL);
return maxindex;
}

int main(){
char* pur = NULL;
int ret = maxcontinunum("123456fakdjf123kjk12345kjkj12d1234567",&pur);
printf("%d %c",ret,*pur);
return 0;
}

这种代码好久没写了,手好生
ayw215 2010-09-26
  • 打赏
  • 举报
回复
今年百度的招聘开始了么。。。。
flyinghawl 2010-09-26
  • 打赏
  • 举报
回复
哈哈,忘了判断最后一个字符是数字的情况啦。



#include <iostream>

using namespace std;

char* cz(const char* str, int len){

int start = 0, zcc = 0, sy = 0;
char * temp;

cout << "cz被调用。" << endl;

for (int k = 0; k < len; k++)
{
char ch;



ch = str[k];

if (ch >= '0' && ch <= '9')
{
sy++;

if (((k >= 1) && ((str[k-1] <= '0' || str[k-1] >= '9'))) || k ==0)
{
start = k;
sy = 1;
}

if ((k == len-1) && (zcc < sy))
{
zcc = sy;
temp = (char*)(str + start);
sy = 0;
}

}else{

if (zcc < sy)
{
zcc = sy;
temp = (char*)(str + start);
sy = 0;
}

}

}

for(char* ls = temp; ls < zcc+temp; ls++)
cout<< *ls;

cout << endl;

cout << "数字串的长度为:" << zcc << endl;


return temp;


}


int main(){

// char test[50] = "abc123deeg89m+_345678bnm367";

char test[100] = "abc123abcdef12345abcdefgh123456789";
char* s;

s = cz(test, 34);

cout << "s的开始地址为" << &s << endl;

return 0;


}





65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧