首页 > 代码库 > NYOJ题目64鸡兔同笼

NYOJ题目64鸡兔同笼

技术分享

--------------------------

 

经典水题,我第一次做这种题的时候完全懵逼用蛮力碰....

然而现在我终于学会列方程了....

 

技术分享

 

解此方程:

技术分享

 

排除掉小数和负数,得到的就是解。

 

AC代码:

 1 import java.util.Scanner; 2  3 public class Main { 4  5     public static void main(String[] args) { 6          7         Scanner sc=new Scanner(System.in); 8          9         int times=sc.nextInt();10         while(times-->0){11             int n=sc.nextInt();12             int m=sc.nextInt();13             if(m-2*n<0 || (m-2*n)%2!=0){14                 System.out.println("No answer");15             }else{16                 int y=(m-2*n)/2;17                 int x=n-(int)y;18                 System.out.println(x+" "+(int)y);19             }20         }21         22     }23     24 }

 

题目来源: http://acm.nyist.net/JudgeOnline/problem.php?pid=64

NYOJ题目64鸡兔同笼