숫자의 표현

magicdrill·2025년 1월 13일
0

숫자의 표현

어려운 문제는 아닌데 쓸데없이 출력문을 안 지워서 계속 시간초과가 발생했다.
내일은 키패드 누르기 문제를 반드시 성공하겠다.

#include <string>
#include <vector>
#include <iostream>

using namespace std;

int solution(int n) {
    int answer = 0;
    int i, j, middle = n / 2, sum = 0;
    
    vector<int> DP(n + 1, 0);
    for(i = 1; i <= n; i++){
        DP[i] = DP[i-1] + i;
        cout << DP[i] << " ";
    }
    cout << "\n";
    
    for(i = n; i >= 1; i--){
        for(j = i - 1; j >= 0; j--){
            if(DP[i] - DP[j] == n){
                cout << DP[i] << " - " << DP[j] << " = " << n << "\n";
                answer++;
                break;
            }
            else if(DP[i] - DP[j] > n){
                break;
            }
        }
    }
    
    
    return answer;
}

0개의 댓글