问题是求一条南北走向的直线,到各点的距离和最短程序不对,不知道为什么?!package optimalway;
import java.util.*;
//主函数
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
Position[] pos=new Position[n];
for(int i=0;i<n;i++)
pos[i]=new Position();
for(int i=0;i<pos.length;i++)
System.out.println("油井的坐标分别为:("+pos[i].getX()+","+pos[i].getY()+") ");
int i,j;
for(i=1;i<n;i++)
for(j=n-1;j>=i;j--)
if(pos[j].getY()>pos[j-1].getX())
{
Position temp=pos[j];
pos[j]=pos[j-1];
pos[j-1]=temp;
}
if(n%2==1)
System.out.println(pos[n/2]);
System.out.println("最优的管道位置是Y:"+pos[n/2]+"到"+pos[n/2+1]);
}
}
class Position{
public Position(){
Random rand = new Random();
x=rand.nextInt();
y=rand.nextInt();
}
public int getX(){
return x;
}
public int getY(){
return y;
}
private int x;
private int y;
}
package optimalway;
import java.util.*;
//主函数
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n=in.nextInt();
Position[] pos=new Position[n];
for(int i=0;i<n;i++)
pos[i]=new Position();
for(int i=0;i<pos.length;i++)
System.out.println("油井的坐标分别为:("+pos[i].getX()+","+pos[i].getY()+") ");
int i,j;
for(i=1;i<n;i++)
for(j=n-1;j>=i;j--)
if(pos[j].getY()>pos[j-1].getX())
{
Position temp=pos[j];
pos[j]=pos[j-1];
pos[j-1]=temp;
}
if(n%2==1)
System.out.println(pos[n/2]);
System.out.println("最优的管道位置是Y:"+pos[n/2]+"到"+pos[n/2+1]);
}
}
//油井的位置坐标类
class Position{
public Position(){
Random rand = new Random();
x=rand.nextInt();
y=rand.nextInt();
}
public int getX(){
return x;
}
public int getY(){
return y;
}
private int x;
private int y;
}
解决方案 »
- 很奇怪的问题,关于图片的,为什么用jfram的画图可以显示出来,但是为什么我生成到我的磁盘,大小确实0KB,我读的是一个dicom图片.大家分析我的代码.
- 在用java写代码时, 主函数前为什么要加static
- 使用JDIC中的IWebBrowser的问题
- jedit A BeanShell error occurred 使用jedit的朋友请进
- JTable + Vector 问题! 谢谢帮忙解决哈!!!
- 请问这样获取自己的桌面
- 如何用Java根据提供的数据生成折线图,然后保存成图形文件?(急
- 如何让树的节点代有多重属性?
- 如何生成一个图片文件(比如.gif,.jpg)?
- 让程序挂起一段时间的函数?
- 讨论:org.eclipse.jface.viewers.ComboBoxCellEditor 下拉菜单功能有时会失效的问题
- 谁能帮我看下,为什么这个中COST3的时间为0??
private int y; public Position() {
Random rand = new Random();
x = rand.nextInt();
y = rand.nextInt();
} public int getX() {
return x;
} public int getY() {
return y;
} /**
* @param args the command line arguments
*/
public static void main(String[] args) { Scanner in = new Scanner(System.in);
int n = in.nextInt(); Position[] pos = new Position[n];
for (int i = 0; i < n; i++)
pos[i] = new Position(); for (int i = 0; i < pos.length; i++)
System.out.println("油井的坐标分别为:(" + pos[i].getX() + "," + pos[i].getY() + ") "); int i, j;
for (i = 1; i < n; i++)
for (j = n - 1; j >= i; j--)
if (pos[j].getY() > pos[j - 1].getX()) { Position temp = pos[j];
pos[j] = pos[j - 1];
pos[j - 1] = temp; } if (n % 2 == 1)
System.out.println(pos[n / 2]);
System.out.println("最优的管道位置是Y:" + pos[n / 2] + "到" + pos[n / 2 + 1]);
}
}