c++:输入若干字符串,输出其中连续出现次数最多的字符窜,输出连续次数。如补充~
usingnamespacestd;#include<aaa.h>structss{intnumber;stringpp;};ssaaa[9];main(){vector...
using namespace std;
#include<aaa.h>
struct ss
{int number;
string pp;};ss aaa[9];
main()
{vector<string>strs;
string str;
string *p=NULL;
while(cin>>str)
strs.push_back(str);
int count=1;
int w=0;
vector<string>::iterator iter;
for(iter=strs.begin()+1;iter!=strs.end();iter++)
{if(*iter!=*(iter-1))
{p=&(*iter);
while(*iter==*(iter+1))
{iter++;count++;}
if(count>=2)
{aaa[w].pp=*p;
aaa[w].number=count;
w++;}
count=1;}}
int max=aaa[0].number;
int ww;
for(ww=0;ww<w;ww++)
if(aaa[ww].number>max)max=aaa[ww].number;
for(ww=0;ww<w;ww++)
if(aaa[ww].number==max)
cout<<aaa[ww].number<<" "<<aaa[ww].pp<<endl;
system("pause");}
//程序目的如下:
输入:aaa \n
adjfhkj \n
aaa \n
bbb \n
bbb \n
bbb \n
ajfhgkjh \n
ctrl+z(结束)。
输出:3 bbb
求高人指点~~~~
运行时总是内存不能只读,应该没有语法错误。。 展开
#include<aaa.h>
struct ss
{int number;
string pp;};ss aaa[9];
main()
{vector<string>strs;
string str;
string *p=NULL;
while(cin>>str)
strs.push_back(str);
int count=1;
int w=0;
vector<string>::iterator iter;
for(iter=strs.begin()+1;iter!=strs.end();iter++)
{if(*iter!=*(iter-1))
{p=&(*iter);
while(*iter==*(iter+1))
{iter++;count++;}
if(count>=2)
{aaa[w].pp=*p;
aaa[w].number=count;
w++;}
count=1;}}
int max=aaa[0].number;
int ww;
for(ww=0;ww<w;ww++)
if(aaa[ww].number>max)max=aaa[ww].number;
for(ww=0;ww<w;ww++)
if(aaa[ww].number==max)
cout<<aaa[ww].number<<" "<<aaa[ww].pp<<endl;
system("pause");}
//程序目的如下:
输入:aaa \n
adjfhkj \n
aaa \n
bbb \n
bbb \n
bbb \n
ajfhgkjh \n
ctrl+z(结束)。
输出:3 bbb
求高人指点~~~~
运行时总是内存不能只读,应该没有语法错误。。 展开
1个回答
展开全部
// for(iter=strs.begin();iter!=strs.end();iter++)
iter=strs.begin();
while ( iter != strs.end() )
{
count=1;
while( iter+1 != strs.end() && *iter == *(iter+1) )
{
iter++;
count++;
}
if(count>=2)
{
aaa[w].pp=*iter;
aaa[w].number=count;
w++;
}
iter++;
}
iter=strs.begin();
while ( iter != strs.end() )
{
count=1;
while( iter+1 != strs.end() && *iter == *(iter+1) )
{
iter++;
count++;
}
if(count>=2)
{
aaa[w].pp=*iter;
aaa[w].number=count;
w++;
}
iter++;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询