时间限制: 1000 ms 内存限制: 65536 KB
提交数: 72428 通过数: 38273
【题目描述】
数学中经典的“鸡兔同笼”问题,已知头共x个,脚共y只,问笼中的鸡和兔各有多少只?
【输入】
头和脚的数量。
【输出】
鸡和兔各自数量。一个空格隔开。
【输入样例】
30 90
【输出样例】
15 15
方法一(假设鸡)(也称抬脚法):
源代码:
#include<iostream>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
int sum=(m-n*2)/2,nam=n-sum;//利用公式
cout<<nam<<" "<<sum;
}
方法二(假设兔):
源代码:
#include<iostream>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
int sum=(n*4-m)/2,nam=n-sum;//利用公式
cout<<sum<<" "<<nam;
return 0;
}
方法三(遍历):
遍历就是把所有方法列举一遍:
源代码如下:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
for(int j=1;j<=n;j++){
for(int t=1;t<=n;t++){
if(j+t==n&&j*2+t*4==m){
cout<<j<<" "<<t;
}
}
}
}