Question number: | 201403-1 |
Question name: | Opposite number |
time limit: | 1.0s |
Memory limit: | 256.0MB |
Problem Description: | Problem Description There are N non-zero and different integers. Please make a program to find out how many pairs of opposite numbers there are (a and -a are a pair of opposite numbers). Input format The first line contains a positive integer N. (1 ≤ N ≤ 500). Output format Only output an integer, that is, how many pairs of opposite numbers are contained in these N numbers. Sample input 5 Sample output 2 |
import java.util.Scanner;
public class 相反数201403_1 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int len=sc.nextInt();
int[] a=new int[1001];//<0
int[] b=new int[1001];//>0
for(int i=1;i<=len;i++){
int num=sc.nextInt();
if(num<0){
a[-num]+=1;
}else{
b[num]+=1;
}
}
int res=0;
for(int j=1;j<=1000;j++){
if(a[j]<=b[j]){
res+=a[j];
}else{
res+=b[j];
}
}
System.out.println(res);
}
}
Question number: |
201403-2 |
Question name: | window |
time limit: | 1.0s |
Memory limit: | 256.0MB |
Problem Description: | Problem Description In a graphics operating system, there are N windows, and each window is a rectangular area with two sides parallel to the coordinate axis. The point on the boundary of the window also belongs to the window. There are hierarchical differences between windows. In an area where more than one window overlaps, only the content of the window at the top level will be displayed. Input format 输入的第一行有两个正整数,即 N 和 M。(1 ≤ N ≤ 10,1 ≤ M ≤ 10) 输出格式 输出包括 M 行,每一行表示一次鼠标点击的结果。如果该次鼠标点击选择了一个窗口,则输出这个窗口的编号(窗口按照输入中的顺序从 1 编号到 N);如果没有,则输出"IGNORED"(不含双引号)。 样例输入 3 4 样例输出 2 样例说明 第一次点击的位置同时属于第 1 和第 2 个窗口,但是由于第 2 个窗口在上面,它被选择并且被置于顶层。 |
简单说一下窗口这个题的思路:我的想法是二维数组的索引号就代表是坐标,然后存的值就是窗口序号。需要注意的是每次点击一次,序号会进行更新,因为点击到的窗口会变成最上层的。然后再读数就好了。
import java.util.Scanner;
public class 窗口201403_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N=sc.nextInt();
int M=sc.nextInt();
int[][] a =new int[2560][1440];
int[] area=new int[4*N+1];
for(int i=1;i<=4*N;i++){
area[i]=sc.nextInt();
}
for(int i=0;i<N;i++){
for(int j=area[4*i+1];j<=area[4*i+3];j++){
for(int k=area[4*i+2];k<=area[4*i+4];k++){
a[j][k]=i+1;
}
}
}
int[] res=new int[M+1];
for(int i=1;i<=M;i++){
res[i]=a[sc.nextInt()][sc.nextInt()];
if(res[i]!=0){
for(int j=area[4*(res[i]-1)+1];j<=area[4*(res[i]-1)+3];j++){
for(int k=area[4*(res[i]-1)+2];k<=area[4*(res[i]-1)+4];k++){
a[j][k]=res[i];
}
}
}
}
for(int i=1;i<=M;i++){
if(res[i]==0){
System.out.println("IGNORED");
}else{
System.out.println(res[i]);
}
}
}
}