马和瓦的问题

原意:大马,中马,小马一共有100匹,一头大马拉3块瓦,一头中马拉2块瓦,3头小马拉一块瓦,一共有100块瓦.问:大马,中马,小马 各有多少只?

用for 循环写的代码:

public class a{
 static int c;

    public static void main(String[] args){
        m1();
    }
    public static void m1(){
        for(int d=1; d < 100; ++d){
            for(int z=1; z <100 ; ++z){
                for(double x=1; x <100 ; ++x){
                    if (d+z+x==100 && 3*d+2*z+x/3==100) {
                        c+=1;
                        System.out.println( "第 "+ c + " 种答案 : " );
                        System.out.println( "大马 "+d+"   中马 "+z+"   小马"+x );
                    }
                }
            }
            
        }
    }
}

运行结果:

呵呵,自己没事写着玩的,写出来分享一下

原文地址:https://www.cnblogs.com/xrmqbl804915256/p/4722699.html