基于visual Studio2013解决面试题之0908最大连续数字串




题目



解决代码及点评

/*
	求最大连续递增数字串(如"ads3sl456789DF3456ld345AA"中的"456789")

	这道题的解法还是从头到尾的遍历即可
*/

#include <iostream>
using namespace std;

int FindMaxLen(char *pszBuf, char *pszOutPut)
{
    int nMaxLen = 0;
    int nTmpLen = 0;
    int nTmpPos;
    int nMaxPos;
    int i, j;
    if (pszBuf == NULL)
    {
        return 0;
    }
    for (i = 0; i < strlen(pszBuf); i++)
    {
        nTmpLen = 0;
        if (pszBuf[i] >= '0' && pszBuf[i] <= '9')
        {
            nTmpPos = i;
            nTmpLen++;
            while(pszBuf[i] >= '0' && pszBuf[i] <= '9'&&
                i < strlen(pszBuf) && pszBuf[i] < pszBuf[i+1]&&
                pszBuf[i+1] >= '0' && pszBuf[i+1] <= '9')
            {
                nTmpLen++;
                i++;
            }
            if(nTmpLen > nMaxLen)
            {
                nMaxLen = nTmpLen;
                nMaxPos = nTmpPos;
            }
        }
    }
    for (i = nMaxPos, j = 0; i < nMaxPos + nMaxLen; i++)
    {
        pszOutPut[j++] = pszBuf[i];
    }

    return nMaxLen;
}

int main()
{
    char szBuf1[] = "ads3sl456789DF3456ld345AA";
    char szBufOut[100] = {0};
    int nLen = FindMaxLen(szBuf1, szBufOut);
    cout<<"最大连续递增数字串为:"<<szBufOut<<endl;
    system("pause");
    return 0;
}

代码下载及其运行

代码下载地址:http://download.csdn.net/detail/yincheng01/6704519

解压密码:c.itcast.cn


下载代码并解压后,用VC2013打开interview.sln,并设置对应的启动项目后,点击运行即可,具体步骤如下:

1)设置启动项目:右键点击解决方案,在弹出菜单中选择“设置启动项目”


2)在下拉框中选择相应项目,项目名和博客编号一致

3)点击“本地Windows调试器”运行


程序运行结果









posted on 2013-12-20 01:29  三少爷的剑123  阅读(154)  评论(0编辑  收藏  举报

导航