문제 링크 

 

n <= m 조건을 가지고 있고,  다리를 만드는 경우의 수를 센다. 

문제 하단에 '다이나믹 프로그래밍' 이라고 힌트가 써있길래 2차원 배열로 풀 수 있었다. 

 

먼저 n과 m이 같은 수라면 경우의 수는 전부 1개다.  

n == 1 이면, m이 뭐든간에 경우의 수는 m개다. 

 

입력을 받기 전에, DP배열을 미리 채워놓는다. 

 

a[2][2] = 1 

a[2][3] = a[1][1] + a[1][2]

a[2][4] = a[1][1] + a[1][2] + a[1][3] 

 

a[i][j] = a[i-1][1] + a[i-1][1] + .... + a[i-1][j-1]

 

"맞았습니다" 코드 링크 

/** 다리 놓기
 * https://www.acmicpc.net/problem/1010
 * http://boj.kr/2dd342c6f44849369f678383dea56dbf
 * */
#include <bits/stdc++.h>
using namespace std;

int t, n, m;
int a[32][32];
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  for(int i = 1; i <= 30; i++){
    a[i][i] = 1; a[1][i] = i;
  }
  for(int i = 2; i < 30; i++){
    for(int j = i+1; j < 30; j++){
      for(int k = 1; k < j; k++){
        a[i][j] += a[i-1][k];
      }
    }
  }
  cin >> t;
  while(t--){
    cin >> n >> m;
    cout << a[n][m] << '\n';
  }
  return 0;
}
728x90

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

Z 백준 1074번 c++  (0) 2022.02.23
경로 찾기 백준 11403번 c++  (0) 2022.02.23
셀프 넘버 백준 4673 c++  (0) 2022.02.22
명령 프롬프트 백준 1032번 c++  (0) 2022.02.22
저항 백준 1076번 c++  (0) 2022.02.22
Out Of Bounds 로 틀리다가, 조건문 추가해서 맞았다 ..
 
#include <bits/stdc++.h>
#define limit 10002
using namespace std;

bool arr[limit];

int check(int num){ // 자기자신과 각 자리 숫자를 더한다 
  int sum = num;
  while(num != 0){
    sum += num % 10; // 1의 자리 숫자를 더한다
    num /= 10;       // 다음 자리 숫자가 나오도록 나눈다 
  }
  return sum;
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  for(int i = 1; i <limit; i++){
    int idx = check(i);
    if(idx <= limit) arr[idx] = true;
  }

  for(int i = 1; i <limit; i++) if(!arr[i]) cout << i << '\n';
  return 0;
}
 

Out Of Bounds 틀렸던 이유 

 

셀프 넘버는 원래 숫자보다 크게 나온다. 자기자신과 자기자신의 각 자리숫자를 더하기 때문이다. 
arr배열 크기가 10002 인데. 아래 처럼 idx를 무조건 접근하면 Out Of Bounds 발생한다. 
  for(int i = 1; i <limit; i++){
    int idx = check(i);
    arr[idx] = true;
  }
 
728x90

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

경로 찾기 백준 11403번 c++  (0) 2022.02.23
다리 놓기 백준 1010번 c++  (0) 2022.02.23
명령 프롬프트 백준 1032번 c++  (0) 2022.02.22
저항 백준 1076번 c++  (0) 2022.02.22
크로아티아 알파벳 백준 2941번 c++  (0) 2022.02.22

문제 링크 

 

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

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

+ Recent posts