201403-1 相反数:
package exam201403;
import java.util.Scanner;
public class Task1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count = 0;
int[] num = new int[n];
for(int i=0; i<n; i++){
num[i] = sc.nextInt();
for(int j=0; j<i; j++){
if(num[i] == -num[j]){
count++;
}
}
}
System.out.println(count);
}
}
201403-1 窗口:
利用数组实现:
package exam201403;
import java.util.Scanner;
public class Task2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
int n = sc.nextInt();
int[] X1 = new int[m];
int[] Y1 = new int[m];
int[] X2 = new int[m];
int[] Y2 = new int[m];
int[] tag = new int[m]; //记录初始编号
int[] clickX = new int[n];
int[] clickY = new int[n];
int[]temp = new int[5];
for(int i=m-1; i>=0; i--){ //后输入的在顶层,顶层放在数组前面
X1[i] = sc.nextInt();
Y1[i] = sc.nextInt();
X2[i] = sc.nextInt();
Y2[i] = sc.nextInt();
tag[i] = m-i;
}
for(int i=0; i<n; i++){
clickX[i] = sc.nextInt();
clickY[i] = sc.nextInt();
}
for(int i=0; i<n; i++){
boolean flag = true;
for(int j=0; j<m; j++){
if(clickX[i]>=X1[j] && clickX[i]<=X2[j] && clickY[i]>=Y1[j] &&clickY[i]<=Y2[j]){
System.out.println(tag[j]);
temp[0] = X1[j];
temp[1] = Y1[j];
temp[2] = X2[j];
temp[3] = Y2[j];
temp[4] = tag[j];
for(int k=j; k>0; k--){ //把点击的窗口放在最前,其他顺序不变(前排后移)
X1[k] = X1[k-1];
Y1[k] = Y1[k-1];
X2[k] = X2[k-1];
Y2[k] = Y2[k-1];
tag[k] = tag[k-1];
}
X1[0] = temp[0];
Y1[0] = temp[1];
X2[0] = temp[2];
Y2[0] = temp[3];
tag[0] = temp[4];
flag = !flag;
break;
}
}
if(flag){
System.out.println("IGNORED");
}
}
}
}
更简单的方法,利用 容器ArrayList和静态类 实现:
package exam201403;
import java.util.ArrayList;
import java.util.Scanner;
public class Task2_list {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<loc> al = new ArrayList<loc>();
Scanner sc = new Scanner(System.in);
int m=0, n=0;
int x1, y1, x2, y2;
int i, j;
m = sc.nextInt();
n = sc.nextInt();
for(i=0; i<m; i++){ //先输入的窗口在底部
x1 = sc.nextInt();
y1 = sc.nextInt();
x2 = sc.nextInt();
y2 = sc.nextInt();
al.add(new loc(x1, y1, x2, y2, i+1));
}
String[] print = new String[n];
for(i=0; i<n; i++){
x1 = sc.nextInt();
y1 = sc.nextInt();
j = m-1;
for(; j>=0; j--){ //先输入的窗口在尾部,要从顶部开始找,下标越大越靠前
int tag = al.get(j).find(x1, y1);
if(tag != -1){
print[i] = tag+"";
al.add(al.get(j));
al.remove(j);
break;
}
}
if(j == -1){
print[i] = "IGNORED";
}
}
for(i=0; i<n; i++){
System.out.println(print[i]);
}
}
static class loc{
int x1, x2, y1, y2, tag;
public loc(int x1, int y1, int x2, int y2, int tag) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
this.tag = tag;
}
public int find(int x, int y){
if(x>=x1 && x<=x2 && y>=y1 && y<=y2){
return tag;
}
return -1;
}
}
}