Java written 24-point card game _java

Source: Internet
Author: User
Tags abs int size

Any 4 1-13 digits, subtraction calculates 24 points.

Implementation principle:

1) permutations and combinations of 4 digits
2 calculate the probability of each permutation combination

Cal24.java

Import Java.util.HashSet;
 
 
Import Java.util.Set;
  public class Cal24 {private static final double precision = 0.00001;
   
 
  private static final int target = 24;
    Public string[] Execute (string[] inputs) {int[] digits = new INT[4];
    for (int i = 0; i < inputs.length i++) {Digits[i] = integer.valueof (inputs[i));
  Return to new String[]{calc (digits)};
    Private String Calc (final int data[]) {final set<string> out = new hashset<string> (); Combination digit = new combination () {@Override protected void handle (int[] result) {final 
        Int[] R = result; Combination oper = new combination () {@Override protected void handle (int[] c) {Double X
            = R[0];
            for (int i = 0; i < r.length-1 i++) {x = Docalculate (x, R[i + 1], c[i]); } if (Math.Abs (Math.Abs (x)-target) < precision | | Math.Abs (Math.Abs (1/x)-TArget) < precision) {StringBuilder SB = new StringBuilder ();
                for (int j = 0; J < R.length; J + +) {sb.append (r[j]);
                if (J!= r.length-1) {sb.append (Getoperation (C[J)));
            } out.add (Sb.tostring ());
        }
             
          }
        };
         
      Oper.combine (New int[]{0, 1, 2, 3}, Data.length-1, true);
     
    }
    };
     
    Digit.combine (data);
    StringBuilder sb = new StringBuilder ();
      for (string string:out) {sb.append (string);
    Sb.append ("\ n");
  return sb.tostring ();
      Private double Docalculate (double x, double y, int operation) {switch (operation) {case 0:
    return x + y;
    Case 1:return XY;
    Case 2:return x * y;
    Case 3:return x/y;
    Default:return 0;
    }} private static String getoperation (int operation) {Switch (operation) {case 0:return "+";
    Case 1:return "-";
    Case 2:return "*";
    Case 3:return "/";
    Default:return "";
  } public static void Main (string[] args) {System.out.println (New Cal24 (). Calc (New Int[]{1, 5, 5, 5})); }
}

Combination.java

Public abstract class Combination {private Boolean repeat;
   
  private int total = 0;
  public void Combine (int data[]) {Combine (data, data.length, false);
  public void Combine (int data[], int count) {combine (data, count, false);
    public void Combine (int data[], int count, Boolean repeat) {this.repeat = repeat;
    int times = Data.length;
    int size = (int) Math.pow (times, Count);
      for (int i = 0; i < size; i++) {int[] result = ToArray (data, I, count);
        if (result!= null) {handle (result);
      Total + +;
    }} private int[] ToArray (int data[], int i, int count) {int [] indices = new Int[count];
    int times = Data.length;
      for (int j = 0; J < Count; J +) {int temp = 0;
        if (i > 0) {temp = I%times;
      i = (i-temp)/times;
    } Indices[j] = temp;
     } if (!repeat) {//remove repetition for (int x = 0; x < count + x + +) {   for (int y = 0; y < count; y++) {if (x!= y) {if (indices[x] = = Indices[y]) return
          Null
    [] [] result = new Int[count];
      for (int x = 0; x < count x + +) {int selected = data[indices[x]];
    RESULT[X] = selected;
  return result;
  public int gettotal () {return total;
} protected abstract void handle (int[] result); }

The above is the entire contents of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.