题意
有个人在上楼,他会数每一层楼多少阶台阶,假设上了两层,第一层有3个台阶,第二层有4个台阶,那么他数的数就是1,2,3,1,2,3,4,现在给你这个数数的序列问上了多少层楼,每层楼有多少个台阶。
题解
每个碰到一个1就把他前面那个数字放入vector,最后一个数也放入vector,输出即可。
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
const int INF = 0x3f3f3f3f;
typedef long long LL;
int n, a[MAXN];
vector<int> ans;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
if (i != 1 && a[i] == 1) ans.push_back(a[i-1]);
}
ans.push_back(a[n]);
printf("%d\n", ans.size());
for (auto i: ans) printf("%d ", i);
return 0;
}
/*
7
1 2 3 1 2 3 4
*/