U E D R , A S I H C RSS

Reverse And Add/허아영

느낌

2006-01-09 10:59:09 Accepted 0.174 800

코드

~cpp
#include <iostream>  
using namespace std;  

#include <math.h>  

unsigned int numLength(unsigned int num)  
{  
    unsigned int turn = 0;  
    while(num >= 10)  
    {  
        num /= 10;  
        turn++;  
    }  
    return turn+1;  
}  

bool isPalindrome(unsigned int *num, unsigned int length)  
{  
    unsigned int i;  
    bool isPal =  false;  
    if(length == 1) 
        isPal = true; 
    else { 
        for(i = 0; i < length/2; i++)  
        {  
            if(num[i] == num[length-i-1])  
                isPal = true;  
            else {  
                isPal = false;  
                break;  
            }  
        }  
    } 
    return isPal;  
}  

unsigned int ReverseAndAdd(unsigned int *num, unsigned int length)   
{  
    unsigned int i, reverseNum = 0, Num = 0;  
    unsigned int *temp = new unsigned int [length];  
	
    for(i = 0; i < length; i++)  
    {  
        temp[i] = num[length-i-1];  
        reverseNum += temp[i] * pow(10, length-i-1);    // 모아서 더하기  
        Num += num[i] * pow(10, length-i-1);  
    }  
	
//    delete[] temp;  
    return (Num+reverseNum);  
}  


unsigned int main()  
{  
    unsigned int addNum, length, i, turn = 0, testCaseNum;  
    unsigned int num;  
    unsigned int * store_numbers;  
    cin >> testCaseNum;  
    while(testCaseNum >= 1)  
    {  
        turn = 0;  
        cin >> num;  
        store_numbers = new unsigned int[numLength(num)];  
		
        for(i = 0; i < numLength(num); i++)            // 나누어 넣기  
        {  
            store_numbers[i] = num / pow(10, (numLength(num)-i-1));  
            store_numbers[i] = store_numbers[i] % 10;  
        }  
		
        while(1)  
        {          
            length = numLength(num);  
            for(i = 0; i < length; i++)            // 나누어 넣기  
            {  
                store_numbers[i] = num / pow(10, (length-i-1));  
                store_numbers[i] = store_numbers[i] % 10;  
            }  
            if(isPalindrome(store_numbers, length))  
            {  
                if(turn == 0) 
                    addNum = num; 
                if(length == 1) 
                    addNum = num; 
                break;  
            }  
            addNum = ReverseAndAdd(store_numbers, length);  
            // 바꿔서 더하기      
            turn++;  
            num = addNum;  
        }      
		
        cout << turn << " " << addNum << endl;  
        testCaseNum--;  
    }  
//    delete []store_numbers;  
    return 0;  
}  
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:55
Processing time 0.0167 sec