首页 > 代码库 > java编程题 --鸡兔同笼

java编程题 --鸡兔同笼

已知鸡和兔的总数量为n,总腿数为m。输入n和m,依次输出鸡的数目和兔的数目。如
果无解,则输出No answer。
样例输入:
14 32
样例输出:
12 2
样例输入:
10 16
样例输出:
No answer

  

package Solve;

import java.util.Scanner;

public class Solve
{
	static Scanner scan = new Scanner(System.in);
	
	public static void main(String[] args)
	{
		int n = scan.nextInt(),m = scan.nextInt();
		int chicken = (4 * n - m) / 2;
		int tu =  n - chicken;
		if ( tu * 4 + chicken * 2 == m) 
		{
			if ( chicken > -1 && tu > -1) 
				System.out.println(chicken + " " + tu);
			else
				System.out.println("No answer");
		}
		else
			System.out.println("No answer");
	}
	

}

 

java编程题 --鸡兔同笼