C++/Codeforces

C++ [59A] Word 문제해석 및 풀이

S_Hoon 2020. 8. 4. 01:19

http://codeforces.com/problemset/problem/59/A

 

Problem - 59A - Codeforces

 

codeforces.com

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.

 

Net에 있는 많은 사람들이 한 단어에 대문자 소문자를 섞어서 Vasya는 굉장히 화가났습니다. 이러한 연유로 그는 그가 가장 좋아하는 브라우저에 확장프로그램을 만들기로 결정했습니다. 이것은 모든 단어를 대문자로만 혹은 소문자로만 구성되게 바꿔주는 프로그램입니다. 단어를 바꿀 시에 최대한 적은 수의 철자를 바꿔야 합니다. 예를 들어, HoUse라는 단어는 house가 되어야하고, VIP는 VIP 그대로 유지되어야 합니다. 만약 단어가 같은 수의 대문자 소문자를 가지고 있다면 그 단어는 소문자가 되어야합니다. 예를 들어, maTRIx는 matrix가 되어야합니다. 왜냐하면 소문자 3개, 대문자 3개이기 때문입니다.

 

Input

The first line contains a word s — it consists of uppercase and lowercase Latin letters and possesses the length from 1 to 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.

 

인풋

첫째 줄에 대문자 혹은 소문자로 구성되어있는 라틴 문자를 입력해야 합니다. 단어의 길이는 1 ~ 100 글자입니다.

아웃풋

올바르게 고쳐진 단어를 출력하세요. 만약 단어가 정확하게 대문자가 더 많다면 모든 철자를 대문자로 만드십시오.

소문자도 마찬가지입니다.


문제풀이

엄청 간단한 문제입니다.

단지 문자를 입력하고

입력받은 문자가 대문자인지 소문자인지 판단한 이후에

대문자가 더 많으면 모든 철자를 대문자로 변경 후 출력

소문자가 더 많으면 모든 철자를 소문자로 변경 후 출력

 

#include <iostream>
#include <cctype>	// isupper, toupper 등을 사용할 수 있게 해줌
#include <string>	// 'string'을 사용할 수 있게 해줌

int main(){
    int uppercase = 0;
    int lowercase = 0;
    std::string s;
    std::cin >> s;	// 변경할 단어 입력
    
    for (size_t i = 0; i < s.length(); i++){	// 대문자, 소문자의 수를 알려줌
        if (isupper(s[i])) uppercase++;
        else lowercase++;
    }
    
    if(uppercase > lowercase){	
    	// 대문자가 더 많다면 대문자로 변경
        for(size_t i = 0; i < s.length(); i++) s[i] = toupper(s[i]);
    }
    // 소문자가 더 많거나 혹은 대문자, 소문자의 수가 같다면 소문자로 변경
    else for(size_t i = 0; i < s.length(); i++) s[i] = tolower(s[i]);
    printf("%s\n", s.c_str()); // 변경한 단어 출력
    return 0;
}

 

'C++ > Codeforces' 카테고리의 다른 글

C++[118A] String Task 문제해석 및 풀이  (0) 2020.08.04
C++[1A] Theatre Square 문제해석 및 풀이  (0) 2020.08.04