U E D R , A S I H C RSS

Reverse And Add/이승한

No older revisions available

No older revisions available



Python

~cpp 
def check(num):
    reNum = str(num)
    reNum = int(reNum[::-1])
    if reNum == num:
        print reNum
    else:
        check(reNum+num)
    
if __name__ == '__main__':
    num = input()
    check(num)

C++

~cpp 
#include <iostream>
using namespace std;
#include <assert.h>

const int TEN = 10;

int reverse(int number); //뒤바꾼 수가 원래 수와 같다면 -1 이 리턴

void main(){
	int N;
	int input[10] = {0};
	int output[10] = {0};
	int outputN[10] = {0};
	int trial = 0;
	
	cin>> N;
	for(int cycle=0; cycle< N; cycle++ ){
		cin>>input[cycle];
		trial = 0;
		while(1){
			if( reverse( input[cycle] ) == -1){
				output[cycle] = input[cycle];
				outputN[cycle]= trial;
				break;
			}else{
				input[cycle]+= reverse( input[cycle] );
				trial++;
			}			
		}
	}

	for(int cycle=0; cycle< N; cycle++ )
		cout<<outputN[cycle]<<" "<<output[cycle]<<endl;
}

int reverse(int number){
	int originalNum = number;
	int numbers[10] = {0};
	int zeros = 1;
	int chipers = 0; //a number of five chipers 다섯 자리수, 자릿수
	int returnNum = 0;

	//자릿수를 센다.
	for(chipers = 0; number > zeros; zeros *= TEN){
		chipers++;
	}
	zeros /= TEN;
	
	//수를 차례로 배열에 넣는다.
	for(int i=0; i< chipers; i++){
		numbers[i] = number / zeros;
		number -= zeros * numbers[i];
		zeros /= TEN;
	}
	
	//배열을 뒤집는다.
	zeros = 1;
	for(i=0; i< chipers; i++){
		returnNum += numbers[i] * zeros;
        zeros *= TEN;
	}

	//returnNum을 이용하여 앞과 뒤가 같은지 검사한다.
	if( returnNum == originalNum )
		return -1;
	return returnNum;
}
  • 여전히 복잡한 제 소스...ㅠ.ㅠㅋ;
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-05-27 07:09:19
Processing time 0.1027 sec