2015년 7월 12일 일요일

수학자 문제 풀이

//
//  main.cpp
//  AlgoTest
//
//  Created by Lonnie Noel on 2015. 7. 12..
//  Copyright (c) 2015 Lonnie Noel. All rights reserved.
//

#include <iostream>

const int N = 8; // pie
const int K = 4; // mathmatic


int currentMathmaticIndex=K-1;


int nCount = 0;
int MaxLast_K = (N-K)+1;
int MaxFirst_K = (N/K);

int Arr[K];


void CheckPieCount(int CurrentIndex)
{
    if(CurrentIndex < 1)
    {
        //printf("[%d]범위에서 벗어났습니다 \n", CurrentIndex);
        return;
    }
    
    int nPieCount = 0;
    for(int i = Arr[CurrentIndex]; i>= MaxFirst_K; i--)
    {
        //printf("for 테스트 : %d \n", i);
        if(Arr[CurrentIndex] < Arr[CurrentIndex-1])
        {
            //printf("%d 최대 갯수는 : %d 입니다 \n", CurrentIndex, i);
            break;
        }
        
        nPieCount = 0;
        printf("CurrentIndex : %d \n", CurrentIndex );
        for(int j= 0; j < K; j++)
        {
            printf("[arr[%d] : %d \n", j, Arr[j]);
            nPieCount += Arr[j];
            
        }
        
        if(nPieCount == N)
        {
            if(Arr[CurrentIndex] != Arr[CurrentIndex-1])
                nCount++;
           
            if(Arr[CurrentIndex]-1 >= Arr[CurrentIndex-1]+1)
            {
                //if((Arr[CurrentIndex]-1) > (Arr[CurrentIndex-1]+1))
                {
                    printf("[CurrentIndex] : %d || [CurrentIndex-1] : %d \n", (Arr[CurrentIndex]-1), (Arr[CurrentIndex-1]+1));
                    //nCount++;
                    Arr[CurrentIndex] --;
                    Arr[CurrentIndex-1] ++;
                }
            }
            else
            {
                break;
            }
            
        }
        
    }
    
    CheckPieCount(CurrentIndex-1);
    
}

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    
    
    
    //배열 초기화
    for(int i = 0; i < K; i++)
    {
        if(i == (K-1))
        {
            Arr[i] = MaxLast_K;
        }
        else
        {
            Arr[i] = 1;
        }
    }
    
    CheckPieCount(currentMathmaticIndex);
    
    if(N % K == 0)
    {
        nCount ++;
    }
    
    
    printf("Output : %d \n", nCount);
    
    return 0;
}