牛牛的老师给出了一个区间的定义:对于x ≤ y,[x, y]表示x到y之间(包括x和y)的所有连续整数集合。例如[3,3] = {3}, [4,7] = {4,5,6,7}.牛牛现在有一个长度为n的递增序列,牛牛想知道需要多少个区间并起来等于这个序列。
例如:
{1,2,3,4,5,6,7,8,9,10}最少只需要[1,10]这一个区间
{1,3,5,6,7}最少只需要[1,1],[3,3],[5,7]这三个区间
输入包括两行,第一行一个整数n(1 ≤ n ≤ 50), 第二行n个整数a[i](1 ≤ a[i] ≤ 50),表示牛牛的序列,保证序列是递增的。
输出一个整数,表示最少区间个数。
5 1 3 5 6 7
3
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] array=new int[n]; int count=1; for(int i=0;i<n;i++){ array[i]=sc.nextInt(); } for(int i=1;i<n;i++){ if(array[i-1]!=array[i]-1){ count++; } } System.out.println(count); } }总的来说就是判断是否连续的,即array[i]是否等于array[i+1]-1,如果不是count++
#include <iostream> usingnamespacestd; intmain(void) { intlen, temp, first; cin >> len; intcount = 1; cin >> first; for(inti = 0; i < len-1; i++) { cin >> temp; if(temp - first != 1) { count++; } first = temp; } cout << count << endl; return0; }
#include <iostream>
using namespace std;
int main() {
int a[50];
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
int counter = 1;
for (int i = 0; i < n - 1; i++) {
if (a[i] + 1 != a[i + 1] ) {
counter++;
}
}
cout << counter << endl;
return 0;
}
import java.util.Scanner; /** * @ClassName Main * @Description TODO * @Author Wlison * @Date 2020/2/18 11:43 * @Version 1.0 **/ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int res = 1; int pre = sc.nextInt(); int cur = pre; for (int i = 1; i < n; i++) { cur = sc.nextInt(); if (cur-pre!=1){ res++; } pre = cur; } System.out.println(res); } } }
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin=new Scanner (System.in); int n=cin.nextInt();//数组长度; int temp=cin.nextInt();//第一个元素(也会用于后期的比较) int out=1; for(int i=1;i<n;i++) { int d=cin.nextInt(); if(temp+1 !=d) { out++; } temp=d; } System.out.print(out); } }
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; int count = 1; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); if (i > 0 && arr[i] - arr[i - 1] != 1) { count++; } } System.out.println(count); } }
//如果后一个数比前一个数大1,就将n减去1 #include<iostream> #include<vector> using namespace std; int main(){ int n; cin >> n; vector<int> v1; int m,s=n; for (int i = 0; i < n; i++){ cin >> m; v1.push_back(m); } for (int i = n - 1; i >= 1; i--){ if (v1[i] - v1[i - 1] == 1){ s--; } } cout << s << endl; system("pause"); return 0; }
n=int(input()) seq=list(map(int,input().split())) res=1 i=0 while i<n-1: if seq[i]+1!=seq[i+1]: i+=1 res+=1 else: i+=1 print(res)
#include<iostream> using namespace std; int main(){ int n,count=0; cin>>n; int* a=new int[n]; for(int i=0;i<n;i++) cin>>a[i]; for(int i=1;i<n;i++){ if(a[i]-a[i-1]!=1) count++; if(i==n-1) count++; } cout<<count<<endl; }
#include<iostream> #include<stdio.h> #include<algorithm> #include<cstring> #include<vector> #include<math.h> using namespace std; int main(){ int n; cin >> n; int a[55]; for(int i = 0; i < n; i++){ cin >> a[i]; } int count = 1; for(int i = 1; i < n; i++){ if(a[i] - a[i - 1] <= 1){ continue; } else{ count++; } } cout << count << endl; return 0; }
import java.util.*; public class Main { private static final int N_MAX = 50 + 5; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[N_MAX]; for (int i=1; i<=n; i++) { arr[i] = sc.nextInt(); } int count = 0; for (int i=2; i<=n+1; i++) { if (arr[i] - arr[i-1] != 1) { count++; } } System.out.println(count); } }
import java.util.*; public class Main{ public static void main(String[] args){ try(Scanner in = new Scanner(System.in)){ int n = in.nextInt(); int[] a = new int[n]; for(int i = 0;i < n;i++){ a[i] = in.nextInt(); } System.out.println(helper(a)); } } public static int helper(int[] a){ int sum = 1; for(int i = 1;i < a.length;i++){ if(a[i] - a[i - 1] != 1) sum++; } return sum; } }