读书人

C语言生趣程序百例精解之JAVA实现

发布时间: 2012-09-05 15:19:34 作者: rapoo

C语言趣味程序百例精解之JAVA实现

C语言趣味程序百例精解之JAVA实现:
76.小明买书?
C语言生趣程序百例精解之JAVA实现


public class Test76{
public static void main(String args[]){
????buyBooks76();
}????
????/**??
???? * 76.小明买书?
???? *???
???? */??
????public static??void buyBooks76() {???
????????double a = 3.1;???
????????double b = 1.7;???
????????double c = 2;???
????????double d = 5.3;???
????????double e = 0.9;???
????????double f = 7.2;???
????????double cha = 0.1;???
????????double s = 0;???
????????double min=0;
??
????????for (int a1 = 0; a1 <= 1; a1++)???
????????????for (int b1 = 0; b1 <= 1; b1++)???
????????????????for (int c1 = 0; c1 <= 1; c1++)???
????????????????????for (int d1 = 0; d1 <= 1; d1++)???
????????????????????????for (int e1 = 0; e1 <= 1; e1++)???
????????????????????????????for (int f1 = 0; f1 <= 1; f1++) {???
????????????????????????????????s = a1 * a + b1 * b + c1 * c + d1 * d + e1 * e???
????????????????????????????????????????+ f1 * f;???
????????????????????????????????if (s != 0 && Math.abs(s - 10) <= cha) {//相差一角钱
????????????????????????????????????cha = Math.abs(s - 10);???
????????????????????????????????????System.out.print("差值:" + cha);???
????????????????????????????????????if(a1!=0) System.out.print(" a=" + a);???
????????????????????????????????????if(b1!=0) System.out.print(" b=" + b);???
????????????????????????????????????if(c1!=0) System.out.print(" c=" + c);???
????????????????????????????????????if(d1!=0) System.out.print(" d=" + d);???
????????????????????????????????????if(e1!=0) System.out.print(" e=" + e);???
????????????????????????????????????if(f1!=0) System.out.print(" f=" + f);???
????????????????????????????????????System.out.println();???
????????????????????????????????}???
????????????????????????????}???
????}???
}
运行:
C:\bat>java Test76
差值:0.09999999999999964 c=2.0 e=0.9 f=7.2
差值:0.09999999999999964 b=1.7 c=2.0 d=5.3 e=0.9
差值:0.09999999999999964 a=3.1 b=1.7 d=5.3

读书人网 >C语言

热点推荐