这些是我的代码。我练习关于小王子的算法'。算法的内容是,如果您输入起点和终点,您应该找到穿越行星范围的数量。我在下面提交了我的代码以获得分数。但我收到了运行时错误'信息。我无法找到解决方案。请告知我的代码。我认为关于算法的答案是正确的。
import java.util.LinkedList;
import java.util.Scanner;
class Planet
{
int cx;
int cy;
int r;
}
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int testCase=Integer.parseInt(scanner.nextLine());//input test case
int answer[]=new int[testCase];
String str[]=scanner.nextLine().split(" ");//input start and dest point
int x1=Integer.parseInt(str[0]);
int y1=Integer.parseInt(str[1]);
int x2=Integer.parseInt(str[2]);
int y2=Integer.parseInt(str[3]);
if(x1!=y1&&x2!=y2)
{
for(int i=0;i<testCase;i++)
{
int count=0;
int planetNum=Integer.parseInt(scanner.nextLine());//input planet counts
LinkedList<Planet> planetList=new LinkedList<>();
for(int j=0;j<planetNum;j++)
{
String inputPlanet[]=scanner.nextLine().split(" ");//each planet's (x,y) and radius
Planet planet=new Planet();
planet.cx=Integer.parseInt(inputPlanet[0]);
planet.cy=Integer.parseInt(inputPlanet[1]);
planet.r=Integer.parseInt(inputPlanet[2]);
planetList.add(planet);
}
for(int j=0;j<planetList.size();j++)
{
double startDistance=Math.sqrt((planetList.get(j).cx-x1)*(planetList.get(j).cx-x1)
+(planetList.get(j).cy-y1)*(planetList.get(j).cy-y1));
double destDistance=Math.sqrt((planetList.get(j).cx-x2)*(planetList.get(j).cx-x2)
+(planetList.get(j).cy-y2)*(planetList.get(j).cy-y2));
if(startDistance<planetList.get(j).r||destDistance<planetList.get(j).r)
count++;
}
answer[i]=count;
}
}
else
{
for(int i=0;i<testCase;i++)
answer[i]=0;
}
for(int i=0;i<testCase;i++)
System.out.println(answer[i]);
}
}
输入
2
-5 1 12 1
7
1 1 8
-3 -1 1
2 2 2
5 5 1
-4 5 1
12 1 1
12 1 2
1
0 0 2
输出
3
0
答案 0 :(得分:1)
你的问题在哪里?我复制了你的代码。将scanner.nextLine()更改为inputArray [inputIndex ++]并使用显示的输入填充它(inputArray)。我收到零例外。没有例外 - 它浪费我们的时间。我们仍然想要帮助。 使用stacktrace向我们展示您的例外情况。
class Planet // I put everything under the Planet class.
{
int cx;
int cy;
int r;
// These two simulate user input
static int inputIndex = 0;
static String[] inputArray = {
"2",
"-5 1 12 1",
"7",
"1 1 8",
"-3 -1 1",
"2 2 2",
"5 5 1",
"-4 5 1",
"12 1 1",
"12 1 2",
"1",
"0 0 2"};
public static void main(String[] args) {
int testCase=Integer.parseInt(inputArray[inputIndex++]);//input test case
int answer[]=new int[testCase];
String str[]=inputArray[inputIndex++].split(" ");//input start and dest point
int x1=Integer.parseInt(str[0]);
int y1=Integer.parseInt(str[1]);
int x2=Integer.parseInt(str[2]);
int y2=Integer.parseInt(str[3]);
if(x1!=y1&&x2!=y2)
{
for(int i=0;i<testCase;i++)
{
int count=0;
int planetNum=Integer.parseInt(inputArray[inputIndex++]);//input planet counts
LinkedList<Planet> planetList=new LinkedList<>();
for(int j=0;j<planetNum;j++)
{
String inputPlanet[]=inputArray[inputIndex++].split(" ");//each planet's (x,y) and radius
Planet planet=new Planet();
planet.cx=Integer.parseInt(inputPlanet[0]);
planet.cy=Integer.parseInt(inputPlanet[1]);
planet.r=Integer.parseInt(inputPlanet[2]);
planetList.add(planet);
}
for(int j=0;j<planetList.size();j++)
{
double startDistance=Math.sqrt((planetList.get(j).cx-x1)*(planetList.get(j).cx-x1)
+(planetList.get(j).cy-y1)*(planetList.get(j).cy-y1));
double destDistance=Math.sqrt((planetList.get(j).cx-x2)*(planetList.get(j).cx-x2)
+(planetList.get(j).cy-y2)*(planetList.get(j).cy-y2));
if(startDistance<planetList.get(j).r||destDistance<planetList.get(j).r)
count++;
}
answer[i]=count;
}
}
else
{
for(int i=0;i<testCase;i++)
answer[i]=0;
}
for(int i=0;i<testCase;i++)
System.out.println(answer[i]);
}
}