当前位置:编程学习 > JAVA >>

需要7m,11m,17m的管子各10根,至少需要多少根100m的管子,并求出每根管子的利用率

如题:java编码实现! --------------------编程问答--------------------

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: rollet
 * Date: 13-1-25
 * Time: 下午7:12
 * 需要7m,11m,17m的管子各10根,至少需要多少根100m的管子,并求出每根管子的利用率
 */
public class TestTwo {


    public static void main(String[] args) {
        PIPE test = new PIPE(7, 11, 17, 10, 0, 0, 0, 0);
        List<PIPE> list = test.jisuan(100);
        for (PIPE pipe : list) {
            System.out.print(pipe.getA() + "=" + pipe.getAvalue() + ",");
            System.out.print(pipe.getB() + "=" + pipe.getBvalue() + ",");
            System.out.print(pipe.getC() + "=" + pipe.getCvalue() + ",");
            System.out.println("第" + pipe.getNumvalue() + "根管子使用率为" + new BigDecimal(pipe.getA() * pipe.getAvalue() + pipe.getB() * pipe.getBvalue() + pipe.getC() * pipe.getCvalue()).setScale(0) + "%");
        }
    }

    static class PIPE {
        int a;
        int b;
        int c;
        int num;
        int avalue;
        int bvalue;
        int cvalue;
        int numvalue;


        PIPE(int a, int b, int c, int num, int avalue, int bvalue, int cvalue, int numvalue) {
            this.a = a;
            this.b = b;
            this.c = c;
            this.num = num;
            this.avalue = avalue;
            this.bvalue = bvalue;
            this.cvalue = cvalue;
            this.numvalue = numvalue;
        }

        public int getA() {
            return a;
        }

        public int getB() {
            return b;
        }

        public int getC() {
            return c;
        }

        public int getNum() {
            return num;
        }

        public int getAvalue() {
            return avalue;
        }

        public int getBvalue() {
            return bvalue;
        }

        public int getCvalue() {
            return cvalue;
        }


        public int getNumvalue() {
            return numvalue;
        }

        public List<PIPE> jisuan(int i) {
            List<PIPE> list = new ArrayList<PIPE>();
            int ge = i;
            numvalue++;
            int alock = 0;
            int block = 0;
            int clock = 0;
            while (ge >= 0) {
                if (ge >= c && alock < num) {
                    ge -= c;
                    cvalue++;
                    alock++;
                } else if (ge >= b && block < num) {
                    ge -= b;
                    bvalue++;
                    block++;
                } else if (ge >= a && clock < num) {
                    ge -= a;
                    avalue++;
                    clock++;
                } else {
                    ge = i;
                    list.add(new PIPE(a, b, c, num, avalue, bvalue, cvalue, numvalue));
                    numvalue++;
                    avalue = 0;
                    bvalue = 0;
                    cvalue = 0;
                }
                if (clock == 10 && alock == 10 && block == 10) {
                    list.add(new PIPE(a, b, c, num, avalue, bvalue, cvalue, numvalue));
                    break;
                }
            }
            return list;
        }
    }

}


--------------------编程问答-------------------- for(int i=100/17;i>0;i--){
        for(int j=(100-i*17)/11;j>0;j--){
        if((100-i*17-j*11)%SEVEN==0){
        System.out.println("17厘米的用了="+i);
        System.out.println("11厘米的用了="+j);
        System.out.println("7厘米的用了="+(100-i*17-j*11)/7);
        return ;
        }
        }
       }

不知道是不是你想要的。
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,