鸡兔同笼

鸡兔同笼

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 1
描述
已知鸡和兔的总数量为n,总腿数为m。输入n和m,依次输出鸡和兔的数目,如果无解,则输出“No answer”(不要引号)。
输入
第一行输入一个数据a,代表接下来共有几组数据,在接下来的(a<10)
a行里,每行都有一个n和m.(0<m,n<100)
输出
输出鸡兔的个数,或者No answer
样例输入
2
14 32
10 16
样例输出
12 2
No answer
来源
[张洁烽]原创
上传者

张洁烽


问题分析:

假设x为鸡的个数,y为兔的个数,

问题所包含的条件:

1.x+y=n;

2.2x+4y=m;

3.x>=0  y>=0

4.m为偶数


代码:

#include <iostream>
#include <stdio.h> 
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <algorithm>
using namespace std; 

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	int a;
	scanf("%d",&a);
	while(a--){
		int n,m;
		scanf("%d%d",&n,&m); 
		int x=(4*n-m)/2;
		if(m%2 == 0 && x>=0 && x<=n){
			printf("%d %d\n",x,n-x);
		}else{
			printf("No answer\n");
		}
		
	}  
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值