🫠 백준 1152(https://www.acmicpc.net/problem/1152)
1152번: 단어의 개수
첫 줄에 영어 대소문자와 공백으로 이루어진 문자열이 주어진다. 이 문자열의 길이는 1,000,000을 넘지 않는다. 단어는 공백 한 개로 구분되며, 공백이 연속해서 나오는 경우는 없다. 또한 문자열
www.acmicpc.net
🧐 Algorithm _ 단어의 개수 세는 법
1. 단어의 개수를 세는 대신 띄어쓰기를 기준으로 하여 공백의 개수를 세었다.
(∵ 단어를 구분짓는 기준이 띄어쓰기이기 때문)
2. getline 함수를 이용해 cin으로 받은 입력값을 str이라는 변수에 저장해 주었다.
이때, 저장되는 값들은 모두 char 형이다.
예를 들어 hello라는 변수를 입력하면, str[1] = 'e'가 되는 것이다.
3. 그 후 공백에 해당하는 문자 ' ' 의 개수를 세워주기만 하면 된다.
4. 다만, 주의할 점은 가장 앞에 공백이 나오는 경우와 가장 뒤에 공백이 들어가는 경우이다.
이와 관련하여 가장 앞에 공백이 나오는 경우를 삼항연산자로 처리해 주었고
가장 뒤에 공백이 들어가는 경우는 for문에서 str.size()-1 을 해줌으로 처리해 주었다.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string str;
getline(cin, str); // getline(cin, str, '\n');
// cin.getline(char str, streamsize n, char dlim); delim을 만나기 전까지의 모든 문자를 읽어 str배열에 저장.
int cnt_space = 0;
for (int i = 0; i < str.size() - 1; i++) {
if (str[i] ==' ')
cnt_space++;
}
int cnt = (str[0] == ' ') ? cnt_space : cnt_space + 1;
cout << cnt;
}
🫠 백준 1157(https://www.acmicpc.net/problem/1157)
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net
🧐 Algorithm _ 왜 틀리는거지...
1. 소문자와 대문자의 구별을 없애기 위해 소문자를 대문자로 바꿔준다.
2. 알파벳을 총 25개이므로 각각의 알파벳의 개수를 세주기 위한 count배열 생성
3. count한 값이 2개 이상이라면(max와 동일한 count값이 있다면) ? 출력
4. 그게 아니라면 max값에 해당하는 index에 해당하는 알파벳 출력
1번.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string str;
getline(cin, str);
// 1. 소문자와 대문자의 구별을 없애야 함
for (int i = 0; i < str.length(); i++){
if (str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] - 'a' + 'A';
}
// 2. 알파벳은 총 25개(Z-A = 25)이므로 count를 위한 배열을 생성 및 초기화해줘야함
int count[26]; for (int i = 0; i < 26; i++) count[i] = 0;
for (int i = 0; i < str.length(); i++){
int idx_number = str[i] - 65;
count[idx_number]++;
}
char c;
int max = -1;
for (int i = 0; i < 26; i++){
if (count[i] == 0)
continue;
else if (count[i] > max){
max = count[i];
c = (char)(i + 65);
}
else if (count[i] == max){
c = '?';
}
}
cout << c;
}
2번.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string str;
getline(cin, str);
// 1. 소문자와 대문자의 구별을 없애야 함
for (int i = 0; i < str.length(); i++){
if (str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] - 'a' + 'A';
}
// 2. 알파벳은 총 25개(Z-A = 25)이므로 count를 위한 배열을 생성 및 초기화해줘야함
vector<int> alpha_count(26); for (int i = 0; i < 26; i++) alpha_count[i] = 0;
for (int i = 0; i < 26; i++){
int idx_number = str[i] - 65;
alpha_count[idx_number]++;
}
int max = *max_element(alpha_count.begin(), alpha_count.end());
int cnt = count(alpha_count.begin(), alpha_count.end(), max);
int index = max_element(alpha_count.begin(), alpha_count.end()) - alpha_count.begin();
if (cnt >= 2)
cout << "?";
else if (cnt < 2)
cout << (char)(index + 65);
}
🫠 백준 11720(https://www.acmicpc.net/problem/11720)
11720번: 숫자의 합
첫째 줄에 숫자의 개수 N (1 ≤ N ≤ 100)이 주어진다. 둘째 줄에 숫자 N개가 공백없이 주어진다.
www.acmicpc.net
🧐 Algorithm _ 한줄에 입력을 모두 공백없이 받기
1. 먼저 공백없이 입력을 받아 저장하는 string 변수 str과
2. string을 char배열에 넣기 위한 작업을 진행한다.
char c[str.length()+1] 이라는 배열을 생성해주고
이 배열에 str에 저장된 문자열을 집어넣어 주기 위해 copy함수를 사용해준다.
3. 그 후 char 형의 5를 int 형의 5로 바꿔줘야 하므로 다음과 같은 방법을 이용해 준다.
9 = '9'-'0' (= 57 - 48)
위와 같은 방법을 이용하면 c[i] - '0'은 해당 숫자와 동일한 값을 갖게 된다.
4. 이후 최종적으로 sum이라는 변수에 최종적인 값들의 합을 저장해주면 된다.
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int num; cin >> num;
string str; cin >> str;
int sum = 0;
char c[str.length() + 1];
str.copy(c, str.length() + 1);
int arr[str.length() + 1];
for (int i = 0; i < str.length(); i++) {
arr[i] = c[i] - '0';
}
for (int i = 0; i < str.length(); i++) {
sum += arr[i];
}
cout << sum;
}
'Algorithms > Winter_Algorithm' 카테고리의 다른 글
self.winter.(06). 정렬, pair _백준[1427, 2108, 10814, 11650, 11651, 18870] (0) | 2023.01.09 |
---|---|
self.winter.(05). 소수판별과 에라토스테네스의 체 (시간초과의 저주) (0) | 2023.01.09 |
self.winter.(04).백준 with python 코테기초 (python) (0) | 2023.01.07 |
self.winter.(03).백준 with python 코테기초 (python) (0) | 2023.01.06 |
this.winter plan.(01).백준[1065, 1978, 2581, 10814] (C++) (0) | 2023.01.04 |