一个笼子里面关了鸡和兔子(鸡有2只脚,兔子有4只脚,没有例外)。已经知道了笼子里面脚的总数a,问笼子里面至少有多少只动物,至多有多少只动物。
输入
一行,一个正整数a (a < 32768)。
输出
一行,包含两个正整数,第一个是最少的动物数,第二个是最多的动物数,两个正整数用一个空格分开。
如果没有满足要求的答案,则输出两个0,中间用一个空格分开。
样例输入
20
样例输出
5 10
方法一:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
if(n%2!=0) cout<<"0 0";
else cout << n/4+n%4/2 << " " << n/2;
return 0;
}
方法二:
#include <iostream>
using namespace std;
int main(){
int a;
cin >> a;
//如果a是奇数 直接输出0 0
if(a%2!=0) cout << 0 << " " << 0 << endl;
//如果a是偶数
else{
//如果a是偶数,且a能被4整除,最少n/4兔子 最多n/2鸡,
if(a%4==0) cout << a/4 << " " << a/2 << endl;
//如果a是偶数,但不能被4整除,最少1只鸡剩余全是兔子 最多全是鸡n/2
else cout << 1+a/4 << " " << a/2 << endl;
}
return 0;
}
方法三: 枚举所有情况
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,sum=0,flag=0;
int min=32768,max=-1;
cin>>a;
for(int i=0;i<=a/4;i++){//兔
for(int j=0;j<=a/2;j++){//鸡
if(i*4+j*2==a){
flag=1;//标记1,表示存在
sum=i+j;
if(sum<min) min=sum;
if(sum>max) max=sum;
}
}
}
if(flag==0)
cout<<0<<" "<<0<<endl;
else
cout<<min<<" "<<max<<endl;
return 0;
}