zoukankan      html  css  js  c++  java
  • 输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。例如,153是水仙花数,因为153=1*+5*+3

    输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。例如,153是水仙花数,因为153=1+5+3

    答案解析:

    从题目当中得到”水仙花数“为一个3位数,则范围确定为[100, 999]。另外需要获取该数字的百位数字,十位数字,个位数字相加起来等于该数本身,则我们需要使用到%除的方式,来获取每一个位权的数字。

    代码示例:

    #include <stdio.h>
    
    int main()
    {
    	 //a表示百位数字,b表示十位数字,c表示各位数字
    	int a, b, c;
    	for (int i = 100; i <= 999; i++)
    	{
    		a = i / 100;
    		b = (i / 10) % 10;
    		c = i % 10;
    		if (a * a * a + b * b * b + c * c * c == i)
    		{
    			printf("%d
    ", i);
    		}
    	}
    	return 0;
    }
    

    运行截图:

     输出所有的“水仙花数”,所谓“水仙花数”是指一个3位数,其各位数字立方和等于该数本身。例如,153是水仙花数,因为153=1+5+3

  • 相关阅读:
    springboot对JPA的支持
    springboot整合redis
    spring boot整合mybatis
    mybatis与spring集成
    mybatis动态sql和分页
    mybatis入门
    使用java代码操作redis
    Redis安装和基本操作
    idea安装及使用
    爬虫
  • 原文地址:https://www.cnblogs.com/weiyidedaan/p/13631119.html
Copyright © 2011-2022 走看看