zoukankan      html  css  js  c++  java
  • 《牛客网》——华为机试在线训练——找最长连续数字串

    例如输入: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("
    ");
        system("pause");
    
    }
  • 相关阅读:
    P5049 旅行(数据加强版)(基环树)
    P5024 保卫王国(动态dp/整体dp/倍增dp)
    CF891C Envy(离线/在线+可撤销并查集/并查集/LCT)
    CF1217题解
    CF1215题解
    浅谈bitset
    CF1214题解
    CF1213F Unstable String Sort(差分)
    C++创建和使用动态链接库
    交叉编译openssl1.1.1a
  • 原文地址:https://www.cnblogs.com/predator-wang/p/11939529.html
Copyright © 2011-2022 走看看