2016年1月21日 星期四

d091: 00476 - Points in Figures: Rectangles

內容 :
在x-y平面上,給你一些矩形和一些點,請你回答這些點落在哪些矩形內(如果有的話)。另外,在這個問題中,剛好落在邊上的點不視為落在該矩形內。
輸入說明 : 
首先是矩形的資料,每個矩形一列,第1個字元代表圖形的類別(r 代表矩形),接下來有4個數值分別代表該矩形左上角及右下角的座標。矩形的個數不會超過10個。
以一列僅含有一個*代表矩形資料結束。
接下來的每列為一個點的座標,也就是要測試的點。若點座標為9999.9 9999.9代表輸入結束(此點不需輸出)
輸出說明 : 
對每一個測試的點,若其落在某矩形內,則輸出下列格式的訊息:
Point i is contained in figure j
如果某個點沒有落在任何矩形內,則輸出:
Point i is not contained in any figure
請注意:點和矩形的編號是按照他們出現在input的順序。請參考Sample Output
範例輸入 : help
r 8.5 17.0 25.5 -8.5
r 0.0 10.3 5.5 0.0
r 2.5 12.5 12.5 2.5
*
2.0 2.0
4.7 5.3
6.9 11.2
20.0 20.0
17.6 3.2
-5.2 -7.8
9999.9 9999.9
範例輸出:
Point 1 is contained in figure 2
Point 2 is contained in figure 2
Point 2 is contained in figure 3
Point 3 is contained in figure 3
Point 4 is not contained in any figure
Point 5 is contained in figure 1
Point 6 is not contained in any figure
提示 : 
Sample Input中矩形及測試點的圖


 * 中文翻譯:Lucky 貓
標籤:
出處: 
UVa476 (管理:MAPLEWING)

import java.util.Scanner;

public class UVAd091 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String ch;
final int X1=0,Y1=1,X2=2,Y2=3;
double[][] arr =new double[10][4];
int c=0;
while(!(ch=sc.next()).equals("*")){
arr[c][X1]=sc.nextDouble();
arr[c][Y1]=sc.nextDouble();
arr[c][X2]=sc.nextDouble();
arr[c][Y2]=sc.nextDouble();
c++;
}
double x,y;
int count=1;
while((x=sc.nextDouble())!=9999.9 && (y=sc.nextDouble())!=9999.9){
boolean flag=true;
for(int i=0;i<c;++i){
if(x>arr[i][X1] && y<arr[i][Y1] && x<arr[i][X2] && y>arr[i][Y2]){
  System.out.println("Point "+count+" is contained in figure "+(i+1));
  flag=false;
   }
}
if(flag)
System.out.println("Point "+count+" is not contained in any figure");
count++;
}

}

}


沒有留言:

張貼留言