zoukankan      html  css  js  c++  java
  • 一道来自华为的C机试题目

    题目是这样的

    求一个字符串中连续字母的个数

    比如I have a book. : 1

    I have a dog. : 0

    I haavee aa dogg : 4


    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    void GetDupStringCount( const char* pStr, int &iOut )
    {
    	if( !pStr )
    		return;
    
    	int iLen = strlen( pStr );
    
    	if( !iLen )
    		return;
    
    	char cValue = *pStr;
    	int iNumCount = 0;//重复个数
    
    	iOut = 0;//设置为0
    	int iIndex = 0;//当前索引
    
    	while( iLen-- )
    	{
    		if( !( ( cValue >= 'a' && cValue <= 'z' ) || ( cValue >= 'A' && cValue <= 'Z' ) ) )
    		{
    			cValue = *( pStr + ++iIndex );
    
    			iNumCount = 0;
    			
    			continue;
    		}
    
    		if( cValue == *( pStr + iIndex ) )
    		{
    			iNumCount++;
    		}
    		else
    		{
    			cValue = *( pStr + iIndex );
    
    			if( iNumCount > 1 )
    			{
    				iOut++;
    				iNumCount = 0;
    				--iIndex;
    			}
    
    		}
    
    		iIndex++;
    
    	}
    }
    
    
    int main( int argc, char* argv[] )
    {
    	char szStr[] = " I haveee  a book!   ";
    
    	int iOut = -1;
    
    	GetDupStringCount( szStr, iOut );
    
    	cout << iOut << endl;
    
    	return 0;
    }


  • 相关阅读:
    P2426 删数
    P2115 [USACO14MAR]破坏Sabotage
    P2679 子串
    P2979 [USACO10JAN]奶酪塔Cheese Towers
    P1114 “非常男女”计划
    P2105 K皇后
    P4053 [JSOI2007]建筑抢修
    P1294 高手去散步
    P4316 绿豆蛙的归宿
    P2253 好一个一中腰鼓!
  • 原文地址:https://www.cnblogs.com/riskyer/p/3246605.html
Copyright © 2011-2022 走看看