반응형

처음 짠 코드

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
int main() {
	//freopen("input.txt.txt","rt",stdin);
	
	int n,k,cnt=0;
	scanf("%d",&n);
	
	for(int i=n/2 +1; i>=1; i--){
		int sum = i;
		for(int j=i-1 ; j>=1; j--){
			sum+=j;
			
			if(sum==n){
				for(k=j; k<i;k++){
					printf("%d + ",k);
				}
				printf("%d = %d\n",k,sum);
				cnt++;
				break;
			}
			else if(sum>n){
				break;
			}
			
		}
		
	}
	printf("%d",cnt);


}

 

------

 

수정

 

1, 2 -> (15-3)%2==0이므로 가능

1,2,3 -> (15-6)%3==0이므로 가능

1,2,3,4 -> != 0 이므로 불가

1,2,3,4,5 ==0이므로 가능

 

 

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
int main() {
	//freopen("input.txt.txt","rt",stdin);
	
	int n,k,cnt=0, sum=3,i=2;
	scanf("%d",&n);
	
	
	// 1 , 2
	// 1, 2, 3 
	//
	
	
	while(sum<=n){
		if((n-sum)%i==0){
			int add = (n-sum)/i;
			for(k=1 + add ; k< i+add ;k++){
				printf("%d + ",k);
			}
			printf("%d = %d\n",k,n);
			cnt++;
		}
		i++;
		sum+=i;
	}
	
	
	printf("%d",cnt);


}

 

 

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
int main() {
	//freopen("input.txt.txt","rt",stdin);
	
	int a, b=1, cnt=0, tmp, i;
	scanf("%d",&a);
	tmp=a;
	a--;
	while(a>0){
		b++;
		a=a-b;
		if(a%b==0){
			for(i=1; i<b; i++){
				printf("%d + ", (a/b)+i);
			}
			printf("%d = %d\n", (a/b)+i, tmp);
			cnt++;
		}
	}

	printf("%d",cnt);


}
반응형

'Algorithm > etc' 카테고리의 다른 글

조세퍼스  (0) 2022.01.09
이분 탐색으로 최소, 최대 적절한 답 찾아내기  (0) 2022.01.08
LRU( Least Recently Used) cpp 구현  (0) 2022.01.06
C++ 문자 배열에서 숫자 추출  (0) 2021.12.27
3의 개수  (0) 2021.12.27

+ Recent posts