首页 > 试题广场 >

鸡兔同笼

[编程题]鸡兔同笼
  • 热度指数:13405 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
一个笼子里面关了鸡和兔子(鸡有2只脚,兔子有4只脚,没有例外)。已经知道了笼子里面脚的总数a,问笼子里面至少有多少只动物,至多有多少只动物。

输入描述:
每组测试数据占1行,每行一个正整数a (a < 32768)


输出描述:
输出包含n行,每行对应一个输入,包含两个正整数,第一个是最少的动物数,第二个是最多的动物数,两个正整数用一个空格分开
如果没有满足要求的答案,则输出两个0。
示例1

输入

2
3
20

输出

1 1
0 0
5 10
#include<iostream>
#define maxn 100
using namespace std;
void CalNum(int m);
int result[2];
int k=0;
int main()
{
 int n,m,v;
 cin>>n;
 v=n;
 while(n>0)
 {
  cin>>m;
  CalNum(m);
  cout<<result[0]<<" "<<result[1]<<endl;
  n--;
 }
 /*
 for(int i=0;i<v;i++)
 {
  for(int j=0;j<2;j++)
  {
   cout<<result[i][j]<<" ";
  }
  cout<<endl;
 }
 cout<<endl;
 */
}
void CalNum(int m)
{
 if(m%2==0)
 {
  result[0] = m/4 + (m%4==0?0:1);
  result[1] = m/2;
 }
 else
 {
  result[0]=result[1]=0;
 }
 //k++;
} 

发表于 2017-08-31 14:07:22 回复(0)
更多回答

小学的题目。。。

#include<iostream>
using namespace std;

int main(){
    int n;
    while(cin >> n){
        if(n % 2 == 1)
            cout << "0 0" << endl;
        else{
            if(n % 4 == 0)
                cout << n / 4 << ' ';
            else
                cout << n / 4 + 1 << ' ';
            cout << n / 2 << endl;
        }
    }
    return 0;
}
发表于 2019-03-22 19:28:22 回复(0)
这题坑就坑在n是不用输入的,但是例子里还是给了输入个数n......
#include<iostream>
#
include<cstdio>

using namespace std;

int main(){
    int a;
    while(scanf("%d",&a) !=EOF){
        int minnum = 0;
        int maxnum = 0;
        if(a%2 == 0){
            minnum = a/4 + (a%4)/2;
            maxnum = a/2;
        }
        cout<<minnum<<" "<<maxnum;
        cout<<endl;
    }
    return 0;
}

发表于 2020-03-01 12:38:51 回复(2)
#include<bits/stdc++.h>
int main(){
    int a;
    while(scanf("%d",&a)!=EOF)
        if(a&1) printf("0 0\n");
        else printf("%d %d\n",a/4+(a%4)/2,a/2);
}
发表于 2019-03-12 23:09:10 回复(0)
#include <iostream>
#include <cstdio>
#include <math.h>
using namespace std;
const int N = 100000;
int prime[N] = { 0 };

int MOST(int n) {
	int sum = n / 2;
	return sum;
}
int Less(int n) {
	int sum=0;
	if (n % 4 == 0) {
		sum = n / 4;
	}
	else
	{
		sum = n / 4 + 1;
	}
	return sum;
}
int main() {
	int a;
	while (cin>>a)
	{
		if (a % 2 != 0) {
			cout << "0 0";
			break;
		}
		else
		{
			cout<<Less(a)<<" ";
			cout<<MOST(a);
		}
	}
}

发表于 2022-02-19 20:40:11 回复(0)
#include<cstdio>
#include<iostream>

using namespace std;

int main(){
    int a;
   
    while(scanf("%d",&a)!=EOF){
        int max,min;
        max=min=0;
        if(a%2==0){
            max=a/2;
            min=a/4+(a%4)/2;
        }
        cout<<min<<' '<<max<<endl;
  
    }
    return 0;
}
#include<cstdio>
#include<iostream>

using namespace std;

int main(){
    int a;
    while(scanf("%d",&a)!=EOF){
        int max,min;
        max=min=0;
        if(a%2==0){
            max=a/2;
            min=a/4+(a%4)/2;
        }
        cout<<min<<' '<<max<<endl;
    }
    return 0;
}

发表于 2021-03-12 19:22:15 回复(0)
#include<stdio.h>
int main()
{
    int sum;
    scanf("%d",&sum);
    if(sum==2)//最多最少都是一只鸡
    {
        printf("1 1");
        return 0;
    }
    if(sum%2!=0)
        printf("0 0\n");
    else//是2的倍数
    {
        if(sum%4==0)//是4的倍数
            printf("%d %d",sum/4,sum/2);
        else//仅仅是2的倍数,不是4的倍数
            printf("%d %d",sum/4+1,sum/2);//加一是因为不满足都是兔子但是剩下了两条腿所以大多数是兔子加一只鸡
    }
    return 0;
}

发表于 2020-04-06 17:05:15 回复(1)
Java 解法
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int a = scanner.nextInt();
            if (a%2!=0||a<=1)System.out.println("0 0");
            else {
                int min = a/4+(a%4)/2;
                int max = a/2;
                System.out.println(min+" "+max);
            }
        }
    }
}


发表于 2020-03-14 13:36:36 回复(0)
这题感觉是数学题hhh,如果不是2的倍数,即不可能,如果是2的倍数,则最少为尽量选4个腿的,所以如果为4的倍数则为a/4,如果不为4的倍数则为a/4+1,最多即为所有都选2只腿
#include<iostream>
using namespace std;
int main(){
    int a[6];
    for(int i=0;i<6;i++)
        cin>>a[i];
    int total=0;
    for(int i=1;i<6;i++){
        if(a[i]<a[0])
            total+=a[i];
    }
    cout<<total<<endl;
}

发表于 2019-02-09 17:20:34 回复(1)
while True:
    try:
        num = int(input())
        temp1,temp2 = divmod(num,2)
        temp3,temp4 = divmod(num,4)
        if temp2 == 1:
            print('0 0')
        else:
            print("%d %d" % (temp3+temp4//2,temp1))
    except Exception:
        break
编辑于 2018-09-28 17:28:24 回复(0)
#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int min = 0;
        int max = 0;
        int j, t;
        int a;
        cin >> a;
        // 求 最少
        t = a/4;
        j = (a - t*4)/2;
        if(a == t*4+j*2)
        {
            min = j+t;
        }
        // 求 最多
        j = a/2;
        t = (a - j*2)/4;
        if(a == j*2+t*4)
        {
            max = j+t;
        }
        cout << min << " " << max << endl;
    }

    return 0;
}


发表于 2016-08-13 10:37:35 回复(0)
#include <stdio.h>

int main()
{
    int a;
    int least, most;
    while(scanf("%d", &a)!=EOF)
    {
        if(a%2==1)
        {
            printf("0 0\n");
            continue;
        }
        a=a/2;//总腿数减半,同时假设鸡1条腿,兔2条腿
        most=a;//全是鸡
        if(a%2==0) least=a/2;//全是兔
        else least=a/2+1;//有一只鸡,剩下全是兔
        printf("%d %d\n", least, most);
    }
    return 0;
}

发表于 2018-02-22 19:26:12 回复(1)
#include <cstdio>
#include <stdlib.h>
#include <iostream>

using namespace std;

int mini(int n);
int maxs(int n);

//Chicken rabbit cage
int main() {
	int n;
	while (scanf("%d", &n) != EOF) {
		int minn = 0;
		int maxx = 0;
		
		//只有偶数才有解 
		if (n % 2 == 0) {
			//求最小动物数量 
			minn = n / 4 + (n % 4) / 2;
			//求最大动物数量 
			maxx = n / 2;
		}
		
		printf("%d %d\n", minn, maxx);
	}
	return 0;
}

发表于 2023-03-24 14:46:18 回复(0)
#include<bits/stdc++.h>
using namespace std;

int main(){
    int n;
    while(cin>>n){
        if(n%2==1){
            cout<<0<<" "<<0<<endl;
        }
        else{
            if(n%4==0){
                cout<<n/4<<" ";
            }
            else{
                cout<<n/4+1<<" ";
            }
            cout<<n/2<<endl;
        }
    }
    return 0;
}
发表于 2022-10-09 13:48:52 回复(2)
#include <iostream>
using namespace std;

int main() {
    int n,minn,maxn;
    while(cin >> n){
        minn = 0,maxn=0;
        if(n%2!=0){
            cout << 0 << " " << 0<<endl;
        }else{
            maxn = n/2;
            int temp = n/4;
            int remain = n%4;
            minn=temp+remain/2;
            cout << minn<<" "<<maxn<<endl;

        }
    }
    return 0;
}

发表于 2024-03-26 21:05:19 回复(0)
真的只要一行哦
#include <iostream>
using namespace std;
int main() {
    int a;
    while(cin>>a){
        a%2==0?cout<<a/4+a%4/2<<" "<<a/2<<endl:cout<<"0 0"<<endl;
    }
    return 0;
}

发表于 2024-03-26 13:36:37 回复(0)
def chick(a):
    flag = 0
    arr = []
    for i in range(a):
        for j in range(a):
            if 2 * i + j * 4 == a:
                flag = 1
                k = i + j
                arr.append(k)
                #print("%d %d"%(i, j))
    
    #print("%d %d"%(min(arr), max(arr)))
    if flag == 0:
        print("%d %d"%(0, 0))
    else:
        print("%d %d"%(min(arr), max(arr)))

while True:
    try:
        a = int(input())
        chick(a)
    except:
        break
  

编辑于 2024-03-23 10:02:05 回复(0)
#include <iostream>
using namespace std;

int main() {
    int foot;
    cin>>foot;
    if((foot%2)!=0){
        cout<<'0'<<' '<<'0'<<endl;
    }
    else if(foot%2==0&&foot%4!=0){
        cout<<((foot/2)-1)/2+1<<' '<<foot/2<<endl;
    }
    else cout<<foot/4<<' '<<foot/2<<endl;
}

发表于 2024-03-19 16:31:48 回复(0)
#include <iostream>
using namespace std;

int main() {
    int a;
    while (cin >> a) { // 注意 while 处理多个 case
        if (a % 2 != 0) cout << "0 0" << endl;
        else if (a % 4 == 0) cout << a / 4 << ' ' << a / 2 << endl;
        else cout << a / 4 + 1 << ' ' << a / 2 << endl;
    }
}

发表于 2024-03-19 16:30:22 回复(0)
public static void main(String[] args) {
        // TODO 自动生成的方法存根
        //鸡兔同笼
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int num = in.nextInt();
            if (num % 2 == 0) {
                //min
                //最少的情况就是最大程度是兔子,先/4,得到最大的兔子数,如果/4有余数,那就是剩下的鸡(不出意外是一只)
                //最多的情况就是全是鸡,有最多的动物数
                int a = num / 4;
                int b = num % 4;
                int min = a+b/2;
                int max = num / 2;
                System.out.println(min+" "+max);
            }else {
                //不会出现单数脚……
                System.out.println(0+" "+0);
            }
        }
    }
编辑于 2024-03-19 11:36:29 回复(0)
#include <iostream>
using namespace std;

int main() {
    int a;
    while (cin >> a) {
        if (a % 2 == 0) {   //脚数为偶数才有解
            cout << a / 4 + (a % 4) / 2 //最少的动物数(兔子尽可能多)
                 << " "
                 << a / 2   //最多的动物数(全是鸡)
                 << endl;
        } else {            //脚数为奇数
            cout << "0 0" << endl;  //硬编码输出0
        }
    }
    return 0;
}

编辑于 2024-02-11 11:18:38 回复(0)

问题信息

难度:
97条回答 9780浏览

热门推荐

通过挑战的用户

查看代码