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

user_3763047219 avatar
user_3763047219
c_cpp
01/03/2023 3:38 PM
608 B
21
Indexable
#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;
}
Editor is loading...