問題描述
試題編號: | 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);
}
}