[ACM] chickens and rabbits with cage

Time Limit: 3 Sec Memory Limit: 64 MB
Submit: 987 Solved: 251

Title Description

The total number of chickens and rabbits known as n, the total number of legs is m. N inputs and m, the number of chicken and rabbit are sequentially output, if no solution, then the output "No answer" (without the quotes).

Entry

A first line of input data a, several sets of data representative of a total of the next, in the following (a <10) a row, every row m and n have a

Export

Output number of chickens and rabbits, or No answer

Sample input

2
14 32
10 16

Sample Output

12 2
No answer
#include<stdio.h>
#include<iostream>
using namespace std;
int main(){
	int  r,c;
    int a,i,s;
    scanf("%d",&a);
    for(i=1; i<=a; i++){
        int n,m;
        scanf("%d%d",&n,&m);
        for(r=0; r<=n; r++){
            c=n-r;
            if((r*4+c*2==m)&&(r+c==n)){
				printf("%d %d\n",c,r);
                break;
			}
        }
		if(r>n)
            printf("No answer\n");
    }
	return 0;
}
Published 46 original articles · won praise 39 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_42128813/article/details/103773932