C/C++ basic algorithm question: daffodil number

topic description

Determines if the given three-digit number is a daffodil number. The so-called daffodil number refers to the number whose value is equal to the sum of the cubes of its own  digits . Example 153 is a daffodil number. 153=1^3+5^3+3^3 

input format 

an integer. 

output format 

It is the number of daffodils, output "YES", otherwise output "NO" (excluding quotation marks) 

sample input 

123 

sample output 

NO 

Data Size and Conventions 

A three-digit integer, otherwise output "NO" 

int main()
	{
		int sum, a, b, c;//定义a为百位数 b为十位数 c为个位数
		cin >> sum;
		a = sum / 100;
		b = sum / 10 % 10;
		c = sum % 10;
		if (a * a * a + b * b * b + c * c * c == sum)
		{
			cout << "YES";
		}
		else
		{
			cout << "NO";
		}
	}

Guess you like

Origin blog.csdn.net/dislike_carry/article/details/128732081