如题,要求输出这两个数组中相同的元素个数(counter),
      还有就是对应下标上相同元素的个数(location);例如:arr1={1,2,3,4};  arr2={4,5,6,7};
  则:counter=1,location=0;
     
      下面是我试着写的,总感觉不够完善(注释部分的功能和下面的代码重复了),
      大家帮我改一下,或者有更高效的代码分享一下,不胜感激![code=java]
import java.util.Scanner;
public class Compare
{
private static Scanner scan;
public static void numCompare()
{
int counter = 0; //猜对的数字
int location = 0; //猜对的位置

scan = new Scanner(System.in);
int sysRandom = scan.nextInt();
int userInput = scan.nextInt();

int[] sysArr = { sysRandom/1000,         
sysRandom%1000/100,
sysRandom%1000%100/10,
sysRandom%1000%100%10 };
int[] userArr = { userInput/1000,         
userInput%1000/100,
userInput%1000%100/10,
userInput%1000%100%10 };
for(int i=0;i<4;i++)
{
if(userArr[i]==sysArr[i])
{
counter++;
}

// for(int j=0;j<4;j++)
// {
// if(userArr[j]==sysArr[i]&&i==j)
// {
// location++;
// }
// }

}

System.out.println("counter:"+counter);
System.out.println("location:"+location);
}
}
class Test
{
public static void main(String[] args)
{
Compare.numCompare();
}
}
[code=java]Java编程语言求助

解决方案 »

  1.   

    如果你有大量数要猜如果就4个数,不管什么算法复杂度都是O(1)啦
    另外你的代码是不是把location和counter写反了。。  public static void findNumOfMatches(final int[] generated, final int[] input) {
        Map<Integer, Integer> generatedMap = new HashMap<Integer, Integer>();
        for (int i = 0; i < generated.length; i++) {
          generatedMap.put(generated[i], i);
        }    int total = 0;
        int totalLocations = 0;
        for (int i = 0; i < input.length; i++) {
          Integer location = generatedMap.remove(input[i]);
          if (location != null) {
            total++;
            if (location == i)
              totalLocations++;
          }
        }    System.out.println("Total correct numbers: " + total);
        System.out.println("Correct locations: " + totalLocations);
      }
      

  2.   


    public class Test01 {
    public static void main(String[] args) {
    final int[] arr1 = new int[] { 1, 2, 3, 4 };
    final int[] arr2 = new int[] { 4, 5, 6, 7 };
    find(arr1, arr2);
    } static void find(int[] arr1, int[] arr2) {
    int N = arr1.length;
    int counter = 0, location = 0;
    //假设数字范围为[0,9]
    int[] result = new int[10];
    for(int i = 0;i < N;++i) {
    result[arr1[i]]++;
    result[arr2[i]]++;
    if(arr1[i] == arr2[i]) {
    location++;
    }
    }
    for(int i = 0;i < result.length;++i) {
    if(result[i] == 2) {//或者result[i] > 1
    counter++;
    }
    }
    System.out.printf("counter:%d,location:%d",counter,location);
    }
    }
      

  3.   

    public class Test0424 {
    static int counter,loc;
    public static void count(int[] a,int[] b) {
    int[] m = new int[a.length];
    for(int i=0;i<a.length;i++) {
    for(int j=0;j<b.length;j++) {
    if(a[i]==b[j]) {
    counter++;

    }
    m[i] = counter;
    counter = 0;
    }
    for(int i=0;i<m.length;i++) {
    System.out.println("数组b中与a["+i+"]相同的有"+m[i]+"个数");
    }
    }

    public static void location(int[] a,int[] b) {
    if(a.length>b.length) {
    int[] c = a;
    a = b;
    b = c;

    for(int i=0;i<a.length;i++) {
    if(a[i]==b[i]) {
    loc++;
    }
    }
    System.out.println("数组a中与b中相同下标且值相同的有"+loc+"个");
    }

    public static void main(String[] args) {
    int[] a1 = {1,2,3,4,5,6,9,10,8,5};
    int[] a2 = {1,2,3,4,5,6,9,10,8,5,1,2,3,4,5,6,9,10,8,5,1,2,3,4,5,6,9,10,8,5,1,2,3,4,5,6,9,10,8,5};
    count(a1,a2);
    location(a2,a1);

    }
    }
      

  4.   

    我是新手,自学的,用的都是很基础的知识,不知道对不对;运行结果是:
    数组b中与a[0]相同的有4个数
    数组b中与a[1]相同的有4个数
    数组b中与a[2]相同的有4个数
    数组b中与a[3]相同的有4个数
    数组b中与a[4]相同的有8个数
    数组b中与a[5]相同的有4个数
    数组b中与a[6]相同的有4个数
    数组b中与a[7]相同的有4个数
    数组b中与a[8]相同的有4个数
    数组b中与a[9]相同的有8个数
    数组a中与b中相同下标且值相同的有10个