Love丶FFC's Blog

CCF-CSP_20140301:相反数

2019-09-10 09:32:27
阅读:8404   •   评论:153
标签:,

问题描述

有 N 个非零且各不相同的整数。请你编一个程序求出它们中有多少对相反数(a 和 -a 为一对相反数)。

输入格式

第一行包含一个正整数 N。(1 ≤ N ≤ 500)。

第二行为 N 个用单个空格隔开的非零整数,每个数的绝对值不超过1000,保证这些整数各不相同。

输出格式

只输出一个整数,即这 N 个数中包含多少对相反数。

样例输入

5
1 2 3 -1 -2

样例输出

2

编程语言:Java

解题思想:

1.用枚举法逐一比对。

时间复杂度:O(n^2)

IDE代码如下:

  1. package CCF_CSP;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6.  
  7. public class _20140301_100point {
  8.  
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11. int N = scanner.nextInt();
  12. List<Integer> num = new ArrayList<Integer>();
  13.  
  14. for(int i=0;i<N;i++)
  15. num.add(scanner.nextInt());
  16.  
  17. int opposite = 0; //相反数个数
  18.  
  19. for(int i=0;i<num.size();i++) //枚举
  20. {
  21. for(int j=i+1;j<num.size();j++)
  22. {
  23. if(num.get(i)==num.get(j)*(-1)) //逐一比对
  24. {
  25. opposite++;
  26. }
  27. }
  28. }
  29.  
  30.  
  31. System.out.println(opposite);
  32.  
  33. }
  34.  
  35. }
  36.  

评论板

共有 153 条评论

  1. fruinly

    cialis for daily use Endocrine data

  2. FoorryNox

    How to use Orenitram ER cialis for sale in usa epinephrine and epinephrine racemic both decrease sedation

  3. Preliaf

    But Song Ning didn propranolol and erectile dysfunction t dare to go to the back mountain, so he could only ask the elder sister A Sang first, but A Sang was direct, saying that this matter was waiting for Gu Min to come back levitra vardenafil generico

  4. Twnvba

    order fenofibrate 200mg generic purchase fenofibrate online order fenofibrate without prescription

  5. Bbzwwp

    buy ketotifen 1 mg online order generic ketotifen cost tofranil 75mg

  6. Pmnwyk

    cialis 40mg sale viagra on line buy viagra 100mg sale

  7. Botvyl

    precose 25mg uk repaglinide online order buy griseofulvin 250mg generic

  8. Okicwu

    how to get minoxidil without a prescription non prescription ed drugs ed pills that work quickly

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:491848