문제 링크 

 

입력 되는 모든 문자열의 길이가 같다. 

1. 따라서 문자열 길이의 처음부터 끝까지를 비교하는 이중반복문으로 풀었다. 

2. 모든 문자열을 한 번에 입력받지 않고, 그때 그때 첫 문자열과 비교하여 답을 내는 방법으로도 풀었다. 

 

"맞았습니다" 코드 링크 1

#include <bits/stdc++.h>
using namespace std;

int n, h, idx;
vector<string> v;
string input, answer;
bool check_flag = true;

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for(int i = 0; i < n; i++){
    cin >> input;
    v.push_back(input);
  }
  int len = v[0].length(); // 문자열 길이 (모두 같다)
  int vsize = v.size();

  for(idx = 0; idx < len; idx++){
    check_flag = true;
    for(h = 0; h < vsize-1; h++) { // 모든 문자열의 인덱스 h자리가 같은지.
      if (v[h][idx] != v[h + 1][idx]) {
        check_flag = false; break;
      }
    }
    check_flag ? answer += v[h][idx] : answer += "?";
  }
  cout << answer;
  return 0;
}

 

"맞았습니다" 코드 링크2

#include <bits/stdc++.h>
using namespace std;

int n, h, idx;
string tempstr, answer;

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n >> answer;
  while(n--) {
    cin >> tempstr;
    for(int i = 0; i < tempstr.size(); i++)
      if(tempstr[i] != answer[i]) answer[i] = '?';
  }
  cout << answer;
  return 0;
}
728x90

'알고리즘 > 백준' 카테고리의 다른 글

다리 놓기 백준 1010번 c++  (0) 2022.02.23
셀프 넘버 백준 4673 c++  (0) 2022.02.22
저항 백준 1076번 c++  (0) 2022.02.22
크로아티아 알파벳 백준 2941번 c++  (0) 2022.02.22
통계학 백준 2108번 c++  (0) 2022.02.22
문자에 맞는 숫자를 가져와야 하니까 map을 이용했다. 
10의 '값'승이 곱할 값과 같아서 pow()함수를 썼다. 

 

 
#include <bits/stdc++.h>
using namespace std;

long long answer;
string a, b, c;
map<string, int> valuemap;
vector<string> stv{"black", "brown", "red", "orange", "yellow",
                   "green", "blue", "violet", "grey", "white"};

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  for(int i = 0; i < 10; i++){
    valuemap.insert({stv[i], i});
  }
  cin >> a >> b >> c;

  if(valuemap[a] != 0) answer = 10 * valuemap[a];
  answer += valuemap[b];
  answer *= pow(10, valuemap[c]);
  cout << answer;

  return 0;
}
 
728x90

'알고리즘 > 백준' 카테고리의 다른 글

셀프 넘버 백준 4673 c++  (0) 2022.02.22
명령 프롬프트 백준 1032번 c++  (0) 2022.02.22
크로아티아 알파벳 백준 2941번 c++  (0) 2022.02.22
통계학 백준 2108번 c++  (0) 2022.02.22
종이의 개수 백준 1780번 c++  (0) 2022.02.21
 
string 의 메서드를 쓸 줄 아는지 묻는 문제였다. 
find() replace() 를 통해 두개로 구성된 알파벳을 특정 문자로 바꿨다. 
 
#include <bits/stdc++.h>
using namespace std;

string s;
int start_idx;
vector<string> croa = {"lj", "dz=", "nj", "c=", "c-", "d-", "s=", "z="};

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> s;

  for(int i = 0; i < croa.size(); i++){ // 모든 단어를 찾는다
    while(1){
      start_idx = s.find(croa[i]);
      if(string::npos == start_idx) break; // 없으면 다른 단어 찾기
      s.replace(start_idx, croa[i].length(), "@"); // 찾으면 한덩이를 @ 로 변환
    }
  }

  cout << s.length();
  return 0;
}
 
728x90

'알고리즘 > 백준' 카테고리의 다른 글

명령 프롬프트 백준 1032번 c++  (0) 2022.02.22
저항 백준 1076번 c++  (0) 2022.02.22
통계학 백준 2108번 c++  (0) 2022.02.22
종이의 개수 백준 1780번 c++  (0) 2022.02.21
하얀 칸 백준 1100번 c++  (0) 2022.02.21
최빈값 구할 때, <숫자, 숫자의 빈도수> 를 저장하는 map을 정렬해야 하는 부분에서 시간이 걸렸다. 
2022.2.22에 재채점 되서 다시 풀었다. 아래의 설명이 추가되었다. 
(0 + 0 + (-1)) / 3 = -0.333333... 이고 이를 첫째 자리에서 반올림하면 0이다. -0으로 출력하면 안된다.
 

"맞았습니다" 코드 링크 

#include <bits/stdc++.h>
using namespace std;

int n, num;
double sum;
map<int,int> num_cnt;
vector<int> v;

bool comp(pair<int,int> &a, pair<int,int> &b){
  if(a.second > b.second) return true;
  return false;
}

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for(int i = 0; i < n; i++){
    cin >> num; // 숫자와 빈도수
    if(num_cnt.count(num) == 0) num_cnt.insert({num, 1});
    else num_cnt[num]++;
    v.push_back(num);
    sum += num; // 총합 구하기
  }

  sort(v.begin(), v.end()); // 오름차순 정렬

  // 평균
  double avg = round(sum / n);
  if(abs(avg) == 0) avg = 0;
  cout << avg << '\n';

  // 중간값
  cout << v[n/2] << '\n';

  // 최빈값
  vector<pair<int,int>> pv(num_cnt.begin(), num_cnt.end()); // map -> vector
  sort(pv.begin(), pv.end(), comp);

  (pv[0].second == pv[1].second) ? cout << pv[1].first << '\n' : cout << pv[0].first << '\n';

  // 범위
  cout << abs(v[n-1] - v[0]) << '\n';

  return 0;
}
 

 
map 정렬을 위해 map을 vector<pair<int,int>> 로 바꿨다. 
  // 최빈값
  vector<pair<int,int>> mapv(m.begin(), m.end()); // map -> vector
  sort(mapv.begin(), mapv.end(), comp); // value 값 기준으로 정렬

  if(mapv[0].second == mapv[1].second) cout << mapv[1].first << '\n';
  else cout << mapv[0].first << '\n';
 
 
그리고 조건 함수는 '빈도수' 조건으로 작성한다.
bool comp(pair<int,int> &a, pair<int,int> &b){
  if(a.second > b.second) return true;
  return false;
}
 
728x90

'알고리즘 > 백준' 카테고리의 다른 글

저항 백준 1076번 c++  (0) 2022.02.22
크로아티아 알파벳 백준 2941번 c++  (0) 2022.02.22
종이의 개수 백준 1780번 c++  (0) 2022.02.21
하얀 칸 백준 1100번 c++  (0) 2022.02.21
다이얼 백준 5622번 c++  (0) 2022.02.21
 
현재 한 변의 길이가 9 인데 한가지 색깔이 아니다. 한 변 길이가 3이 되도록 9등분 해야 한다. 
 
(0,0)이 왼쪽 위의 좌표라면,  시작점 좌표는 9개가 된다. 길이는 3이다. 
(0,0), (0,3), (0,6)
(3, 0), (3,3) (3, 6)
(6, 0), (6,3) (6, 6)
 
#include <bits/stdc++.h>
using namespace std;

int n;
int arr[3000][3000];
int answer[3];

bool check(int starti, int endj, int size){ // 전부 같은 색깔인지 검사
  int color = arr[starti][endj];
  for(int i = starti; i < (starti+size); i++){
    for(int j = endj; j < (endj+size); j++) {
      if (color != arr[i][j]) return false;
    }
  }
  return true;
}

void div_paper(int x, int y, int len){
  if(check(x, y, len)){ 
    answer[arr[x][y]]++; return;
  }

  int div_len = len / 3; // len == 9, div_len == 3
  for(int i = 0; i <3; i++){
    for(int j = 0; j <3; j++){ // 좌표에 0, 3, 6을 더한 시작점에서 다시 검사한다. 
      div_paper(x + i*div_len, y + j*div_len, div_len);
    }
  }
}

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for(int i = 0; i < n; i++){
    for(int j = 0; j <n; j++){
      cin >> arr[i][j]; arr[i][j]++; // 양수로 만들기
    }
  }

  div_paper(0,0,n);
  for(auto c : answer) cout << c << '\n';
  
  return 0;
}
 
728x90

'알고리즘 > 백준' 카테고리의 다른 글

크로아티아 알파벳 백준 2941번 c++  (0) 2022.02.22
통계학 백준 2108번 c++  (0) 2022.02.22
하얀 칸 백준 1100번 c++  (0) 2022.02.21
다이얼 백준 5622번 c++  (0) 2022.02.21
네 번째 점 백준 3009번 c++  (0) 2022.02.21
 
체스 판은 하얀칸과 검정칸이 번갈아가며 색칠되어 있다. 
(0,0)이 하얀칸이라는 조건이 있으니까, 짝수행의 짝수열은 하얀칸이다. 홀수행의 홀수열도 하얀칸이다. 
 
행,열이 둘다 짝수일때 F값인지 검사했다. 
행,열이 둘다 홀수일때 F값인지 검사했다. 
 
#include <bits/stdc++.h>
using namespace std;

char board[10][10];
int answer;

void check(int &h, int &w){
  if(board[h][w] == 'F') answer++;
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  for(int i = 0; i < 8; i++)
    for(int j = 0; j < 8; j++)
      cin >> board[i][j];
    
  for(int i = 0; i < 8; i++){
    for(int j = 0; j < 8; j++){
      if(i % 2 == 0 && j % 2 == 0) check(i, j);
      if(i % 2 == 1 && j % 2 == 1) check(i, j);
    }
  }
  cout << answer;

  return 0;
}
 
 
아래의 성질을 이용했다. 
짝수 + 짝수 == 짝수 
홀수 + 홀수 == 짝수 
#include <bits/stdc++.h>
using namespace std;

string st;
int answer;

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  for (int i = 0; i < 8; i++){
    cin >> st;
    for (int j = 0; j < 8; j++) {
      if((i + j) % 2 == 0 && st[j] == 'F') answer++;
    }
  }
  cout << answer;

  return 0;
}
 
 
728x90

문제 링크 

 

처음에는 문자열의 범위 조건으로 총합을 구했다. 

다른 분의 코드를 보고 문자의 인덱스 자체를 숫자로 쓰는 코드로 바꿔서 다시 풀었다. 

 

 

"맞았습니다" 코드 링크1 

#include <bits/stdc++.h>
using namespace std;

string input;
int len, sum;

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> input;
  len = input.size();

  for(int i = 0; i < len; i++){
    char ch = input[i];

    if(ch >= 'W') sum += 10;
    else if(ch >= 'T') sum += 9;
    else if( ch >= 'P') sum += 8;
    else if(ch >= 'M') sum += 7;
    else if(ch >= 'J') sum += 6;
    else if(ch >= 'G') sum += 5;
    else if(ch >= 'D') sum += 4;
    else if(ch >= 'A') sum += 3;
  }
  cout << sum;

  return 0;
}

 

 

"맞았습니다" 코드 링크2 : 문자의 인덱스를 숫자로 활용 

#include <bits/stdc++.h>
using namespace std;

string input;
string numst = "22233344455566677778889999"; // 다이얼 숫자 그대로 사용

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> input;

  int sum = 0;
  int len = input.size();

  for(int i = 0; i < len; i++){
    int numidx = input[i] - 'A';
    sum += (numst[numidx] - '0' + 1); // 다이얼 숫자보다 +1초 더 걸림.
  }
  cout << sum;

  return 0;
}
728x90

+ Recent posts