8

java编程题 - SegmentFault 思否

 3 years ago
source link: https://segmentfault.com/a/1190000040151464
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

java编程题


  • 前言
    写一些Java编程题有助于稳定基础,希望大家一起进步
    注:本片文章有 借鉴编程前辈。

1.水仙花数


int num1,num2,num3;
        for (int i = 100; i < 1000; i++) {

            // 百
            num1 = i / 100;
            // 十
            num3 = i % 100 / 10 ;
            // 个
            num2 = i % 10 ;

            if ((num1 * num1 * num1 + num2 * num2 * num2 + num3 * num3 * num3) == i){
                System.out.println(i);
            }
        }

解析:
注意int 类型不保留小数 所以 1.53 = 1

153 / 100 = 1.53

153 % 100 = 53; (53 / 10 = 5.3 )

153 % 10 = 3


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK