"Programming Guidance and online Practice" example 2.1 chicken and rabbit cage problem

Source: Internet
Author: User

Problem Description:

A cage was closed with chickens and rabbits. Already know the total number of feet in the cage, ask at least how many animals in the cage, how many animals?

Input data

The first line is the number of data groups tested N, followed by n-line input, Meizu test data for a row, including a positive integer a (a < 32768)

Output requirements

n rows, each line corresponds to an input, the output is two positive integers, the first is the fewest animal books, the second is the largest number of animals, two positive integers are separated by a space

Output two 0 if no requirement is met

Input sample

2

3

20

Output sample

0 0

5 10

With the current ' IQ ', I'm afraid there will be a violent solution! The fewest animals must be rabbits, starting from zero, plus 4 until the difference between the two (total feet) is less than 4 if the difference is 2 then add an animal if it's 0, it's over.

If it's odd, then you have to output 0 0.

But this is obviously a lot of trouble.

This is how the book analyzes the problem:

This problem can be described as either giving a positive integer n, if n is odd, output 0 0, otherwise if n is a multiple of 4, the output N/4 N/2, if n is not a multiple of 4, the output n/4+1 N/2. This is a general calculation problem,

So long as the corresponding judgment and output code can be achieved. The topic shows that the input integers are within a smaller range, so you just need to consider integer arithmetic.

The code is naturally simple--

IQ is naturally catching the rush--

The solution in the book is obviously more ' bigger picture '

//http://www.cnblogs.com/xdblog/p/5451306.html#include <iostream>#include<stdio.h>intMain () {intNnumber; intNfeet; scanf ("%d", &nnumber);  for(inti =0; i < Nnumber; i++) {scanf ("%d", &Nfeet); if(nfeet%2!=0)//If an odd output occurs 0 0{printf ("0 0\n"); }Else if(nfeet%4==0){//if it can be divisible by 4 .printf"%d%d\n", nfeet/4, nfeet/2); }Else{//cannot be divisible by 4 to indicate two more feetprintf"%d%d\n", nfeet/4+1, nfeet/2); }    }    return 0;}

"Programming Guidance and online Practice" example 2.1 chicken and rabbit cage problem

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.