// recursive.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; //char szGame[]={'w','w','k','k','k','k','l','l','l','m','m','m'}; char szGame[]={'w','w','k','l','l','l','m','m','m'}; int countLongest(char* pBuf); int main(int argc, char* argv[]) { cout<<countLongest(szGame)<<endl; return 0; } int max=0; int countLongest(char* pBuf) { /* int result=0; char *p=pBuf; if((*pBuf)=='/0') { result>max?result:max; cout<<"max="<<max<<endl; return result; } while((*pBuf)!='/0'&&(*p)==(*pBuf)) { result++; pBuf++; } result>max?(max=result):max; p++; return countLongest(p); */ //improved version int result=0; char *p=pBuf; if((*pBuf)=='/0') { return result; } while((*pBuf)!='/0'&&(*p)==(*pBuf)) { result++; pBuf++; } p++; return result>countLongest(p)?result:countLongest(p); }