日期:2014-05-20 浏览次数:20818 次
/* * 错误。。。待纠正 */ public class P142_4{ public static void main (String[]args){ int men =0 ; int women=0; int kids=0; int a = women+kids; for ( int i = 1 ;i <=10 ;i++,men++,kids++ ){ for ( women = 1; women <=30-(men+kids);women++){ int money = men*3 + women*2 +kids ; System.out.println("****************"); System.out.println("男人有:"+men); System.out.println("女人有:"+women); System.out.println("小孩有:"+kids); System.out.println("****************"); System.out.println("分数是:"+money); int people = men + a ; System.out.println("****************"); System.out.println("人数是:"+people); if ( people == 30 && money==50 ){ System.out.println("男人有:"+men); System.out.println("女人有:"+women); System.out.println("小孩有:"+kids); break;} } } System.out.println("程序结束!"); } }
int man,woman,kid,jiFen; for (int i = 1; i <30; i++) { man = i; for (int j = 1; j < 30; j++) { woman = j; kid = (30-man-woman); jiFen = man*3+woman*2+kid; if(jiFen==50){ System.out.println(man+"*3 +"+woman+"*2+"+kid+"=50"); } } }
------解决方案--------------------
两层就够了。
for ( int i = 0 ;i <=10;i++){
for(int j= 0;j<=30-3*i;j++){
int count = 3*i+2*j+(30-i-j);
if(count==50){
System.out.println(i+" "+j+" "+(30-i-j));
}
}
}
------解决方案--------------------
public class Test21 { public static void main(String[] args) { for (int man = 0; man < 17; man++) { for (int woman = 0; woman < 26; woman++) { if (man * 3 + 2 * woman + (30 - man - woman) == 50) { System.out.println("男人有:" + man); System.out.println("女人有:" + woman); System.out.println("小孩有:" + (30 - man - woman)); System.out.println("****************"); } } } System.out.println("程序结束!"); } }