느낌

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;  
}  
Retrieved from http://wiki.zeropage.org/wiki.php/ReverseAndAdd/허아영
last modified 2021-02-07 05:27:55