Chicken and rabbit cage
Description
The total number of chickens and rabbits is n and the total number of legs is M. Input N and M, and output the numbers of chickens and rabbits in sequence. If there is no solution, output "No answer" (do not quote the quotation marks ).
-
Input
-
Input a data in the first row indicates that there are several groups of data in the next row. In the next row (A <10), each row contains N and M. (0 <m, n <100)
-
Output
-
Output The number of rabbits or no answer
-
Sample Input
-
214 3210 16
-
Sample output
-
12 2No answer
#include<iostream> using namespace std; int main() { int n; int a=0,b=0; int shu,leg; bool you =false; cin>>n; while(n--){ you = false; cin>>shu>>leg; for( a=0;a<100;a++){ for(b=0;b<100;b++){ if( (2 *a + 4 *b == leg) && (a +b == shu)) { cout<<a<<" "<<b<<endl; you=true; } } } if (you==false) cout<<"No answer"<<endl; } return 0; }
Chicken and rabbit cage