杭电acm有关string的题目

hdu: 1860         http://acm.hdu.edu.cn/showproblem.php?pid=1860

将string转化为数组

#include <iostream>
#include <string>
using namespace std;
int main()
{
    int i,m,j;
    string s,x;
    while(getline(cin,x)&&x[0]!='#')//注意:是字符串第一个字符不等于#,而不是整个字符不等于#
    {
        getline(cin,s);
        for(m=0;m<x.length();m++)
        {
            j=0;
            for(i=0;i<s.length();i++)
            {
                if(x[m]==s[i])
                {
                    j++;
                }
            }
            cout<<x[m]<<" "<<j<<endl;
        }
    }
    return 0;
}

巧妙应用string的一些函数达到解题的效果

hdu:2017   http://acm.hdu.edu.cn/showproblem.php?pid=2017

#include <string>
#include <iostream>
using namespace std;
int main()
{
	
	string s;
	int n,i,Count;
	cin>>n;
	getchar();
	while(n--)
	{
		getline(cin,s);
		Count=0;
		for ( i = 0; i < s.size(); i++) 
		{
			if (isdigit(s[i])) //判断是否是数字,如果是,就返回true
			{
				Count++;
			}	
		}
		cout<<Count<<endl;		
	}
	return 0;
}
hdu :1321    http://acm.hdu.edu.cn/showproblem.php?pid=1321
#include <iostream>
#include <string >
#include <algorithm>
using namespace std;
int main()
{
    int n;
    string s;
    cin>>n;
    getchar();
    while(n--)
    {
        getline(cin,s);
        reverse(s.begin(),s.end());//将字符串反序排列
        cout<<s<<endl;
    }
    return 0;
}

hdu:1328       http://acm.hdu.edu.cn/showproblem.php?pid=1328

解题思路:将A变成B,B变成C......Z变成A,容易得出A到Y的ASCII值依次往后推一个,而Z单独的变成A

#include <iostream>
#include <string >
using namespace std;
int main()
{
    int n,i,m;
    string s;
    cin>>n;
    m=1;
    while(n--)
    {
        cin>>s;
        for(i=0;i<s.length();i++)
        {
            if(s[i]=='Z')
                s[i]='A';
            else
                s[i]+=1;
        }
        cout<<"String #"<<m++<<endl;
        cout<<s<<endl;    
    //    if(n!=0)
        cout<<endl;
    }
    return 0;
}
感受:string真是一个利器,真的很好用。可以转化为数组,避免了用char数组来;其中还包含了一些好用的函数

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值