1. 程式人生 > >CCF2014.3 第一題:相反數(java)

CCF2014.3 第一題:相反數(java)

CCF2014.3 第一題:相反數(java)

問題描述
  有 N 個非零且各不相同的整數。請你編一個程式求出它們中有多少對相反數(a 和 -a 為一對相反數)。
輸入格式
  第一行包含一個正整數 N。(1 ≤ N ≤ 500)。
  第二行為 N 個用單個空格隔開的非零整數,每個數的絕對值不超過1000,保證這些整數各不相同。
輸出格式
  只輸出一個整數,即這 N 個數中包含多少對相反數。
樣例輸入
5
1 2 3 -1 -2
樣例輸出
2

import java.util.Scanner;

public class XiangFanShu {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		int[] array=new int[n];
		for(int i=0;i<n;i++) {
			array[i]=sc.nextInt();
		}
		
		int count=0;
		for(int i=0;i<n;i++) {
			for(int j=i+1;j<n;j++) {
				if(array[i]+array[j]==0)count++;
			}
		}
		System.out.println(count);
	}
}