CCF2015.9 第一題:數列分段 (java)
阿新 • • 發佈:2018-11-30
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); } }