import java.util.*;
import java.io.*;
import java.math.*;
public class CirclesSeparation {
int N, now;
double ox[] = new double[1000], oy[] = new double[1000];
double x[] = new double[1000], y[] = new double[1000];
double r[] = new double[1000], m[] = new double[1000];
boolean touch(int i,int j)
{
double dis = (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]);
if (dis > (r[i]+r[j]) * (r[i]+r[j])) {
return false;
}
return true;
}
void dfsMove(int ok, int j)
{
double px = x[j] - x[ok];
double py = y[j] - y[ok];
double dis = Math.sqrt((x[j]-x[ok])*(x[j]-x[ok]) + (y[j]-y[ok])*(y[j]-y[ok]));
double dd = r[ok] + r[j] - dis + 0.001;
x[j] += dd * px / dis;
y[j] += dd * py / dis;
//System.out.println(x[j] + "\t" + y[j]);
int i;
for (i=0;i<=now;i++) {
if (i != j && touch(i, j)) {
dfsMove(j, i);
}
}
}
public double[] minimumWork(double[] ix, double[] iy, double[] ir, double[] im) {
int i,j,k,l;
N = ix.length;
for (i=0;i<N;i++) {
ox[i] = ix[i];
oy[i] = iy[i];
x[i] = ix[i];
y[i] = iy[i];
r[i] = ir[i];
m[i] = im[i];
}
for (i=1;i<N;i++)
{
now = i;
for (j=0;j<i;j++) {
if (!touch(i, j)) continue;
dfsMove(i, j);
}
}
double ret[] = new double[N+N];
for (i=0;i<N;i++) {
ret[i+i] = x[i];
ret[i+i+1] = y[i];
}
return ret;
}
}
按照格式写,然后返回结果就可以。这是最基本的。
其实我们可以用他提供的工具先做调试
一般每题会有available.这样一个链接,
进去后
1、先下载页面顶上 CirclesSeparationVis.jar 和 一些其他的东西(如果有)
2、在这行In other words, you should implement the following pseudocode in the main method of your solution:的后面会给出一些输入输出步骤,把他们翻译成对应语言的输入输出,并且把他们写在主函数中,像这题的:
12345678910111213
N = parseInt(readLine())
for (i=0; i < N; i++)
x[i] = parseDouble(readLine())
for (i=0; i < N; i++)
y[i] = parseDouble(readLine())
for (i=0; i < N; i++)
r[i] = parseDouble(readLine())
for (i=0; i < N; i++)
m[i] = parseDouble(readLine())
ret = minimumWork(x, y, r, m)
for (i=0; i < 2*N; i++)
printLine(ret[i])
flush(stdout)
翻译成java的是:
12345678910111213141516171819
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
double x[], y[], r[], m[], ret[];
int N, i;
N = cin.nextInt();
x = new double[N];
y = new double[N];
r = new double[N];
m = new double[N];
for (i=0;i<N;i++) x[i] = cin.nextDouble();
for (i=0;i<N;i++) y[i] = cin.nextDouble();
for (i=0;i<N;i++) r[i] = cin.nextDouble();
for (i=0;i<N;i++) m[i] = cin.nextDouble();
CirclesSeparation rrr = new CirclesSeparation();
ret = rrr.minimumWork(x, y, r, m);
for (i=0;i<N+N;i++) {
System.out.println(ret[i]);
}
}