Love丶FFC's Blog

CCF-CSP_20140901:相邻数对

2019-09-10 09:37:59
阅读:8184   •   评论:145
标签:,

问题描述

给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1。

输入格式

输入的第一行包含一个整数n,表示给定整数的个数。

第二行包含所给定的n个整数。

输出格式

输出一个整数,表示值正好相差1的数对的个数。

样例输入

6
10 2 6 3 7 8

样例输出

3

样例说明

值正好相差1的数对包括(2, 3), (6, 7), (7, 8)。

评测用例规模与约定

1<=n<=1000,给定的整数为不超过10000的非负整数。

编程语言:Java

解题思想:

1.使用枚举法逐一比对。

2.使用Java中的Math.abs方法。

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

IDE代码如下:

  1. package CCF_CSP;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class _20140901_100point {
  6.  
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. int n = scanner.nextInt();
  10. int[] num = new int[n];
  11.  
  12. for(int i=0;i<n;i++)
  13. num[i]=scanner.nextInt();
  14.  
  15. int neighbor = 0; //相邻数对的个数
  16.  
  17. for(int i=0;i<n;i++) //枚举法
  18. for(int j=i+1;j<n;j++)
  19. {
  20. if(Math.abs(num[i]-num[j])==1) //绝对值为1
  21. {
  22. neighbor++;
  23. }
  24. }
  25.  
  26. System.out.println(neighbor);
  27. }
  28.  
  29. }
  30.  

评论板

共有 145 条评论

  1. fruinly

    vomiting 4 diarrhea 4 changes in appetite 3 lethargy 1 where to buy viagra connect Angel rTnEfxFPSsIYiDEFWP 6 18 2022

  2. FoorryNox

    buying cialis online Overall, our data suggest that manipulations in the mTOR pathway may have potential therapeutic benefit

  3. Preliaf

    In an accompanying commentary reported in Pediatrics, Cody Meissner Tufts University, USA notes that development of antiviral vaccines began when Peyton Rous postulated in the early 1900s that sarcoma transmission between hens may be virus related, followed by identification in 1964 of the first human DNA tumour virus, the Epstein Barr virus natural viagra for women

  4. Wopqms

    buy generic fenofibrate over the counter where to buy tricor without a prescription tricor 160mg cheap

  5. Zokrpg

    ketotifen over the counter zaditor buy online tofranil uk

  6. Cribxw

    oral cialis 10mg sildenafil over the counter order sildenafil 50mg online cheap

  7. Yngcna

    precose 25mg pill buy fulvicin no prescription buy griseofulvin 250 mg sale

  8. Hwbabb

    buy minoxidil online buy flomax 0.4mg online best ed medication

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名: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 -

已运行:

访问量:497537