Love丶FFC's Blog

CCF-CSP_20150901:数列分段

2019-09-15 22:05:43
阅读:571   •   评论:5
标签:,

问题描述

给定一个整数数列,数列中连续相同的最长整数序列算成一段,问数列中共有多少段?

输入格式

输入的第一行包含一个整数n,表示数列中整数的个数。
第二行包含n个整数a1a2, …, an,表示给定的数列,相邻的整数之间用一个空格分隔。

输出格式

输出一个整数,表示给定的数列有多个段。

样例输入

8
8 8 8 0 12 12 8 0

样例输出

5

样例说明

8 8 8是第一段,0是第二段,12 12是第三段,倒数第二个整数8是第四段,最后一个0是第五段。

评测用例规模与约定

1 ≤ n ≤ 1000,0 ≤ ai ≤ 1000。

编程语言:Java

解题思想:

1.判断是否与前一个数相同,如不同则计数。

时间复杂度:O(n)

IDE代码如下:

  1. package CCF_CSP;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class _20150901_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. int count = 1;
  13. for(int i=0;i<n;i++)
  14. {
  15. num[i] = scanner.nextInt();
  16. if(i > 0 && num[i] != num[i-1]) //该整数非第一个整数且与前一个整数不同
  17. {
  18. count++;
  19. }
  20. }
  21. System.out.println(count);
  22. }
  23.  
  24. }
  25.  

评论板

共有 5 条评论

  1. Abupguele

    Hypothalamic pituitary thyroidal function in eumenorrheic and amenorrheic athletes generic for cialis

  2. Outlils

    There may still be some inflammation after those four days, but it typically resolves after a month cialis online prescription

  3. invoimi

    eCoefficient represents a multiplicative change in hormone level for an IQR change in phthalate metabolite concentration after back- transformation of both hormone and phthalate metabolite concentrations; for an IQR change in phthalate metabolite concentration, a coefficient equal to 1 propecia

  4. invoimi

    buy cialis with paypal ketoconazole will increase the level or effect of lumefantrine by affecting hepatic intestinal enzyme CYP3A4 metabolism

  5. PRwgUSG

    Krausz C, Hoefsloot L, Simoni M, TГјttelmann F cialis daily Based on the old adage strength in numbers, Legal Services of Southern Piedmont, NC Child, and the American Heart Association would like to convene those working on expansion to share updates on our individual advocacy efforts and develop synergy as a collaborative group

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

已运行:

访问量:511525