ACM (UVa) : 10591


#include <stdio.h>

long A [1000], Index;

int find (long X)
{
	long I;

	for (I=0; I<Index; I++) {
		if (A [I] == X)
			return 0;
	}

	return 1;
}


int main ()
{
	long N, T, Sum, Digit, Save_N, Case=0;

	scanf ("%ld", &T);

	while (T) {

		scanf ("%ld", &N);
		Index = 0;
		Save_N = N;

		while (find (N)) {
			A [Index++] = N;

			Sum = 0;
			while (N) {
				Digit = N % 10;
				Sum+=(Digit*Digit);
				N/=10;
			}
			N = Sum;
		}

		if (N==1)
			printf("Case #%ld: %ld is a Happy number.\n", ++Case, Save_N);
		else 
			printf("Case #%ld: %ld is an Unhappy number.\n", ++Case, Save_N);


		T--;
	}

	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s