Time Limit: 3 second
Memory Limit: 2 MB
【问题描述】
输入一个正整数序列(1<=个数<=500,所有数据均在整数范围内),遇负数停止,将正整数序列按输入顺序反向输出。(本题请尝试使用链表结构完成)。
【输入】
多行,为一系列正整数,最后一个负数作为结束标志
【输出】
多行。与输入顺序相反的正整数序列,换行由系统自行完成。(每个数之间空一格,最后一个数后面也有一个空格,并回车结束)
【输入样例】
1 3 5 7 9 -100
【输出样例】
9 7 5 3 1
【题解】
可以用栈来做,先进后出表。
【代码】
#include <cstdio>
const int maxn = 1000;
int stack[maxn],top = 0;
void input_data()
{
int t;
scanf("%d",&t);
while (t >= 0)
{
stack[++top] = t; //读到不是负数就入栈。
scanf("%d",&t);
}
}
void output_ans()
{
while (top != 0)
printf("%d ",stack[top--]); //top--会在入栈后执行。
}
int main()
{
input_data();
output_ans();
return 0;
}