您的位置:首页 > 其它

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

2016-07-21 09:25 363 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: