美国程序员面试题
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;
}
}