Java programming problem--chicken and rabbit cage

Source: Internet
Author: User
The total number of chickens and rabbits is known to be n and the total number of legs is m. Enter N and M, and in turn output the number of chickens and the number of rabbits. If there is no solution, the output is no answer. Sample input: 14 32 Sample output: 12 2 Sample input: 10 16 Sample output: No answer

  

Package Solve;import Java.util.scanner;public class solve{static Scanner scan = new Scanner (system.in);p ublic 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); Elsesystem . OUT.PRINTLN ("No answer");} ElseSystem.out.println ("No answer");}}

 

Java programming problem--chicken and rabbit cage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.