北电笔试题,谁给出答案.越精巧越好

zez 2005-09-07 11:55:10
输入字符串 inPutStr="ads34EFsdsl456789DDasFdiel3456ld345678AAA"
输出 outPutStr="456789"
返回 6

即在字符串中找出 第一个最大长度的连续的数, 返回长度.

int fun(char* outPutStr, char* inPutStr)
{

}
...全文
2842 62 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
62 条回复
切换为时间正序
请发表友善的回复…
发表回复
youzelin 2005-11-19
  • 打赏
  • 举报
回复
不好意思,没注意到注释是乱码
youzelin 2005-11-19
  • 打赏
  • 举报
回复
VC 6.0 编译通过,完全符合楼主您的需求

#include <string.h>
#include <ctype.h>
#include <iostream>
using namespace std;

int fun (char * & outPutPtr, char * inPutPtr) {
int i = 0, j = 0, p = 0; // ÁÙʱϱê
int maxLen = 0; // ÐèÒª·µ»ØµÄ×î´óÊý×Ö´®³¤¶È
int inLen = strlen(inPutPtr);
while (i < inLen) {
while (i < inLen && !isdigit(inPutPtr[i])) {
i++; if (!maxLen) j = i;
}
if (i >= inLen) {
if (maxLen) {
outPutPtr = new char[maxLen + 1];
for (int m = 0; m < maxLen; m++) outPutPtr[m] = inPutPtr[j + m];
outPutPtr[maxLen] = '\0';
}
return maxLen; // ×Ö·û´®×ßÍ꣬¼´µ±·µ»Ø
}
for (p = i + 1; p < inLen && isdigit(inPutPtr[p]); p++);
maxLen = ((p - i) > maxLen) ? (p - i) : maxLen;
if (p >= inLen) return maxLen;
if (!isdigit(inPutPtr[j + maxLen - 1])) j = i;
i = p + 1;
}
outPutPtr = new char[maxLen + 1];
for (int m = 0; m < maxLen; m++) outPutPtr[m] = inPutPtr[j + m];
outPutPtr[maxLen] = '\0';
return maxLen;
}

void main () {
char * in = "ads34EFsdsl456789DDasFdiel3456ld345678AAA";
char * out = NULL;
int len = fun(out, in);
if (out) cout << out << endl;
cout << len << endl;
}
Leaveye 2005-11-19
  • 打赏
  • 举报
回复
倒,竟然漏了东西。重来:

int fun(char* outPutStr, char* inPutStr)
{
int maxCnt = 0, currCnt = 0, i;
char *pNumStr;

if(inPutStr == NULL) {
return 0;
}

while(*inPutStr) {
switch(*inPutStr) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
++currCnt;
break;
default:
if(currCnt > maxCnt)
pNumStr = inPutStr - currCnt;
currCnt = 0;
break;
}
++inPutStr;
}

for(i = 0; i < maxCnt; ++i) {
outPutStr[i] = pNumStr[i];
}
outPutStr[i] = '\0';

return maxCnt;
}
Leaveye 2005-11-19
  • 打赏
  • 举报
回复
小写一个:

int fun(char* outPutStr, char* inPutStr)
{
int maxCnt = 0, currCnt = 0, i;
char *pNumStr, *p = inPutStr;

if(inPutStr == NULL) {
return 0;
}

while(*inPutStr) {
switch(*inPutStr) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
++currCnt;
break;
default:
if(currCnt > maxCnt)
pNumStr = inPutStr - currCnt;
currCnt = 0;
break;
}
}

for(i = 0; i < maxCnt; ++i) {
outPutStr[i] = pNumStr[i];
}
outPutStr[i] = '\0';

return maxCnt;
}
chen_fu 2005-11-19
  • 打赏
  • 举报
回复

谢谢nyingchi()提的意见,我把代码改进了一下 ;)
不过我不得不说一句:nyingchi()的代码还有逻辑错误,应该用<= 和>=,,并且if用的不太好。
chen_fu 2005-11-19
  • 打赏
  • 举报
回复
int fun(char* outPutStr, char* inPutStr)
{
if(inPutStr == NULL || outPutStr==NULL)
return 0;

int MaxLen =0;
int numLen = 0;
char* maxStrEnd;

while(*inPutStr != '\0')
{
if(*inPutStr >='0' && *inPutStr <='9')
{
numLen++;
}
else
{
if(numLen > MaxLen)
{
MaxLen=numLen;
maxStrEnd = inPutStr;
}

numLen = 0;
}

inPutStr ++;
}
if(numLen > MaxLen)
{
MaxLen=numLen;
maxStrEnd = inPutStr;
}

maxStrEnd--;

outPutStr[MaxLen] = '\0';
for(int i= MaxLen -1; i >=0; i--)
{
outPutStr[i]= *maxStrEnd--;
}

return MaxLen;
}
THK007 2005-11-18
  • 打赏
  • 举报
回复
mark
guyanhun 2005-11-18
  • 打赏
  • 举报
回复
int fun(char* outPutStr, char* inPutStr)
{
assert( outPutStr !=NULL && inPutStr != NULL );
char *it ,*temp ,ch ;
temp =inPutStr;
int count =0, result =0;
while ((ch = *temp )!='\0')
{
if( ch >= '0' && ch <= '9')
{
++count ;
if(count > result )
{
result =count;
it = temp -result +1 ;
}
}
else
count =0;
++temp ;
}
memcpy(outPutStr ,it ,result );
outPutStr[result] = '\0';
return result ;
}

郁闷, 0 点, 楼主也太...
nyingchi 2005-11-18
  • 打赏
  • 举报
回复
chen_fu(精灵东东)
你的代码虽然和我的只有一点点不同
但是你的效率低很多的你不觉吗?
cxyol 2005-11-18
  • 打赏
  • 举报
回复
mark
chen_fu 2005-11-18
  • 打赏
  • 举报
回复
int fun(char* outPutStr, char* inPutStr)
{
int len =0, MaxLen =0;
char* inTempChar, *outTempChar;
while(*inPutStr != '\0')
{
int numLength = 0;

inTempChar =inPutStr;

while(*inPutStr >='0' && *inPutStr <='9')
{
numLength++;
inPutStr++;
}
if(numLength > MaxLen)
{
MaxLen = numLength;
outTempChar = outPutStr;
while(inTempChar < inPutStr)
*outTempChar++ = *inTempChar++;
*outTempChar='\0';
}

inPutStr ++;
}
return MaxLen;
}
nyingchi 2005-11-18
  • 打赏
  • 举报
回复
//查找一个串里面最长的数字字符串
//str待查找的字符串
//MaxNumString找到的最长的字符串
//时间复杂度: strlen(str)
int FindMaxNumberString(const char *str, char *MaxNumString)
{
int maxLength=0;
int maxPos=0;
int pos=0;
while( str[pos]!='\0')
{
if(str[pos] >'0' &&str[pos]<'9')
{ //如果是数字的话,就计算这个数字相邻的数字的个数
int len=0;
while( str[pos] >'0' && str[pos]<'9')
{
len++;
pos++;
}
if(len > maxLength)
{ //如果这里的数字串,比当前计算得到的数字串还长
//那么他就是最长当前最长的数字串
maxLength= len;
//记录下数字串的开始位置
maxPos= pos-len;
}
}
else
pos++;
}
//得到最长的字符串
for(int i=0; i< maxLength ; i++)
MaxNumString[i]=str[i+maxPos];
MaxNumString[i]='\0';
//返回最长字符串的长度
return maxLength;
}
eternall 2005-11-18
  • 打赏
  • 举报
回复
int fun(char* outPutStr, char* inPutStr)
{
char *pStart, *pEnd;
int StrLen,curDigiLen = 0;

StrLen = strlen(inPutStr);
pStart = pEnd = (char*)inPutStr;

for(int i=0; i<StrLen; i++)
{
if(*pEnd>= '0' && *pEnd<='9')
{
pEnd++;
}
else
{
pEnd ++;
pStart = pEnd;
}

if(pEnd-pStart>curDigiLen)
{
memcpy(outPutStr, pStart, pEnd-pStart);
curDigiLen = pEnd-pStart;
}
}

return curDigiLen;
}
gdcnliuy 2005-11-18
  • 打赏
  • 举报
回复
int Find(char *pFind, char *pMax)
{
int iMBegin = 0, iMLast = 0, iFBegin = 0, iFLast=0;
int iLength = strlen(pFind);
char *p = new char[iLength +1];
strcpy(p,pFind);
for(int i = 0; i<iLength; i++) //找出最大的数字位置
{
if((('9'+1)> p[i]) && (p[i] >( '0'-1))) //是数字
{
if( iFBegin == iFLast)//开始计数
{
iFBegin = i;
}
else
iFLast = i;
}
else
{
if( (iMLast-iMBegin)<(iFLast-iFBegin))//有更大的数
{
iMBegin = iFBegin;
iMLast = iFLast;
iFBegin=iFLast= 0;
}
else
{
iFBegin = iFLast = 0;
}
}
}
string pp = pFind;
string get = pp.substr(iMBegin,iMLast-iMBegin+1);
strcpy(pMax,get.c_str());
return iMLast-iMBegin;

}
youngdreamer2008 2005-09-26
  • 打赏
  • 举报
回复
我觉得新手的大忌就是:简单问题复杂化
想起了把大象装入冰箱需要几部的问题:-)
思路就是:
int numlen=0;
outputstr="\0";
然后for (char*p=inputstr;*p!='\0';p++)
{if(isnum(*p))
int templen=1;
while(*(p+templen) 是数字)
{templen++; }
再判断if (templen>numlen)
{numlen=templen;
outputchar=p;
outputchar+numlen='\0';}
最后 return numlen;
zez 2005-09-26
  • 打赏
  • 举报
回复
谁也没有想太复杂,楼上各位以及我的程序都是用的这个最简单的逻辑. 但你这样写就十几行,换成代码,肯定不比上面任何一个函数少多少行的..
勉励前行 2005-09-26
  • 打赏
  • 举报
回复
//將first化為整數,+1後看next與其是否相等
inline char *IsNumberAddOne(char *first,char *next)
{// assert(next > first && n > 0 && n < 10)
if(first + 1 == next ){
if(*first +1 == *next && isdigit(*next))
return next ;
else if ( *first == '9' && *next == '1' && *(next+1) == '0')
return ++next ;
else
return NULL ;
}
if( *(next-1) < '0' || *(next-1) > '9')
return NULL ;
char tmp = *next ;
*next = 0 ;
int A = atoi(first);
*next = tmp ;
if(A == 0 && *first != '0')
return NULL ;
char buff[10];
itoa(++A,buff,10);
int Len = strlen(buff) ;
if(strncmp(buff,next,Len) == 0)
return next + Len -1;
return NULL ;
}
inline char * FindContinueNumber2(char *Source)
{
char *end = Source ;
for(int i = 1 ; i < 10 ; ++i) {
char *first = Source;
char *next = first + i ;
if(*next< '0' || *next > '9')
break ;
while( *first ){
char *p = IsNumberAddOne(first,next);
if(p) {
first = next ;
next = p + 1;
if( p > end )
end = p ;
}
else
break ;
}
}
return end ; ;
}
int func(char* output, char * input)
{
char *start ;
char *end ;
int maxLen = 0 ;
for(char *p = input ; *p ; ++p) {
if(isdigit(*p)) {
char *tmpstart = p ;
p = FindContinueNumber2(p);
if( p - tmpstart + 1 > maxLen) {
maxLen = p - tmpstart + 1;
start = tmpstart ;
end = p + 1 ;
}
}
}
strncpy(output,start,maxLen);
output[maxLen] = 0 ;
return maxLen ;
}

int main(int argc, char* argv[])
{
const int MAX_STR_LEN = 128;//根据入串的长度,来确定输出串的最大长度
char* inPutStr="ads123425678EFsdsl979899100101102103AAA";
char outPutStr[MAX_STR_LEN];
int len;

len = func(outPutStr, inPutStr);

printf("len = %d, str = %s\n", len, outPutStr);//979899100101102103

return 0;
}
goodzyx 2005-09-25
  • 打赏
  • 举报
回复
#include "iostream.h"
#include "string.h"

bool IsNumber(char t)
{
if ( t >= '0' && t<='9' )
return true;
else
return false;
}

int fun(char* outPutStr, char* inPutStr)
{
char p1,p2;
int length = strlen(inPutStr);

int pos=0;
int m=0,n=0;

for(int i=1;i<length;i++)
{
p1=inPutStr[i];
p2=inPutStr[i-1];
if (IsNumber(p1) && IsNumber(p2))
{
n++;
}
else
{
if (n>m) {
pos = i-n-1;
m=n;
}
n=0;
}
}

m=m+1;
for(i=0;i<m;i++)
outPutStr[i] = inPutStr[pos+i];

return m;
}

int main(int argc, char* argv[])
{
char in[]="ads34EFsdsl456789DDasFdiel3456ld345678AAA";
char *out= new char[strlen(in)];
for(int i=0;i<strlen(in);i++)
out[i]=' ';
int a=fun(out,in);
if (a==1) cout<<"对不起没有连续的数字"<<endl;
else
cout<<a<<" "<<out<<endl;
return 0;
}
勉励前行 2005-09-24
  • 打赏
  • 举报
回复
上面的函數原型有個錯誤 int func(char* output, const char * input)
改為:int func(char* output, char * input) 即可。沒調試,真的出錯了,
我在試試看能否簡單地實現:識別1112131415 123124125126 這種連續數字。
勉励前行 2005-09-24
  • 打赏
  • 举报
回复
//找出連續的數字(這個是簡單的實現,只能找出連續的個位數字)
//改寫他,可實現找出連續的多位數字,如1112131415等。
inline char * FindContinueNumber(char *Source)
{
char *p = Source ;
while(isdigit(*p) && (*p + 1 == *(p+1)) )
++p ;
return ++p ;
}
//
int func(char* output, const char * input)
{
char *start ;
char *end ;
int maxLen = 0 ;
for(char *p = input ; *p ; ++p) {
if(isdigit(*p)) {
char *tmpstart = p ;
p = FindContinueNumber(p);
if( p - tmpstart > maxLen) {
maxLen = p-tmpstart ;
start = tmpstart ;
end = p ;
}
}
}
if(maxLen > 0 ) //ªð¦^µ²ªG
strncpy(output,start,maxLen);
return maxLen ;
}
加载更多回复(42)

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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