您的位置 首页 编程语言

isDigitIncreasing

美国程序员面试题

 

A number is called digit-increasing if it is equal to n + nn + nnn + … for some digit n between 1 and 9. For example 24 is digit-increasing because it equals 2 + 22 (here n = 2)
Write a function called isDigitIncreasing that returns 1 if its argument is digit-increasing otherwise, it returns 0.
The signature of the method is int isDigitIncreasing(int n)

 

 

java 实现代码:

package com.zzy;

/**
 * 更多请关注: http://huamaodashu.com
 * Created by huamaodashu on 31/07/2018.
 */
public class DigitIncreasing {
    public static void main(String[] args) {

//        System.out.println(numOfN(8));
        System.out.println(isDigitIncreasing(7404));
    }

    public static int isDigitIncreasing(int n){


        int i = 0;

        while (i<10){

            int tmp = 0;
            int x = numOfN(n);
            for (int j = 1; j <=x; j++) {
//                System.out.println("j="+j);
                tmp = tmp + numbs(i,j);
//                System.out.println("tmp="+tmp);
                if(tmp == n){
                    return 1;
                }
            }
            i++;
        }
        return 0;
    }

    /*
    判断一个 456 有3个数字组成
     */
    public static int numOfN(int n){
        int count = 0;
        while (n/10 != 0){
            count++;
//            System.out.println("count="+count);
            n = n /10;
        }
        return count+1;
    }

    public static int numbs(int a,int n){
        int sum = a;
        for (int i = 1; i < n; i++) {
            sum=sum+a*(int)Math.pow(10,i);
        }

//        System.out.println(a);
        return sum;
    }
}

猫叔总结了 适合新手操作的副业 《淘宝虚拟产品月入2万的 6个 细分类目》的电子书 仅供参考

如果你对虚拟产品比较感兴趣,可以点击:

淘宝卖什么虚拟产品赚钱(月入2万+)

hadoopall

关于花猫大叔短视频创业 作者: hadoopall

热门文章

发表评论

电子邮件地址不会被公开。