본문 바로가기

C++ in Windows/Challenges

ICPC 대비 문제 9. 유쾌한 점퍼(Jolly Jumpers)

문제 9. 유쾌한 점퍼(Jolly Jumpers)

 

PC/UVa ID: 110201/10038, 인기도: A, 성공률: 보통, 레벨: 1

 

>> 문제

n개의 정수(n>0)로 이루어진 수열에 대해 서로 인접해 있는 두 수의 차가 1에서 n-1까지의 값을 모두 가지면 그 수열을 유쾌한 점퍼라고 부른다. 예를 들어 다음과 같은 수열에서

           1 4 2 3

앞뒤에 있는 수자 차의 절대 값이 각각 3, 2, 1이므로 이 수열은 유쾌한 점퍼가 된다. 이 정의에 따르면 중수 하나로 된 수열도 유쾌한 점퍼다. 어떤 수열이 유쾌한 점퍼인지 판단할 수 있는 프로그램을 작성하라.

 

>> 입력

각 줄 맨 앞에는 3,000 이하의 정수가 있으며 그 뒤에는 수열을 나타내는 n개의 정수가 입력된다.

 

>> 출력

입력된 각 줄에 대해 “Jolly” 또는 “Not jolly”를 한 줄씩 출력한다.

 

>> 입력예

4 1 4 2 3

5 1 4 2 -1 6

>> 출력예

Jolly

Not jolly

 



main.cpp
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

#define SEQUENCE vector<int>

void main(void)
{
	bool Keep = true;
	
	while (Keep)
	{	
		string str;

		::getline(cin, str);

		SEQUENCE seq;

		string buf;
		stringstream ss(str);

		vector<string> tokens;

		while ( ss >> buf )
			tokens.push_back(buf);

		for ( unsigned int i = 0; i < tokens.size(); i++)
		{
			seq.push_back(atoi(tokens[i].c_str()));
		}
		
		if ( seq.size() > 0 )
			if ( seq[0] > 3000 )
				continue;

		if ( seq.size() == 1 )
		{
			cout << "Jolly" << endl;
			continue;
		}
		else if ( seq.size() > 1 )
		{

			SEQUENCE ::iterator it;
			bool isJolly = true;

			for( it = seq.begin()+1; it != seq.end(); it++)
			{
				unsigned int temp = abs((*it) - (*(it-1)));
				
				if ( temp < 1 || temp > seq.size()  - 1 )
				{
					isJolly = false;
					break;
				}
			}

			if ( isJolly == true )
				cout << "Jolly" << endl;
			else
				cout << "Not jolly" << endl;

		}
	}
}





풀고나니 이렇게 푸는게 맞나? 하고 의심된다.