1621 - 鸡兔同笼

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB

一个笼子里面关了鸡和兔子(鸡有2只脚,兔子有4只脚,没有例外)。已经知道了笼子里面脚的总数a,问笼子里面至少有多少只动物,至多有多少只动物。

题目输入

第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,每行一个正整数a (a < 32768)

题目输出

输出包含n行,每行对应一个输入,包含两个正整数,第一个是最少的动物数,第二个是最多的动物数,两个正整数用一个空格分开

如果没有满足要求的答案,则输出两个0。

输入/输出样例

输入格式

2
18
5

输出格式

5 9
0 0

C语言解答

#include <stdio.h>

int main()
{
    int nCases, i, nFeet;
    scanf("%d", &nCases);
    for (i = 0; i < nCases; i ++)
    {
        scanf("%d", &nFeet);
        if (nFeet % 2 != 0)
            printf("0 0\n");
        else if (nFeet % 4 != 0)
            printf("%d %d\n", nFeet / 4 + 1, nFeet / 2);
        else printf("%d %d\n", nFeet / 4, nFeet / 2);
    }
    return 0;
}

C++解答

#include <stdio.h>  
int main( )  
{  
int nCases, i, nFeet; //nCases 表示输入测试数据的组数,nFeet 表示输入的脚数。  
scanf("%d", &nCases);  
for(i = 0; i < nCases; i++){  
scanf("%d", &nFeet);  
if(nFeet %2 != 0) // 如果有奇数只脚,则输入不正确,  
// 因为不论2 只还是4 只,都是偶数  
printf("0 0\n");  
else if (nFeet%4 != 0) //若要动物数目最少,使动物尽量有4 只脚  
//若要动物数目最多,使动物尽量有2 只脚  
printf("%d %d\n", nFeet / 4 + 1, nFeet / 2);  
else printf("%d %d\n", nFeet / 4, nFeet / 2);  
}  
}  

Java解答

import java.io.*;
import java.util.*;
public class Main
{
  public static void main(String g[])throws Exception
  {
    int t,n;
    Scanner cin=new Scanner(System.in);
    t=cin.nextInt();
    for(int i=0;i<t;i++)
    {
      n=cin.nextInt();
      if(n%2==0)
        System.out.println((n/4+n%4/2)+" "+(n/2+n%2/4));
      else System.out.println("0 0");
    }
  }
}