백준 2675번 - Bronze II
#include <iostream>
#include <string>
using namespace std;
int main(void) {
int t;//testcase 개수
int r; //반복횟수
string s;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> r;
cin >> s; // 문자 입력받음
for (int j = 0; j < s.length(); j++) { // 문자열 배열로 해결
for (int k = 0; k < r; k++) {
cout << s[j]; // 반복 횟수만큼 출력
}
}
cout << endl;
}
}
백준 1157번 - Bronze I
- 대문자 -> 소문자로 :: +32
- 소문자 -> 대문자로 :: -32
- 대문자 : 65~90, 소문자 : 97~122
- 아스키코드 사용
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string wd; //입력받는 단어
int arr[26] = {}; // alphabet 26개
int maxcnt = 0;
int idx = 0;
int cnt = 0;
cin >> wd; // 단어 입력 받기
for (int i = 0; i < wd.length(); i++) // 문자열로 alphabet 개수를 센다
{
if (wd[i] < 97)
arr[wd[i] - 65]++; //대문자
else
arr[wd[i] - 97]++; //소문자
}
for (int i = 0; i < 26; i++) { // 최대빈도문자 찾기
if (arr[i] > maxcnt) {
maxcnt = arr[i];
idx = i;
}
}
for (int i = 0; i < 26; i++) { // 최대빈도문자 찾기
if (arr[i] == maxcnt) { // 최대빈도와 같은 문자
cnt++;
}
}
if (cnt > 1) {
cout << "?" << endl;
}
else {
cout << (char)(idx + 65) << endl;
}
}
백준 1152번 - Bronze II
- 단어보단 공백 개수로 cnt
- 공백 위치 신경쓰기
#include <iostream>
#include <string>
using namespace std;
int main() {
string wrd; // 단어
getline(cin, wrd);
int cnt = 1;
for (int i = 0; i < wrd.length(); i++) {
if (wrd[i] == ' ') {//빈칸이면 cnt 증가
cnt++;
}
}
if (wrd[0] == ' ') { // 처음이 공백일 경우 -
cnt--;
}
if (wrd[wrd.length() - 1] == ' ') {// 마지막이 공백일 경우 -
cnt--;
}
cout << cnt << endl;
}
백준 2908번 - Bronze II
[풀이 순서]
- 뒷자리부터 비교
- a가 클경우/b가 클경우로 구분하여 문자열 역순으로 출력
#include <iostream>
#include <string>
using namespace std;
int main() {
string num_a, num_b;
int idx = 2; //세자리 숫자라서 0,1,2
// 맨 뒤부터 비교
cin >> num_a >> num_b; // 공백 구분하여 숫자 입력받기
while (num_a[idx] == num_b[idx]) { // idx 2의 값이 같으면
cout << num_a[idx--]; // 다음 비교를 위해 -1 이동
continue; // 이렇게 전체 비교
}
if (num_a[idx] > num_b[idx]) { // 만약 비교할때 a가 클 경우
while (idx >= 0) { // 숫자 3자리
cout << num_a[idx--]; // a 문자열 역순으로 출력
}
}
else {
while (idx >= 0) {
cout << num_b[idx--]; // b 문자열 역순으로 출력
}
}
}
'코딩 > C++' 카테고리의 다른 글
백준 문제풀이 C++ [220223] - 문자열 (0) | 2022.02.23 |
---|---|
백준 문제풀이 C++ [220119] - 문자열 (0) | 2022.01.19 |
[C++] :: String - c++의 문자열 라이브러리 (0) | 2022.01.13 |
백준 문제풀이 C++ [220112] - 함수, 문자열 (0) | 2022.01.13 |
[C++] :: STL <vector> iterator(반복자) (0) | 2022.01.12 |