C 练习实例13 - 水仙花数

题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数 本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。

程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。

#include <iostream>
using namespace std;
int main()
{
	int a,b,c,cnt=0;//个位,十位,百位
	for(int i=100;i<1000;i++) {
		a=i%10;
		b=i/10%10;
		c=i/100;
		if(i==(a*a*a+b*b*b+c*c*c)) {
			cout<<i;
			cnt++;
			if(cnt%5)
				cout<<" ";
			else
				cout<<endl;
		}
	}
	return 0;
}

Sample Output

153 370 371 407
--------------------------------
Process exited after 0.5805 seconds with return value 0
请按任意键继续. . .

相关推荐

  1. C 练习实例13 - 水仙花

    2024-01-05 21:22:04       58 阅读
  2. python练习-水仙花

    2024-01-05 21:22:04       34 阅读
  3. 14水仙花

    2024-01-05 21:22:04       20 阅读
  4. C语言-水仙花

    2024-01-05 21:22:04       58 阅读
  5. C 练习实例13

    2024-01-05 21:22:04       31 阅读
  6. C语言每日一练(12水仙花

    2024-01-05 21:22:04       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-05 21:22:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-05 21:22:04       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-05 21:22:04       82 阅读
  4. Python语言-面向对象

    2024-01-05 21:22:04       91 阅读

热门阅读

  1. 开启远程工作之旅

    2024-01-05 21:22:04       66 阅读
  2. python基础教程六(字典方法)

    2024-01-05 21:22:04       64 阅读
  3. Spring之推断构造方法

    2024-01-05 21:22:04       47 阅读
  4. 电商API连接升级:飞书集成助力营销系统

    2024-01-05 21:22:04       63 阅读
  5. How to collect data

    2024-01-05 21:22:04       69 阅读
  6. 中华人民共和国海关总署下载179号数据对接

    2024-01-05 21:22:04       64 阅读