首页 > 学院 > 开发设计 > 正文

华为上机测试题(水仙花数升级版-java)

2019-11-14 21:14:36
字体:
来源:转载
供稿:网友
华为上机测试题(水仙花数升级版-java

PS:这题满分100,没有做对,大家帮忙看看问题在哪

/** 题目:水仙花数升级版 * 描述: 水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 5^3+ 3^3 = 153)给你A和B,求[A,B]区间内有多少个水仙花数题目类别: 循环,查找,枚举,位运算 难度: 中级 分数: 100 运行时间限制: 无限制 内存限制: 无限制 阶段: 应聘考试 输入: 两个正整数,用空格隔开,保证数字都小于等于1000000。输出: 一个数字,表示[A,B]区间内的水仙花数个数样例输入: 100 1000样例输出: 4答案提示: 100~1000的水仙花数有:153,370,371,407 */

 1 import java.util.Scanner; 2  3 public class Main { 4  5     public static void main(String[] args) { 6          7         int a = 0; 8         int b = 0; 9         int count = 0;10         11         Scanner s = new Scanner(System.in);12         String str = s.nextLine();13         String[] strArray = str.split(" ");14         a = Integer.parseInt(strArray[0]);15         b = Integer.parseInt(strArray[1]);16         s.close();17         if((a < 100 || a > 1000000) || (b < 100 || b > 1000000))18         {19             throw new RuntimeException();20         }21         22         count = getNum(a, b);23         24         System.out.PRintln(count);25     }26 27     public static int getNum(int a, int b) {28         29         int result = 0;30         31         for(int i = a; i <= b; i++)32         {33             int m = i;34             int tmp = 0;35             int value = 0;36             37             while(0 != m)38             {39                 40                 tmp = m%10;41                 value += Math.pow(tmp, 3);42                 m /= 10;43             }44             45             if(value == i)46             {47                 result++;48             }49         }50         return result;51     }52     53 }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表