《牛客网》——华为机试在线训练——找最长连续数字串

例如输入:abcd123.4567.890.123

输出:123.4567.890

“.”可以作为连接符连接两个数字,但是要求"."两边必须都是数字。

面试时遇到的题,因为忽略了对‘0’的处理,所以挂了。

 

#include<iostream>
#include<stdio.h>

#define MAX_SIZE 0x500

bool IsDotCorrect(char*szInput, int i, int nLastCmpPos, int nLen)//Check if it's a correct dot.
{
    if (nLastCmpPos < 0)
    {
        return false;
    }

    if ((i - 1 >= 0 && i + 1 <= nLen - 1 && (szInput[i - 1] >= '0' && szInput[i - 1] <= '9') && (szInput[i + 1] >= '0' && szInput[i + 1] <= '9')) && ((szInput[i + 1] - szInput[nLastCmpPos]) == 1 && szInput[nLastCmpPos] != '0'))
    {
        return true;
    }

    return false;
}
int main()
{
    char szInput[MAX_SIZE] = { 0 };
    gets_s(szInput, MAX_SIZE);

    int nLen = strlen(szInput);
    int nCount = 0;
    int nCountPos = 0;
    bool bIsCount = false;
    int nLastCmpPos = -1;
    int nCountBak = 0;
    int nCountPosBak = 0;

    for (int i = 0; i < nLen; i++)
    {
        if (!bIsCount)//Is Count Start
        {
            nCountPos = i;
        }

        if ((szInput[i] >= '0' && szInput[i] <= '9') || (szInput[i] == '.' && IsDotCorrect(szInput, i, nLastCmpPos, nLen)))//correct char
        {
            bIsCount = true;
            if (szInput[i] == '.')//correct dot ++
            {
                nCount++;
            }
            else
            {
                if (i - 1 >= 0)//if it's first correct char, no need to compare
                {
                    if (nCount == 0) //first num in this loop ,no need to compare
                    {
                        nCount++;
                    }
                    else
                    {
                        if (((szInput[i] - szInput[nLastCmpPos]) == 1) && szInput[nLastCmpPos] != '0')
                        {
                            nCount++;
                        }
                        else if (szInput[i] == '0' && szInput[nLastCmpPos] == '9')
                        {
                            nCount++;
                        }
                        else
                        {
                            bIsCount = false;
                        }

                        /*if (szInput[i] == '0' && szInput[nLastCmpPos] == '9')
                        {
                        nCount++;
                        }*/
                        
                    }
                }
                else//first num ++
                {
                    nCount++;
                }
                nLastCmpPos = i;
            }

            if (!bIsCount)//Count start
            {
                bIsCount = false;//Count again

                if (nCountBak <= nCount)
                {
                    nCountBak = nCount;
                    nCountPosBak = nCountPos;
                }

                nCountPos = 0;
                nCount = 0;
                nLastCmpPos = 0;
            }
        }
        else//incorrect char
        {
            bIsCount = false;//Count again

            if (nCountBak <= nCount)
            {
                nCountBak = nCount;
                nCountPosBak = nCountPos;
            }

            nCountPos = 0;
            nCount = 0;
            nLastCmpPos = 0;
        }
    }

    if (nCountBak <= nCount)
    {
        nCountBak = nCount;
        nCountPosBak = nCountPos;
    }
    //abcd123.4567.890.123
    //b12.34.12
    for (int i = 0; i < nCountBak; i++)
    {
        printf("%c", *(szInput + nCountPosBak + i));
    }
    printf("\n");
    system("pause");

}

 

posted @ 2019-11-27 07:57  _No.47  阅读(887)  评论(0编辑  收藏  举报