Monday, August 24, 2015

UVa 10591 - Happy Number

// UVa 10591 - Happy Number
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;

int main() {

	int cases;
	cin >> cases;
	for (int c = 1; c <= cases; c++) {
		int nn;
		cin >> nn;
		int n = nn;
		bool yet[1000];
		memset(yet, true, sizeof(yet));
		yet[1] = false;
		do {
			if (n < 1000)
				yet[n] = false;
			int m = n;
			int s = 0;
			while (m) {
				s += (m % 10) * (m % 10);
				m /= 10;
			}
			n = s;
		} while (yet[n]);

		if (n == 1)
			printf("Case #%d: %d is a Happy number.\n", c, nn);
		else
			printf("Case #%d: %d is an Unhappy number.\n", c, nn);
	}

	return 0;
}

No comments:

Post a Comment