问题描述
试题编号: | 201403-1 |
试题名称: | 相反数 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: |
问题描述 有 N 个非零且各不相同的整数。请你编一个程序求出它们中有多少对相反数(a 和 -a 为一对相反数)。 输入格式 第一行包含一个正整数 N。(1 ≤ N ≤ 500)。 输出格式 只输出一个整数,即这 N 个数中包含多少对相反数。 样例输入 5 样例输出 2 |
答题栏
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int N;
Scanner s = new Scanner(System.in);
N = s.nextInt();
int[] a1 = new int[N];//存大于0的数
int[] a2 = new int[N];//存小于0的数
int i, temp, index1=0, index2=0;
for(i=0; i<N; i++) {
temp = s.nextInt();
if(temp>0) {
a1[index1] = temp;
index1++;
}else {
a2[index2] = temp;
index2++;
}
}
int j, counter=0;
for(i=0; i<index1; i++) {
for(j=0; j<index2; j++) {
if(a1[i]==-a2[j]) {
counter++;
break;
}
}
}
System.out.println(counter);
}
}