Over a million developers have joined DZone.

A Solution For The "Reverse And Add" Problem

·
A solution for the "Reverse and Add" problem.

Problem description:
http://icpcres.ecs.baylor.edu/onlinejudge/external/100/10018.html

Author: Joana Matos Fonseca da Trindade
Date: 2008.04.14


/*
 * Solution for the "Reverse and Add" problem.
 * UVa ID: 10018
 */
#include 

#define NDIGITS 100

using namespace std;

/* returns the reversed number */
unsigned long long reverse(unsigned long long number) {
	unsigned long long m = 0; /* reversed number */
	int digits[NDIGITS]; /* digits array */
	int pos = 0, power = 1;
	
	/* init */
	for (int i=0; i 0) {
		digits[pos++] = number % 10;
		number = number / 10;
	}

	/* multiply the reversed digits by the powers of ten */
	for (int i=pos-1; i>=0; i--) {
		m += power * digits[i];
		power *= 10;
	}
	
	return m;
}

/* main */
int main() {
	int nc; /* number of cases */
	int m; /* minimum number of iterations */
	unsigned long long n; /* number */
	
	cin >> nc;
	
	/* reverse and add.. */
	for (int i=0; i> n;
		m = 0;
		while (true) {
			if (reverse(n) == n) {
				break;
			} else {
				n += reverse(n);
				m++;
			}
		}
		cout << m << " " << n << endl;
	}
	
	return 0;
} 

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}