package ChongXingZuo;
/**鸡兔同笼:
*
**今有鸡兔同笼,上有三十五头,下有九十四足,问鸡兔各几何?
**分析:设所求的兔子数是rabbit只,鸡数是chicken只,
**已知笼子里的头数是head,脚数是foot,
**依题意,得到如下的方程组:
**rabbit+chicken=head,2*chicken+4*rabbit=foot
**
**解得:chicken=head-rabbit,rabbit=(foot-head*2)/2
*@author 暮之沧蓝
*/
import java.util.Scanner;
public class ChickenAndRabbit {
public static void ChickenAndRabbit(int head, int foot, int[] rabbit, int[] chicken) {
/**创建静态方法,输入参数head,foot;
* 输入参数rabbit为指向兔子数量的引用,
* 参数chicken为指向鸡数量的引用,
* 这里采用引用保存兔子和鸡的数量,便于返回结果
*/
rabbit[0] = (foot - 2 * head) / 2;
chicken[0] = head - rabbit[0];
/**
* 或者:rabbit[0] = foot/2-head;
* chicken[0] = 2*head-foot/2;
*/
}
public static void main(String[] args) {
int[] rabbit= {0},chicken= {0};
Scanner in = new Scanner(System.in);
System.out.println("请输入头数:");
int head = in.nextInt();
System.out.println("请输入脚数:");
int foot = in.nextInt();
ChickenAndRabbit(head, foot, rabbit, chicken);
System.out.printf("鸡有:%d只,兔子有:%d只\n",chicken[0],rabbit[0]);
}
}