CCF2015.9 第一题:数列分段 (java)

CCF2015.9 第一题:数列分段 (java)

问题描述
  给定一个整数数列,数列中连续相同的最长整数序列算成一段,问数列中共有多少段?
输入格式
  输入的第一行包含一个整数n,表示数列中整数的个数。
  第二行包含n个整数a1, a2, …, an,表示给定的数列,相邻的整数之间用一个空格分隔。
输出格式
  输出一个整数,表示给定的数列有多个段。
样例输入
8
8 8 8 0 12 12 8 0
样例输出
5

import java.util.Scanner;

public class ShuLieFenDuan {
	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=1;i<n;i++) {
			if(array[i-1]==array[i]) {
				count++;
			}
			
		}
		System.out.println(n-count);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40677872/article/details/84645427