#include <string>
#include <iostream>
using namespace std;

bool solution(string s)
{
    bool answer = true;
    int pCnt = 0;
	int sCnt = 0;
    
    if( false == ( s.length() <= 50) ) answer = false;
    
    for(int i = 0; i < s.length(); ++i)
	{
		if( 'p' == s[i] || 'P' == s[i] ) pCnt++;
		else if( 'y' == s[i] || 'Y' == s[i]) sCnt++;
	}
    
    if( pCnt == sCnt ) answer = true;
    else answer = false;
    
    return answer;
}

 

 

 

 

 

 

 

 

 

 


 

해당 문제는 피보나치 함수를 재귀적으로 호출하며 0과 1의 개수를 카운팅 하는 문제다.

나는 Count라는 객체를 만들고 멤버변수로 zero, one을 추가하였다.

 

testCase의 수를 입력받고 Counter 포인터 객체인 cnt를 동적으로 메모리를 잡아주었고, 

피보나치 함수에 각 인덱스에 해당 하는 값을 넘겨주어 zero, one을 카운팅 해주었다.

 

testCase 만큼 반복하고 프로그램이 종료되기 전 cnt의 zero, one을 cout을 통하여 출력해준다.

 

코드는 아래와 같다.

 

 


 

 

#include <iostream>
#include <list>
using namespace std;
class Counter
{
public:
	int zero;
	int one;
public:
	Counter()
	{
		zero = 0;
		one = 0;
	}
};
int fibonacci(int n, Counter& c);
Counter* cnt;
int main()
{
	int testCase = 0;
	int succeedCase = 0;
	int num = 0;
	cin >> testCase;
	cnt = new Counter[testCase];
	while (true)
	{
		cin >> num;
		if (0 <= num && num < 40)
		{
			
			fibonacci(num, cnt[succeedCase]);
			++succeedCase;
		}
		else
		{
		}
		if (testCase == succeedCase)
		{
			break;
		}
		
	}
	for (int i = 0; i < succeedCase; ++i)
	{
		cout << cnt[i].zero << " " << cnt[i].one << endl;
	}
	return 0;
}
int fibonacci(int n, Counter& c)
{
	if (n == 0)
	{
		c.zero++;
		return 0;
	}
	else if (n == 1)
	{
		c.one++;
		return 1;
	}
	else
	{
		fibonacci(n - 1, c) + fibonacci(n - 2, c);
	}
}

 

 

 

 

+ Recent posts