出现次数最多的整数
思路:数组是排好序的,所以只需要进行以下两步:1.计算每个数字出现的次数,并保存在一个数组里;2.比较次数数组,输出出现次数最多的数。
我写了两个数组,我看到有使用二维数组的方法,感觉这个更好些。
但我的答案还是有问题,只有80分。先记录下来吧。
import java.util.Scanner;
public class mostinteger {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int array[]=new int[n];
if (n <= 0 && n > 20) {
System.out.println();
} else if (n > 0 && n <= 20) {
for(int i=0;i<n;i++)
{
array[i]=sc.nextInt();
}
int times[]=new int[n];
for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
{
if(array[j]==array[k])
times[j]++;
}
}
int m = 1;
int result=0;
for (int i = 0; i < n; i++) {
if (m < times[i]) {
result = array[i];
m = times[i];
}
}
System.out.println(result);
}
}
}