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