1.美国面试题7
The Stanton measure of an array is computed as follows. Count the number of 1s in the array. Let this count be n. The Stanton measure is the number of times that n appears in the array. For example, the Stanton measure of {1, 4, 3, 2, 1, 2, 3, 2} is 3 because 1 occurs 2 times in the array and 2 occurs 3 times.
if a is
return
reason
{8, 4, 2, 1}
1
This is the Guthrie sequence for 8
{8, 17, 4, 1}
0
This is not the Guthrie sequence for 8
{8, 4, 1}
0
Missing the 2
{8, 4, 2}
0
A Guthrie sequence must end with 1
Write a function named stantonMeasure that returns the Stanton measure of its array argument. If you are programming in Java or C#, the function prototype is
int stantonMeasure(int[ ] a)
If you are programming in C++ or C, the function prototype is
int stantonMeasure(int a[ ], int len) where len is the number of elements in the array.
2. 测试样例:
3.java代码实现
public class StantonMeasure {
public static void main(String[] args) {
// int a[] = {3,1,1,4};
// int a[] = {1,3,1,1,3,3,2,3,3,3,4};
// int a[] = {1,2,1,2};
// int a[] = {1};
int a[] ={};
System.out.println(stantonMeasure(a));
}
public static int stantonMeasure(int[] a){
int count =0;
int num =0;
for (int i=0;i<a.length;i++){
if(a[i]==1){
count++;
}
}
for (int i=0;i<a.length;i++){
if(a[i]==count){
num++;
}
}
return num;
}
}