-
2941 크로아티아 알파벳BOJ 2023. 5. 6. 22:42
문제요약 : 여러 개의 문자를 합한 문자열이 새로운 문자(크로아티아 알파벳)가 된다고 생각하면 된다. 따라서 문자열에 문자를 입력받고 특정 문자열의 개수를 세는 문제이다.
체크포인트 : 위 목록에 없는 알파벳은 한 글자씩 센다.
풀이 : 배열에 문자열을 입력받고 배열의 원소들을 확인하여 크로아티아 알파벳을 변경해서 입력한 것인지 아닌지를 일일이 확인한다.
팁 : 단순히 if문 여러 개와 &&연산자로 구현해도 되지만 (코드 1) 크로아티아 알파벳이 6개이고 첫 문자가 겹치는게 몇 가지 있기 때문에 케이스를 조금 좁혀서 나눠도 괜찮을 것 같다. (코드 2)
코드 1
#include <bits/stdc++.h> using namespace std; int main() { char words[101]; int i = 0; int cnt = 0; scanf("%s", words); for(i = 0; i < strlen(words); i++){ int check = 0; if(words[i] == 'c'&& words[i+1] == '=') { cnt++; i++; check++; } if(words[i] == 'c'&& words[i+1] == '-') { cnt++; i++; check++; } if(words[i] == 'd'&& words[i+1] == 'z' && words[i+2] == '=') { cnt++; i += 2; check++; } if(words[i] == 'd'&& words[i+1] == '-') { cnt++; i++; check++; } if(words[i] == 'l'&& words[i+1] == 'j') { cnt++; i++; check++; } if(words[i] == 'n'&& words[i+1] == 'j') { cnt++; i++; check++; } if(words[i] == 's'&& words[i+1] == '=') { cnt++; i++; check++; } if(words[i] == 'z'&& words[i+1] == '=') { cnt++; i++; check++; } if(check == 0) cnt++; } printf("%d", cnt); return 0; } } // 이 코드는 풀이용 코드라고 생각하자.코드 2
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990#include <bits/stdc++.h>using namespace std;int main() {char words[101];int i = 0;int cnt = 0;scanf("%s", words);for(i = 0; i < strlen(words); i++){int check = 0;if(words[i] == 'c'&& words[i+1] == '=') {cnt++;i++;check++;}if(words[i] == 'c'&& words[i+1] == '-') {cnt++;i++;check++;}if(words[i] == 'd'&& words[i+1] == 'z' && words[i+2] == '=') {cnt++;i += 2;check++;}if(words[i] == 'd'&& words[i+1] == '-') {cnt++;i++;check++;}if(words[i] == 'l'&& words[i+1] == 'j') {cnt++;i++;check++;}if(words[i] == 'n'&& words[i+1] == 'j') {cnt++;i++;check++;}if(words[i] == 's'&& words[i+1] == '=') {cnt++;i++;check++;}if(words[i] == 'z'&& words[i+1] == '=') {cnt++;i++;check++;}if(check == 0) cnt++;}printf("%d", cnt);return 0;}#include <bits/stdc++.h>#include <string>using namespace std;int n;int i = 666;int cnt = 0;int check = 0;string s;string arr[10001];int main() {cin >> n;while(1){s = to_string(i);for(int j = 0; j < s.length();j++){if(s[j] == s[j+1] == s[j+2] == '6'){arr[cnt] = s;check = 1;}if(check == 1){cnt++;check = 0;}i++;if(cnt == n) {cout << arr[n-1];break;}}return 0;}}cs 'BOJ' 카테고리의 다른 글
18111: 마인크래프트 (0) 2023.10.14