java求鸡兔同笼问题

import java.util.Scanner;

public class jitutonglong
{
static int j,t;
public static int qiongju(int head,int foot)
{
int re,x,y;
re=0;
for(x=0;x<=head;x++)
{
y=head-x;
if(x*2+y*4==foot)
{
re=1;
j=x;
t=y;

}

}
return re;
}
public static void main(String args[])
{
int re,head,foot;
System.out.println("穷举法求鸡兔同笼问题:");
System.out.println("请输入头数:");
Scanner input=new Scanner(System.in);
head=input.nextInt();
System.out.println("请输入脚数:");
foot=input.nextInt();
re=qiongju(head, foot);
if(re==1)
{
System.out.println("鸡有"+j+"只,兔有"+t+"只");
}
else
{
System.out.println("无法求解。");
}
}

}

原文地址:https://www.cnblogs.com/fanzhengzheng/p/7518069.html