2012-03-01 13:55:28有1元2元5元10元组成100有几种组成方法
  用程序写出来
 
 

解决方案 »

  1.   

    package com;import java.util.Arrays;
    import java.util.Scanner;public class Compare {
    Scanner in = new Scanner(System.in);
    public static int z = 0;
    public static int expectedValue;
    public static int a;
    public static int b;
    public static int c;
    public static int d; public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("请输入四个数字...");
    a = in.nextInt();
    b = in.nextInt();
    c = in.nextInt();
    d = in.nextInt();
    System.out.println("请输入期望值...");
    expectedValue = in.nextInt();
    System.out.println("四个数字是:" + a + "," + b + "," + c + "," + d
    + "..期望值是:expectedValue=" + expectedValue);
    int[] array = { a, b, c, d };
    Arrays.sort(array);
    StringBuffer buffer = new StringBuffer();
    for (int o = 0; o < array.length; o++) {
    a = array[o];
    for (int p = 0; p < array.length; p++) {
    if (p != o) {
    b = array[p]; for (int q = 0; q < array.length; q++) {
    if (q != o && q != p) {
    c = array[q];
    for (int index = 0; index < array.length; index++) {
    if (index != o && index != p && index != q) {
    d = array[index];
    for (double i = 0; i < 2; i++) {
    if (i == 0) {
    for (double j = 0; j < array.length; j++) {
    for (double k = 0; k < array.length; k++) {
    fun(oper(a, b, j), arith(c,
    d, k), j, k, buffer);
    }
    }
    }
    if (i == 1) {
    for (int j = 0; j < array.length; j++) {
    for (int k = 0; k < array.length; k++) {
    double sum = arith(oper(a,
    b, j), c, k);
    func(sum, d, j, k, buffer);
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    } } private static void func(double i, double j, int j2, int k2,
    StringBuffer buffer) {
    String str = "";
    String str1 = "";
    if (j2 == 0) {
    str = "+";
    } else if (j2 == 1) {
    str = "-";
    } else if (j2 == 2) {
    str = "*";
    } else {
    str = "/";
    }
    if (k2 == 0) {
    str1 = "+";
    } else if (k2 == 1) {
    str1 = "-";
    } else if (k2 == 2) {
    str1 = "*";
    } else {
    str1 = "/";
    }
    for (double k = 0; k < 4; k++) {
    if (k == 0) {
    if ((i + j) == expectedValue) {
    String bb = "[(" + a + str + b + ")" + str1 + c + "]" + "+"
    + d + "=" + expectedValue;
    if (buffer.indexOf(bb) == -1) {
    System.out.println(bb);
    buffer.append(bb);
    }
    }
    }
    if (k == 1) {
    if ((i - j) == expectedValue) {
    String bb = "[(" + a + str + b + ")" + str1 + c + "]" + "-"
    + d + "=" + expectedValue;
    if (buffer.indexOf(bb) == -1) {
    System.out.println(bb);
    buffer.append(bb);
    }
    }
    } if (k == 2) {
    if ((i * j) == expectedValue) {
    String bb = "[(" + a + str + b + ")" + str1 + c + "]" + "*"
    + d + "=" + expectedValue;
    if (buffer.indexOf(bb) == -1) {
    System.out.println(bb);
    buffer.append(bb);
    }
    }
    }
    if (k == 3) {
    if ((i / j) == expectedValue) {
    String bb = "[(" + a + str + b + ")" + str1 + c + "]" + "/"
    + d + "=" + expectedValue;
    if (buffer.indexOf(bb) == -1) {
    System.out.println(bb);
    buffer.append(bb);
    }
    }
    }
    }
    } private static void fun(double i, double j, double j2, double k2,
    StringBuffer buffer) {
    String str = "";
    String str1 = "";
    if (j2 == 0) {
    str = "+";
    } else if (j2 == 1) {
    str = "-";
    } else if (j2 == 2) {
    str = "*";
    } else {
    str = "/";
    }
    if (k2 == 0) {
    str1 = "+";
    } else if (k2 == 1) {
    str1 = "-";
    } else if (k2 == 2) {
    str1 = "*";
    } else {
    str1 = "/";
    }
    for (double k = 0; k < 4; k++) {
    if (k == 0) {
    if ((i + j) == expectedValue) {
    String bb = "(" + a + str + b + ")+(" + c + str1 + d + ")="
    + expectedValue;
    if (buffer.indexOf(bb) == -1) {
    System.out.println(bb);
    buffer.append(bb);
    }
    }
    }
    if (k == 1) {
    if ((i - j) == expectedValue) {
    String bb = "(" + a + str + b + ")-(" + c + str1 + d + ")="
    + expectedValue;
    if (buffer.indexOf(bb) == -1) {
    System.out.println(bb);
    buffer.append(bb);
    }
    }
    } if (k == 2) {
    if ((i * j) == expectedValue) {
    String bb = "(" + a + str + b + ")*(" + c + str1 + d + ")="
    + expectedValue;
    if (buffer.indexOf(bb) == -1) {
    System.out.println(bb);
    buffer.append(bb);
    }
    }
    }
    if (k == 3) {
    if ((i / j) == expectedValue) {
    String bb = "(" + a + str + b + ")/(" + c + str1 + d + ")="
    + expectedValue;
    if (buffer.indexOf(bb) == -1) {
    System.out.println(bb);
    buffer.append(bb);
    }
    }
    }
    }
    } public static double oper(double a, double b, double j) {
    double z = 0;
    if (j == 0) {
    z = a + b;
    } else if (j == 1) {
    z = a - b;
    } else if (j == 2) {
    z = a * b;
    } else if (j == 3) {
    z = a / b;
    }
    return z;
    } public static double arith(double c, double d, double k) {
    double z = 0;
    if (k == 0) {
    z = c + d;
    } else if (k == 1) {
    z = c - d;
    } else if (k == 2) {
    z = c * d;
    } else if (k == 3) {
    z = c / d;
    }
    return z;
    }
    }
    直接运行就可以了、
      

  2.   

    不用了看懂了
       主要是哪个记得for改成递归就好了代码就少了
      

  3.   

    最愚蠢的代码 public static void main(String[] args) throws Exception {
    int[] num=new int[]{1,2,5,10};
    for(int i=0;i<100/num[0];i++){
    for(int j=0;j<100/num[1];j++){
    for(int k=0;k<100/num[2];k++){
    for(int m=0;m<100/num[3];m++){
    if(num[0]*i+num[1]*j+num[2]*k+num[3]*m==100){
    System.out.println(String.format("%d*%d+%d*%d+%d*%d+%d*%d=100", num[0],i,num[1],j,num[2],k,num[3],m));
    }
    }
    }
    }
    }
    }
      

  4.   


    public static void main(String[] args) {
    int num=0;

    for(int i=0;i<=100;i++){
    for(int j=0;j<=50;j++){
    for(int k=0;k<=20;k++){
    for(int h=0;h<=10;h++){
    int t=i*1+j*2+k*5+h*10;
    if(t==100){
    num++;
    System.out.println("1元的有"+i+"个,2元的有"+j+"个,5元的有"+k+"个,10元的有"+h+"个");
    }
    }
    }
    }

    }
    System.out.println("一共有"+num+"种方法组成");
    }
      

  5.   

    public static void main(String[] args) {
    int num=0;

    for(int i=0;i<=100;i++){
    for(int j=0;j<=50;j++){
    for(int k=0;k<=20;k++){
    for(int h=0;h<=10;h++){
    int t=i*1+j*2+k*5+h*10;
    if(t==100){
    num++;
    System.out.println("1元的有"+i+"个,2元的有"+j+"个,5元的有"+k+"个,10元的有"+h+"个");
    }
    }
    }
    }

    }
    System.out.println("一共有"+num+"种方法组成");
    }