HDU 2025通过string类来实现

方法1:通过设置一个数组,来存入最大字符的下标,t[101],设置一个count来判断最大字符的个数
分两种情况;1、只有一个最大字符,存入在t[0]位置;
2、如果有多个字符,在t[count++]中存入相同最大字符的下标;
如果已经找到多个最大字符,但后面有更大的,在1的判断中,对count进行初始化=1;
`#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
string s;
int t[101]={0};
while(cin>>s)
{

	int max=0;
		t[0]=0;
	int count=1;
	for(int i=1;i<s.size();i++)
	{
	    
		if(s[i]>s[max])
		{
			count=1;
			t[0]=i;
			max=i;
		}
		else if(s[i]==s[max])
		{
			t[count++]=i;
			max=i;
		}
		else
		continue;
	}
		if(count==1)
		{
			
			s.insert(t[0]+1,"(max)");
			
		}
		else
		{
			for(int i=0;i<count;i++)
			{
				s.insert(t[i]+1+i*5,"(max)");
			}
		}
	cout<<s<<endl;

}
}`

指针

在这里插入代码片

#include
#include
#include
using namespace std;
int main()
{
string s;

while(cin>>s)
{

	int max=s[0];
	const char *t=s.c_str();
    while(*t)
	{
		if(*t>max)
		{
			max=*t;
		} 
		t++;
	} 
	t=s.c_str();
	 while(*t)
	{
	  if(*t==max)
	  cout<<*t<<"(max)";
	  else
	  cout<<*t;
	  *t++;
	} 
	cout<<endl;

}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值