指標_快樂數字_偷吃步對了

 avatar
user_3763047219
c_cpp
a year ago
455 B
2
Indexable
Never
#include <iostream>
#include <stdio.h>

#include <stdbool.h>
bool isHappy(int);


bool isHappy(int n) {
	while (n>=0) {
		int sum = 0;
		while (n > 0) {
			sum = sum + (n % 10) * (n % 10);
			n=n / 10;
		}
		if (sum == 4) {
			return 0;
		}
		else if (sum == 1) {
			return 1;
		}
		n = sum;
	}
}


int main() {
	int n;
	scanf("%d", &n);
	if (isHappy(n)) {
		printf("true");
	}
	else {
		printf("false");
	}
	return 0;
}