这是问题:
给定一个声明为double centerX = 0,centerY = 0的圆,取半径和一个坐标为x,y的点。编写一个程序,计算该点是否位于圆内(包括边界)。
公式:
两点A(x1,y1)和B(x2,y2)之间的距离为:
((x2-x1)^ 2 +(y2-y1)^ 2)的平方根
输入规格:
第一行将包含半径r。
第二行包含x和y。每个值都是(x,y)坐标,使得x,y> 0。
输出规格:
如果点位于圆内,则打印“点(x,y)在圆内”,否则打印“点(x,y)在圆外”。
样本输入1:
7
2 5
样本输出1:
点(2,5)在圆内
样本输入2:
2个
9 4
样本输出2:
点(9,4)在圆的外面
解释:
在样本输入1中,(0,0)和(2,5)之间的距离为5.3851,小于半径7。因此,该点位于圆内。而在另一组输入中,该点位于圆的外部,因为原点和(9,4)之间的距离为9.8488,该距离大于给定的半径2。
现在这是我的代码:
import java.util.Scanner;
import java.math.*;
class Circ{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int r=s.nextInt();
int x=s.nextInt();
int y=s.nextInt();
if(x>0&&y>0){
double dist=Math.sqrt(Math.pow(x-0,2)+Math.pow(y-0,2));
if(dist>r){
System.out.println("The point ("+x+","+y+") is outside the circle");
}else if(dist<=r){
System.out.println("The point ("+x+","+y+") is inside the circle");
}
}
}
}
它给出正确的输出,但没有通过测试用例吗?
import java.util.Scanner;
public class circleOfCodeG{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int r,x,y;
double dis;
r=sc.nextInt();
x=sc.nextInt(); y=sc.nextInt();
dis=Math.sqrt((x*x)+(y*y));
if(dis<=r)
System.out.println("The point ("+x+", "+y+") is inside the circle");
else
System.out.println("The point ("+x+", "+y+") is outside the circle");
}
}
试试这个。我敢肯定,这将满足“测试用例”。:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句