鸡兔同笼
时间限制:
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; }
-
第一行输入一个数据a,代表接下来共有几组数据,在接下来的(a<10)