求算法。给定2个4位的各个位数上的数字都不重复的数字,如果他们位置和数字都相等,为A,如果数字相等位置不等,为B。例如:
       给定的第1个数:5673
       第2个数:      0153
       这样匹配的结果为:1A1B
       变换第2个数各个数字的位置:5013
       这样匹配的结果为2A0B
不知道大家明白我的意思没有,跪求该算法!!!

解决方案 »

  1.   

    import java.io.IOException;
    import java.util.Scanner;public class Main {
    public static void main(String[] args) throws IOException {
    String num1, num2;
    // user String to save **** int
    Scanner in = new Scanner(System.in);
    num1 = in.nextLine();
    num2 = in.nextLine();
    int A = 0, B = 0;
    for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
    if (num1.charAt(i) == num2.charAt(j))
    B++;
    }
    } for (int i = 0; i < 4; i++) {
    if (num1.charAt(i) == num2.charAt(i))
    A++;
    }
    System.out.println(A + "A" + (B - A) + "B"); }
    }
      

  2.   

    1A1B
    2A0B
    0,1,2怎么来的
      

  3.   


    public static void main(String[] args) {
    int a = 5673;
    int b = 5013;
    HashMap list = new HashMap();
    while (a > 0) {
    list.put(list.size(), a % 10);
    a /= 10;
    }
    int k1 = 0, k2 = 0;
    int i = 0;
    while (b > 0) {
    int t = b % 10;
    if (list.containsValue(t)) {
    if (list.get(i) == Integer.valueOf(t))
    k1++;
    else
    k2++;
    }
    i++;
    b /= 10;
    }
    System.out.println(k1 + "A" + k2 + "B");
    }
      

  4.   


          int A = 0;
          int B = 0;      String s1 = "5673";
          String s2 = "0153";      if(s1.length() == s2.length()) {
             for(int i = 0; i < s1.length(); i++) {
                char c1 = s1.charAt(i);            for(int j = 0; j < s2.length(); j++) {
                   char c2 = s2.charAt(j);               if(c1 == c2) {
                      if(i == j) {
                         A++;
                      }
                      else {
                         B++;
                      }
                   }
                }
             }
          }      System.err.println(A + "A" + B + "B");
    算法有点烂。
      

  5.   


    public static void main(String[] args){
    int a = 0, b = 0;
    String s1 = "5673";
    String s2 = "5013";
    for (int j = 0, i = 0; i<s1.length();)
    {
    if (s1.charAt(i)==s2.charAt(j)){
    if (i==j){
    a++;
    }
    else{
    b++;
    }
    }
    j++;
    if (j==s1.length()){
    i++;
    j = 0;
    }
    }
    System.out.println(a+"A"+b+"B");
    }
      

  6.   


    char [] num1 = new Integer(5673).toString().toCharArray();
            char [] num2 = new Integer(5013).toString().toCharArray();
            int counterA = 0, counterB = 0;
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    if (j!=i&&num1[i] == num2[j])
                        counterB++;
                }
                if (num1[i] == num2[i] )counterA++;                
            }
            System.out.println(counterA + "A" + counterB + "B");
      

  7.   

    算法不会,只会 code
    对于两个位数不重复的可以这样public String t3(){
    int A = 0;
    int B = 0;
    int num1 = 1345;
    int num2 = 1534;

    if(num1 == num2){
    A = 4;
    B = 0;
    return A+"A"+B+"B";
    }

    String str1 = "" + num1;
    String str2 = "" + num2; char[] chars1 = str1.toCharArray();
    char[] chars2 = str2.toCharArray();
    for(int i =0 ; i< chars1.length;i++){
    int c = chars1[i];
    if(str2.indexOf(c) != -1){//有此
    if(str2.charAt(i) == c){
    A ++ ;
    }else{
    B ++;
    }
    }
    }


    return A+"A"+B+"B";
    }
      

  8.   

      public static void main(String[] args) throws Exception {
        Scanner in = new Scanner(System.in);
        for (;;) {
          String num1 = in.nextLine();
          String num2 = in.nextLine();
          int mask1 = 0;
          int mask2 = 0;
          int a = 0;
          for (int i = 0; i < 4; i++) {
            int n1 = num1.charAt(i) - '0';
            int n2 = num2.charAt(i) - '0';
            mask1 |= (1 << n1);
            mask2 |= (1 << n2);
            if (n1 == n2) {
              a++;
            }
          }
          int b = 0;
          for (int i = 0; i < 10; i++, mask1 >>= 1, mask2 >>= 1) {
            int m1 = mask1 & 1;
            int m2 = mask2 & 1;
            if (m1 + m2 == 2) {
              b++;
            }
          }
          System.out.printf("%dA%dB%n", a, b);
        }
      }
      

  9.   

    使用二进制计数器,等价于数组计数器
        Scanner in = new Scanner(System.in);
        for (;;) {
          String num1 = in.nextLine();
          String num2 = in.nextLine();
          int[] count1 = new int[10];
          int[] count2 = new int[10];
          int a = 0;
          for (int i = 0; i < 4; i++) {
            int n1 = num1.charAt(i) - '0';
            int n2 = num2.charAt(i) - '0';
            count1[n1]++;
            count2[n2]++;
            if (n1 == n2) {
              a++;
            }
          }
          int b = 0;
          for (int i = 0; i < 10; i++) {
            if (count1[i] + count2[i] == 2) {
              b++;
            }
          }
          System.out.printf("%dA%dB%n", a, b);
        }
      

  10.   

    我的也能实现:import java.util.Scanner;public class MyTest {
    public static void main(String[] args){
    Scanner scaner = new Scanner(System.in);
    String one = scaner.nextLine();
    String two = scaner.nextLine();
    int A = 0;
    int B = 0;
    System.out.println(one);
    System.out.println(two);
    for(int i=0;i<4;i++){
    for(int j=0;j<4;j++){
    if((one.charAt(i) == two.charAt(j))&&(i==j)){
    A++;
    }
    if((one.charAt(i) == two.charAt(j))&&(i!=j)){
    B++;
    }
    }
    }

    System.out.println(A+"A"+B+"B");
    }
    }
      

  11.   


    public static void main(String[] args) {
    String numA = "1234";
    String numB = "3264"; int pA = 0;
    int pB = 0; for (int i = 0; i < numA.length(); i++) {
    int index = numB.indexOf(String.valueOf(numA.charAt(i))); if (index != -1) {
    if (index == i) {
    pB++;
    } else {
    pA++;
    }
    }
    }
    System.out.println(pA + "A" + pB + "B");
    }