2025:
Description
对于输入的每个字符串,查找其中的最大字母,在该字母后面插入字符串“(max)”。
Input
输入数据包括多个测试实例,每个实例由一行长度不超过100的字符串组成,字符串仅由大小写字母构成。
Output
对于每个测试实例输出一行字符串,输出的结果是插入字符串“(max)”后的结果,如果存在多个最大的字母,就在每一个最大字母后面都插入"(max)"。
Sample Input
abcdefgfedcba
xxxxx
Sample Output
abcdefg(max)fedcba
x(max)x(max)x(max)x(max)x(max)
#include<iostream>
using namespace std;
#include<string>
void Add(string str,int len){
int maxLetter;
maxLetter=str[0];
for(int i=1;i<len;i++){
if(maxLetter<str[i])
maxLetter=str[i];
}
for(int j=0;j<len;j++){
cout<<str[j];
if(maxLetter==str[j])
cout<<"(max)";
}
cout<<endl;
}
int main(){
string str;int len;
while(getline(cin,str)){
len=str.length();
Add(str,len);
}
return 0;
}
2026:
Description
输入一个英文句子,将每个单词的第一个字母改成大写字母。
Input
输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行。
Output
请输出按照要求改写后的英文句子。
Sample Input
i like acm
i want to get an accepted
Sample Output
I Like Acm
I Want To Get An Accepted
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
while(getline(cin,str)){
int len=str.length();//len是定义在while外好还是内好?对内存占用,运行速度有无影响?
for(int i=0;i<len;i++){
str[0]-=32;
cout<<str[i];
if(str[i]==' ')
str[i+1]-=32;
}
cout<<endl;
}
return 0;
}
2027:
Description
统计每个元音字母在字符串中出现的次数。
Input
输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串。
Output
对于每个测试实例输出5行,格式如下:
a:num1
e:num2
i:num3
o:num4
u:num5
多个测试实例之间由一个空行隔开。
请特别注意:最后一块输出后面没有空行:)
Sample Input
2
aeiou
my name is ignatius
Sample Output
a:1 e:1 i:1 o:1 u:1
a:2 e:1 i:3 o:0 u:1
#include<iostream>
#include<string>
using namespace std;
int main(){
int n;cin>>n;getchar();
while(n--){//n在继续执行下面的程序前已经减一了
// cout<<n<<endl;
string str;int len;
int num1=0,num2=0,num3=0,num4=0,num5=0;
getline(cin,str);
len=str.length();
for(int i=0;i<len;i++){
if(str[i]=='a')
num1++;
else if(str[i]=='e')
num2++;
else if(str[i]=='i')
num3++;
else if(str[i]=='o')
num4++;
else if(str[i]=='u')
num5++;
}
cout<<"a:"<<num1<<endl;
cout<<"e:"<<num2<<endl;
cout<<"i:"<<num3<<endl;
cout<<"o:"<<num4<<endl;
if(n!=0){
cout<<"u:"<<num5<<endl;
cout<<endl;
}
else
cout<<"u:"<<num5<<endl;
}
return 0;
}
2029:回文串
Description
“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。
Input
输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串。
Output
如果一个字符串是回文串,则输出"yes",否则输出"no".
Sample Input
4
level
abcde
noon
haha
Sample Output
yes
no
yes
no
#include<iostream>
#include<string>
using namespace std;
int main(){
int n;cin>>n;getchar();
string str;int len;bool flag=true;
while(n--){
getline(cin,str);
len=str.length();
for(int i=0;i<len;i++){
len--;//在执行下面的程序前,len已经减一了
//cout<<len<<endl;
//cout<<str[len]<<endl;//应该就是最后一个字母
if(str[i]!=str[len]){
flag=false;
break;//终止for循环
}
}
if(flag==true){
cout<<"yes"<<endl;
}
else{
cout<<"no"<<endl;
flag=true;
}
}
return 0;
}