【算法-3】求100—999之间的水仙花数
2016-07-19 22:44
453 查看
输出100—999中所有的水仙花数,若3位数xyz满足
, 则xyz为水仙花数;
例如
, 因此153是水仙花数。
直接上代码:
#include <iostream> using namespace std; // 方法一 void daffodil_1() { int a = 0; for (int x=1; x<10; x++) { for (int y =0; y<10; y++) { for (int z = 0; z<10; z++) { a = 100*x+10*y+z; if (a== x*x*x + y*y*y + z*z*z) { cout<<a<<" 是水仙花数"<<endl; } } } } } // 方法二 void daffodil_2() { int x = 0; int y = 0; int z = 0; for (int n=100; n<1000; n++) { x = n / 100; y = (n % 100)/10 ; z = n % 10; if(n == x*x*x + y*y*y +z*z*z) { cout<<n<<" 是水仙花数"<<endl; } } } int main() { cout<<"----------daffodil_1()---------"<<endl; daffodil_1(); cout<<endl; cout<<"----------daffodil_2()---------"<<endl; daffodil_2(); return 0; }
相关文章推荐
- 如何在Apache 上配置 WebDAV 服务器
- python3常用的系统标准库
- hadoop jps 进程显示不全
- spring mvc 对jsonp的支持
- 对象的序列化以及反序列
- mysql事务
- Jackson基础笔记
- 日历一个小空间
- python3的面向对象
- 归并排序
- CentOS-6.5下安装navicat for mysql
- reids集群
- 如何编写批处理文件
- c# - 反序列化报错:Input string was not in a correct format
- Java VS Python
- linux shell 读取文件脚本
- STM32之DMA(直接存储器存储)
- jar 命令 打包装class文件的文件夹
- 【Leetcode】之Remove Duplicates from Sorted List
- js的一些压缩和优化性能