1.题目
Problem - 59A - Codeforces (Unofficial mirror by Menci)
Vasya is very upset that many people on the Net mix uppercase and lowercase letters in one word. That's why he decided to invent an extension for his favorite browser that would change the letters' register in every word so that it either only consisted of lowercase letters or, vice versa, only of uppercase ones. At that as little as possible letters should be changed in the word. For example, the word HoUse must be replaced with house, and the word ViP — with VIP. If a word contains an equal number of uppercase and lowercase letters, you should replace all the letters with lowercase ones. For example, maTRIx should be replaced by matrix. Your task is to use the given method on one given word.
瓦夏对网上许多人在一个单词中混用大小写字母感到非常不满。因此,他决定为他最喜欢的浏览器发明一个扩展程序,改变每个单词中字母的寄存器,使其要么只包含小写字母,要么只包含大写字母。在这种情况下,单词中应尽可能少地更改字母。例如,HoUse必须替换为house,而ViP则替换为VIP。如果一个单词包含相同数量的大写字母和小写字母,则应将所有字母替换为小写字母。例如,maTRIx 应替换为 matrix。您的任务是在一个给定的单词上使用给定的方法。
Input
The first line contains a word s — it consists of uppercase and lowercase Latin letters and possesses the length from 1 to 100.
输入
第一行包含一个单词 s - 它由大写和小写拉丁字母组成,长度从 1 到 100 不等。
Output
Print the corrected word s. If the given word s has strictly more uppercase letters, make the word written in the uppercase register, otherwise - in the lowercase one.
输出
打印更正后的单词 s 。如果给定的单词 s 有严格意义上更多的大写字母,则将该单词写入大写寄存器,否则写入小写寄存器。
Examples
input
HoUse
output
house
input
ViP
output
VIP
input
maTRIx
output
matrix
2.题解代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int nma[N],nmb[N];
int main(){
int nmaj=0,nmbj=0;
string s;
cin>>s;
int len=s.length();
for(int i=0;i<len;i++){
if(s[i]>='A'&&s[i]<='Z'){
nma[nmaj]=i;
nmaj++;
}
else{
nmb[nmbj]=i;
nmbj++;
}
}
if(nmaj>nmbj){
for(int i=0;i<nmbj;i++){
s[nmb[i]]=s[nmb[i]]-'a'+'A';
}
}
else if(nmaj<=nmbj){
for(int i=0;i<nmaj;i++){
s[nma[i]]=s[nma[i]]+'a'-'A';
}
}
cout<<s;
return 0;
}