[NYOJ] [acm-143] Chickens and Rabbits in the Same Cage

Problem Description

Loading...

Problem Analysis

Write out the conditions under which the equation has no solution
then for all remaining cases you can write the result

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include<stdio.h>
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int n,m;
		scanf("%d %d",&n,&m);
		if(m<2*n||4*n<m||(m-2*n)%2||(4*n-m)%2)
			printf("No answer\n");
		else
			printf("%d %d\n",(4*n-m)/2,(m-2*n)/2);
	}
	return 0;
}

The reference solution is written very simply
It seems that’s because my equation wasn’t simplified well
I learned something…

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#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;
	}
}

Problem link:Problem Details - Chickens and Rabbits in the Same Cage - NYOJ

Loading...