프로그래머스 - 숫자의 표현(Lv2)

108번뇌·2020년 11월 9일

고민하다가 못풀었다.
1. 처음 7+8에서 (6+1)+(6+2)
2. (1+3)+(2+3)+(3+3)
3. (1)+(2)+(3)+(4)+(5)
이런식의 구조이다.

#include <string>
#include <vector>

using namespace std;

int solution(int n) {
    int answer = 0;
    
    int sum = 0;
    int Partial = 1;
    while(1)
    {
      sum = 0;
      for(int i=1; i <= Partial; i++)    sum += i; 
    
      if(sum > n)            break;
   
      if(((n-sum)%Partial++)==0)        
      {
         answer++;  

      }
    }
    return answer;
}

for(int i=1; i <= Partial; i++) sum += i; 문장으로 인해 누적 + 연산을 한다.
이상한건

#include <string>
#include <vector>

using namespace std;

int solution(int n) {
  int answer = 0;
  
  int sum = 0;
  int Partial = 1;
  while(1)
  {
    sum = 0;
    for(int i=1; i <= Partial; i++)    sum += i; 
  
    if(sum > n)            break;
 
    if(((n-sum)%Partial)==0)        
    {
    	 Partial++;
       answer++;  
    }
  }
  return answer;
}

를 하면 시간이 초과나버린다.;;;

profile
내일 아침 눈을 떳을 때, '기대되는 오늘 하루를 만들기 위해' 나는 오늘도 생각하고 고민한다.

0개의 댓글