atcoder ABC 357-B题详解
Problem Statement
You are given a string S consisting of lowercase and uppercase English letters. The length of S is odd.
If the number of uppercase letters in S is greater than the number of lowercase letters, convert all lowercase letters in S to uppercase.
Otherwise, convert all uppercase letters in S to lowercase.
Constraints
S is a string consisting of lowercase and uppercase English letters.
The length of S is an odd number between 1and 99, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print the string S after converting the letters according to the problem statement.
Sample Input 1
AtCoder
Sample Output 1
atcoder
The string AtCoder contains five lowercase letters and two uppercase letters. Thus, convert all uppercase letters in AtCoder to lowercase, which results in atcoder.
Sample Input 2
SunTORY
Sample Output 2
SUNTORY
The string SunTORY contains two lowercase letters and five uppercase letters. Thus, convert all lowercase letters in SunTORY to uppercase, which results in SUNTORY.
Sample Input 3
a
Sample Output 3
a
思路分析:
定义一个字符串,用ans存小写字母的个数,用cnt存大写字母的个数,根据题意拿个需要转化,就转化拿个。
code:
#include <iostream>
#include <string>
using namespace std;
string v;
int ans;
int cnt;
int main(){
cin>>v;
for(int i=0;i<v.size();i++){
if(v[i]>='a'){
ans++;
}
if(v[i]>='A'&&v[i]<'a'){
cnt++;
}
}
if(ans>=cnt){
for(int i=0;i<v.size();i++){
if(v[i]>='A'&&v[i]<'a'){
v[i]=v[i]+('a'-'A');
}
}
}
else {
for(int i=0;i<v.size();i++){
if(v[i]>='a'){
v[i]=v[i]-('a'-'A');
}
}
}
for(int i=0;i<v.size();i++){
cout<<v[i];
}
}