原题链接:https://codeforces.com/contest/1005/problem/A
Tanya and Stairways
time limit per test:1 second | memory limit per test:256 megabytes |
input:standard input | output:standard output |
Description
Little girl Tanya climbs the stairs inside a multi-storey building. Every time Tanya climbs a stairway, she starts counting steps from to the number of steps in this stairway. She speaks every number aloud. For example, if she climbs two stairways, the first of which contains steps, and the second contains steps, she will pronounce the numbers
小女孩谭雅在一个多层建筑内爬楼梯。每次她爬一层楼梯,她就会从开始大声的数数,一直数到她现在楼层楼梯的阶数。例如,如果她爬两层楼梯,第一层楼梯阶,第二层楼梯阶,她就会数
You are given all the numbers pronounced by Tanya. How many stairways did she climb? Also, output the number of steps in each stairway.
The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.
Input
The first line contains — the total number of numbers pronounced by Tanya.
The second line contains integers — all the numbers Tanya pronounced while climbing the stairs, in order from the first to the last pronounced number. Passing a stairway with steps, she will pronounce the numbers in that order.
The given sequence will be a valid sequence that Tanya could have pronounced when climbing one or more stairways.
Output
In the first line, output — the number of stairways that Tanya climbed. In the second line, output numbers — the number of steps in each stairway she climbed. Write the numbers in the correct order of passage of the stairways.
Examples
input
7
1 2 3 1 2 3 4
output
2
3 4
input
4
1 1 1 1
output
4
1 1 1 1
input
5
1 2 3 4 5
output
1
5
input
5
1 2 1 2 1
output
3
2 2 1
题目分析:根据输入,求出tanya爬了几层楼梯,并且分别是那几层楼梯。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,x[1000],prev,curr,t;
//x数组存放阶梯,prev、curr分别存放上一次和这一次所在的阶梯
t=0;prev=0; // 初始化
scanf("%d",&n);
while (n--)
{
scanf("%d",&curr);
if (curr<=prev)
{
x[t++] = prev;
prev = 1;
}
prev = curr;
}
x[t++] = prev;
printf("%d\n",t);
for (int i=0; i<t; i++)
printf("%d ",x[i]);
return 0;
}
网友评论