1题
1思路
1代码
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
HashMap<Integer, Integer> counter = new HashMap<Integer, Integer>();
for (Integer a : arr) {
if (counter.containsKey(a)) {
int oldValue = counter.get(a);
counter.put(a, oldValue + 1);
} else {
counter.put(a, 1);
}
}
int arr1[]=new int[counter.size()];
int j=0;
for(Integer values:counter.values())
{
arr1[j]=values;
j++;
}
int temp=arr1[0];
for(int i=1;i<arr1.length;i++){
temp=gcd(temp,arr1[i]);
if(temp==1){
System.out.println("false");
return;
}
}
System.out.println("true");
}
public static int gcd(int a, int b) {// 循环实现
int k = 0;
if(a<b){
k=a;
a=b;b=k;
}else{
k=1;
}
while(k>0){
k=a%b;
a=b;
b=k;
}
return a;// 返回被除数
}
}
1结果
不明原因报错