题目描述
小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字(长度不一定,以0结束,最多不超过100个,数字不超过2^32-1),记住了然后反着念出来(表示结束的数字0就不要念出来了)。这对小鱼的那点记忆力来说实在是太难了,你也不想想小鱼的整个脑袋才多大,其中一部分还是好吃的肉!所以请你帮小鱼编程解决这个问题。
输入格式
一行内输入一串整数,以0结束,以空格间隔。
输出格式
一行内倒着输出这一串整数,以空格间隔。
输入输出样例
输入 #1 复制
3 65 23 5 34 1 30 0
输出 #1 复制
30 1 34 5 23 65 3
本文提供两种思路:一种用栈、一种用数组
#include<bits/stdc++.h>
using namespace std;
stack<long long>s;
int main(){
int n;
while(scanf("%d", &n), n){
s.push(n);
}
while(!s.empty()){
cout << s.top() << " ";
s.pop();
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
long long a[105];
int main(){
long long x;
int i=0;
while(scanf("%lld",&x), x){
a[++i] = x;
}
for(int j=i; j>0; j--)
cout << a[j] << " ";
return 0;
}