您的位置 首页 编程语言

Write a method call repsEqual that takes an array and an integer and returns 1 if the array contains only the digits of the number in the same order that they appear in the number.

美国面试题11

1.题目:

An array can hold the digits of a number. For example the digits of the number 32053 are stored in the array {3, 2, 0, 5, 3}. Write a method call repsEqual that takes an array and an integer and returns 1 if the array contains only the digits of the number in the same order that they appear in the number. Otherwise it returns 0.
If you are programming in Java or C#, the function prototype is
int repsEqual(int[ ] a, int n)
If you are programming in C++ or C, the function prototype is
int repsEqual(int a[ ], int len, int n) where len is the number of elements in the array.

 

2. 样例

 

 

3.java代码实现

public class RepsEqualDemo {
    public static void main(String[] args) {
//        int[] a = {3,2,0,5,3 };
//        int[] a = {3,2,0,5 };
//        int[] a = {3,2,0,5,3,4 };
//        int[] a = {2,3,0,5,3 };
//        int[] a = {9,3,1,1,2 };
        int[] a = {0,3,2,0,5,3 };
        int n = 32053;
        System.out.println(repsEqual(a,n));
    }
    public static int repsEqual(int[]a , int n ){
        int length = a.length;
        int tmp =0;
        for (int i=0; i<length; i++){
           tmp = tmp + (int) ((int) a[i]*Math.pow(10,length-1-i));
            System.out.println("tmp"+tmp);
        }
        if(n == tmp){
            return 1;
        }
        return 0;

    }
}

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

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

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

hadoopall

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

热门文章

发表评论

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