求一个字符串中出现的最大数字字串并返回出现的次数
求一个字符串中出现的最大数字字串,包括负数。
例如字符串 "abc345jfef89s*wf345jfs-345sabo "
在此字符串中最大数字字串为345出现两次
[解决办法]
正则获取所有数字数组,并统计
[解决办法]
写个简单的
#pragma warning(disable :4786)
#include <stdio.h>
#include <map>
void CountNum(char * str)
{
int i,r;
int maxr=0,maxn=0;
std::map <int,int> count;
for(i = 0; str[i]; i++)
{
if(str[i]> = '0 ' && str[i] <= '9 ')
{
r = 0;
do{
r = r * 10 + str[i++] - '0 ';
}while(str[i]> = '0 ' && str[i] <= '9 ');
count[r]++;
if(r> =maxr){
maxr=r;
maxn=count[r];
}
}
else if(str[i]== '- ')
{
i++;
if(str[i]> = '0 ' && str[i] <= '9 '){
r=0;
while(str[i]> = '0 ' && str[i] <= '9 ')
{
r = r * 10 + str[i++] - '0 ';
}
count[-r]++;
}
if(-r> =maxr)
{
maxr=-r;
maxn=count[-r];
}
}
}
printf( "The max number is:%d, occured %d time(s)\n ", maxr, maxn);
}
int main(int argc, char* argv[])
{
CountNum( "abc345jfef89s*wf345jfs-345sabo ");
return 0;
}