Home | Print | Q/A | Guest | NewsLetter
Display context of search results Case-sensitive searching
개발자가성공하는길체-지향분석 › 기획과프로젝트관리CMMI의표현방법 › 데이터모델링 › 3+1문제
Database System
Data Warehouse
Data Analysis
Operating System
Open Source
Enterprise Architecture
Software Engineering
Process
Working Smart

SQL Server
PostgreSQL
Oracle
DB2
Teradata
MySQL
Performance Tuning
Programming

Link
Philosophy
Tools
Misc
주인놈
_
_
SideBar Edit
/*

알고리즘 트래이닝 북 문제 1 : 3N+ 1 문제
----------------------------------------

1. 두수를 입력
2. 두수 사이의 수를 3과 같이 계산
3. 짝수이면 2로 나눔, 홀수이면 3으로 곱한 다음 1을 더함
4. 그래서 1이 될때까지 반복
5. 두수 사이의 값이 1이 될때 까지의 최대 단계를 구하는 것

예) 22

22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1  --> 총 16단계



*/
#include "stdafx.h"
#include <iostream>

using namespace std;

int main(int argc, _TCHAR* argv[])
{
	int i, j, max = 1, max_temp = 1;

	cin >> i >> j;

	if(j < i)
	{
		int temp;
		temp = j;
		j = i;
		i = temp;
	}

	if ( i > 0 && j > 0 && i < 1000000 && j < 1000000)
	{
		for(int loop  = i; loop <= j; loop++)
		{
			int val = loop;

			//cout << val << " : ";

			while(val > 1)
			{
				if(val % 2 == 0)
					val = val / 2;
				else
					val = val * 3 + 1;

				max_temp++;

				//cout << val << ", ";
			}

			if(max_temp > max)
				max = max_temp;

			max_temp = 0;
		}
		
		cout << "\n" << i << " ";
		cout << j << " ";
		cout << max + 1 << endl;
	}
	else
		cout << "입력한 숫자의 범위는 0보다 크고, 1000000 보다 작아야 합니다." << endl;

	return 0;
}




EditText|Print|FindPage|DeletePage|LikePages|http://www.databaser.net|last modified 2010-03-10 10:02:13