Java编程挑战:最少钱币组合求解及圆锥体类实现详解
Java编程:最少钱币数,这是一个古老而又经典的问题,用给定的几种钱币
/***普通人民币金额的简单初始化*/publicclassTest{publicstaticvoidmain(String[]args){int[]commonMoney={100,50,20,10,5,1};System.out.println(getLeastNum(108,commonMoney));}staticStringgetLeastNum(inttargetMoney,int[]commonMoney){StringBufferbuffer=newStringBuffer();intnum=0,total=0;Stringresult=targetMoney+"需要$最低金额";for(intmoney:commonMoney){if(targetMoney>=money){total+=num=targetMoney/money;targetMoney=targetMoney%money;buffer.append(","+num+"张"+money+"$");}if(0==targetMoney)break;}returnresult+total+"张币分别为:"+buffer.substring(1);}}输出:108$需要至少5点钱,即:1件100$,1件5$,3件1$有关java编程题目?
按照课题要求编写的圆、锥体和测试类Java程序如下
test.java文件内容如下
classCircle{
PrivateDoubler;
PrivateStringgcolor;
publiccircle(doubler){
this.r=r;
}
publicdoublearea(){
returnMath.PI*r*r;
}
publicdoubleperimeter(){
returnMath.PI*2*r;
}
publicdoublegetR()
返回is.r;
}
publicvoidsetR(doubler){
this.r=r;
}
publicStringgetColor(){
返回它的.color;
}
publicvoidsetColor(Stringcolor){
>this.color=color;
}
publicStringToString(){
返回"圆的半径为"+r+",颜色为"+color;
}
}
classcon{privatecircle;
privateedoubleh;
privateStringcolor;
publicconic(Circlec,doubleh){
this.c=c;
this.h=h;
}
publicdoublevolume(){
return1.0/3*c.area()*h;
}
publicCirclegetCircle(){
退货.c;
}
publicvoidsetCircle(circle){
this.c=c;
}
publicdoublegetH(){
返回他的。
publicStringgetColor(){
返回this.color;
}
publicvoidsetColor(Stringcolor){
this.color=color;
}
publicStringToString(){
return"圆锥体的底面积为"+c.area()+",高度是“+h+”,颜色是“+color;
}
}
publicClassTest
publiccstaticvoidmain(String[]args){
Circlecircle1=newCircle(2.5);
circle1.setColor('red');
System.out.println(circle1.toString());
System.out.('圆的面积是'+circle1.area());
System.out.println("圆的周长为"+circle1.perimeter());
Conecirclar1=newCone(circle1,2.7);
circlar1.setColor("blue");
System.outt.println(circlar1.toString());
System.out.println("圆锥体的体积是"+circlar1.volume());
}
}