문제

해결방안

반복문을 통해 조건을 걸어 준 후 입력값과 동일한 값을 만족할 시 출력

코드

#include <iostream>
using namespace std;

int main()
{
    int ex, sx, mx;
    cin >> ex >> sx >> mx;
    int year=1;
    int ey = 1, sy = 1, my = 1;
    while (ey != ex || sy != sx || my != mx) {
        ey += 1;
        sy += 1;
        my += 1;
        if (ey == 16) ey = 1;
        if (sy == 29) sy = 1;
        if (my == 20) my = 1;
        year++;
    }
    cout << year;
    return 0;
}

'알고리즘 > 브루트포스' 카테고리의 다른 글

백준2231 분해합  (0) 2019.02.03
백준알고리즘-2309번 일곱난쟁이 브루트포스  (0) 2018.11.07

문제

풀이

정답이 될 수 있는 가장 작은 숫자는 input - input의자릿수 * 9 즉 n의 자리가 모두 9가 될때 최소가 된다. 그리고 가장 큰 수는 input 자체이다.

step1. input의 자릿수 구하기

나는 다음과 같은 방법으로 자릿수와 가장큰 자릿수 (ex- 333d의 가장 큰 자릿수는 100) 를 구했다.

int i(가장 큰 자릿수) = 1000000

int num(자릿 수) = 7

while(input/i==0){ //input / i가 0 이 되었다는 말은 i가 여전히 큼을 의미한다.

i=i/10;
num = num-1;

}

step2. 가장 작은 후보부터 가장 큰 후보까지 반복문을 돌리며 각 그 수와 각자리수의 합이 input과 동일한 수가 됨을 확인하여 본다.

int result = input - num * 9;
int result2 = result; //input과 비교할 값이다.
int result3;  //중간에 계산을 위해 필요한 값이다.

while(result2!=input && result <= input){

result2 = result;
result3 = result;

for(result3 = result; result3 ; result3/=10){
    result2 += result3%10;
}
result ++;
}

코드

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    int input;
    int num=7 ;
    cin >> input;
    int i=1000000;
    int i2;
    while (input/i==0) {
        i = i / 10;
        num--;
    }
    int result = input - num * 9;
    int result2 = result;
    int result3;
    while (result2 != input && result <= input) {
        result2 = result;
        for (result3 = result; result3; result3 /= 10)
            result2 += result3 % 10;
        result++;
    }
    if (result-1 == input)
        cout << 0;
    else
    cout << result-1;
    return 0;
}

문제

풀이

// bj2309.cpp: 콘솔 응용 프로그램의 진입점을 정의합니다.
//
#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
    int arr[10];
    int total=0;
    int i,j;
    for (i = 0; i < 9; i++) {
        cin >> arr[i];
        total = total + arr[i];
    }
    sort(arr, arr + 9);
    for (i = 0; i < 8; i++) {
        for (j = i+1; j < 9; j++) {
            if (total - arr[i] - arr[j] == 100) {
                for (int k = 0; k <9; k++) {
                    if (arr[i] == arr[k] || arr[j] == arr[k]) continue;
                    cout << arr[k]<<endl;
                    }
                    exit(0);
                }
                //exit(0);
            }
        }

    return 0;
}

결과

헷갈린 부분

exit(0)

난이도

'알고리즘 > 브루트포스' 카테고리의 다른 글

백준1476 날짜 계산  (0) 2019.02.06
백준2231 분해합  (0) 2019.02.03

+ Recent posts