杭电ACM中一部分关于string的题目

HDU —1062

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062

此题利用了reverse反排序函数和find查找函数

代码如下:

#include<iostream>
#include<string>
#include<algorithm>//算法
using namespace std;
int main()
{
	int n,i;
	string s;
	cin>>n;
	getchar();
	while(n--)
	{
		getline(cin,s);
		i=0;
		while(s.find(" ",i)!=s.npos)//从下标0开始寻找空格
		{
		    reverse(s.begin()+i,s.begin()+s.find(" ",i));//i为找到空格的下标
			i=s.find(" ",i)+1;                            
		}
		reverse(s.begin()+s.find_last_of(" ",i)+1,s.end());//最后一次空格出现到字符串结束
		cout<<s<<endl;
	}
	return 0;
}


HDU—1200


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1200


代码如下:

#include<iostream>
#include<string>
using namespace std;
int main()
{
    int n,i,j,k,l,m;
    string s;
    char z[100][100];
    while(cin>>n && n!=0)
    {
        m=0;
        getchar();
        getline(cin,s);
        l=s.length()/n;
        for(i=1;i<=l;i++)
        {
            if(i%2!=0) 
            {
                for(j=0;j<n;j++)
                {
                    z[i][j]=s[m++];
                }
            }
            else 
            {
                for(j=n-1;j>=0;j--)//倒着输出
                {
                    z[i][j]=s[m++];
                }
            }
        }
        for(j=0;j<n;j++)
        {
            for(i=1;i<=l;i++)
            {
                cout<<z[i][j];
            }
        }
        cout<<endl;
    }
    return 0;
}

HDU—1321


题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1321


利用reverse函数 代码如下:

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
    string s;
    int n;
    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

代码如下:

#include<iostream>
using namespace std;
#include<string>
int main()
{
    int n,i,j;
    string s;
    cin>>n;
    getchar();
    j=1;
    while(n--)
    {
        getline(cin,s);
        for(i=0;i<s.length();i++)
        {
            if(s[i]!='Z')
            {
               s[i]+=1;
            }
            else s[i]='A';
        }
        cout<<"String #"<<j++<<endl;
        cout<<s<<endl<<endl;
    }
    return 0;
}

HDU-1860 没有使用string

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1860

代码如下:

#include<iostream>
using namespace std;
int main()
{
    int a,i,j;
    char c[5],s[81];
    while(gets(c) && c[0]!='#')
    {    
        gets(s);
        for(i=0;i<strlen(c);i++)
        {   
            a=0;
            for(j=0;j<strlen(s);j++)
            {
                if(s[j]==c[i]) a++;
            }
            cout<<c[i]<<" "<<a<<endl;
        }
        
    }
    return 0;
}

HDU—2025
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2025
代码如下:
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    int i;
    string s,c;
    while(getline(cin,s) &&s.size()<=100)
    {
        c=s;
        sort(s.begin(),s.end());//从小到大排序
        reverse(s.begin(),s.end());//反排序
        for(i=0;i<s.length();i++)
        {
            cout<<c[i];
            if(c[i]==s[0]) cout<<"(max)";
        }
        cout<<endl;
    }
    return 0;
}

HDU-2131
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2131
代码如下:
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
    string str;
    char a;
    int i,l,b;
    while(cin>>a>>str)
    {
        l=str.length();
        b=0;
        for(i=0;i<l;i++)
        {
            if(str[i]>='a'&&str[i]<='z')
            {
                if(str[i]==a||str[i]==a+32)
                {
                    b++;
                }
            }
            else 
            {
                if(str[i]==a||str[i]==a-32)
                {
                    b++;
                }
            }
        }
        cout<<fixed<<setprecision(5)<<b*1.0/l<<endl;//b*1.0/l乘以1.0是为了保证输出是小数
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值