18-language Introduction -18-chicken rabbit with Cage

Source: Internet
Author: User

Title Address: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=64? Description
The total number of chickens and rabbits is known to beN,the total number of legs ism. InputNand them,output the number of chickens and rabbits in turn, and if there is no solution, the output"No Answer" (No quotes).

input
The first line enters a dataA,The next few sets of data are represented in the next(A<10)
Aline, each row has aNand theM. (0<m,n<100)
Output
output the number of chicken rabbits, orNo Answer
Sample Input
2
14 32
10 16
Sample Output
12 2
No Answer?? Code: #include <stdio.h>

Output results
N-Number and, M-leg number and
static void Printresult (int n,int m);

int main ()
{
???? int readlen = 0;
???? scanf ("%d", &readlen);
???? GetChar ();
????
???? while (Readlen > 0)
???? {
????????? int n = 0;
????????? int m = 0;
?????????
????????? scanf ("%d%d", &n,&m);
????????? GetChar ();
?????????
????????? Printresult (N,M);
?????????
????????? --readlen;
???? }
????
???? return 0;
}

Output results
N-Number and, M-leg number and
static void Printresult (int n,int m) {??? if (M<n)??? {????? printf ("No answer");????????? return;???}????? int T Mpvalue = M-2*n;
???? if (Tmpvalue < 0 | | tmpvalue% 2! = 0)
???? {
????????? printf ("No answer");
????????? Return
???? }
????
???? int rabbitcount = TMPVALUE/2;???? int chickencount = N-rabbitcount;?? ? if (Chickencount < 0)?? {?????. printf ("No answer");????????? return;??}????
???? printf ("%d%d\n", chickencount,rabbitcount);
}?? Judging by the number of equations the rabbit: (m-2n)/2 chicken: N-Rabbit number? In addition, the code of his own writing is too cumbersome to judge the conditions, the recommended excellent program is as follows:? #include <iostream>using namespace Std;int Main () {int N,a,b,p,q;cin>>n;while (n--) {cin>>a>>b;q= (b-2*a)/2;p=a-q;if (p<0 | | q<0 | | b%2) cout<< "No answer" <<endl;else cout<<p<< "" <<q<<endl;}?

18-language Introduction -18-chicken rabbit with 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.