201412-1 相反數
阿新 • • 發佈:2018-12-06
注意不要重複計數就ok了,奉上java滿分程式碼
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] numArr = new int[n]; for(int i = 0; i < n; i++){ numArr[i] = scanner.nextInt(); } scanner.close(); int count = 0; for(int i = 0; i < n; i++){ int numI = numArr[i]; for(int j = i + 1; j < n; j++){ if(numArr[j] + numI == 0){ count++; break; } } } System.out.println(count); } }