CF1005A 题解

题意

有个人在上楼,他会数每一层楼多少阶台阶,假设上了两层,第一层有3个台阶,第二层有4个台阶,那么他数的数就是1,2,3,1,2,3,4。
输出一共上了几层楼,每层楼有多少个台阶

题解

通过last标记上一层的数字,(如果不是第一次输入)等到输入为1时,说明进入了新的一层
最后一层要单独加入vector

代码

#include <bits/stdc++.h>
using namespace std;

int n, x, last;
vector<int>v;

int main()
{
 cin >> n;

 for(int i = 1; i <= n; i++)
 {
     cin >> x;
     if(x == 1 && i != 1)  
     {
     	v.push_back(last);
     }
     last = x;  //更新last
 }
 v.push_back(last);   //直接放入最后一个台阶

 cout << v.size() << endl;
 for(auto t : v)   cout << t << " ";
 
 return 0; 
}
posted @ 2021-11-25 21:27  codekin  阅读(27)  评论(0)    收藏  举报
Document