題目描述:一行內(nèi)輸入一串整數(shù),以0結(jié)束,以空格間隔。一行內(nèi)倒著輸出這一串整數(shù),以空格間隔。
分析:輸入規(guī)模較小,主要試試遞歸和模擬棧。
代碼:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<vector>
#define ll long long
using namespace std;
#define inf 0x3f3f3f3f
int n;
void fun()
{
int a;
scanf("%d",&a);
if (a == 0) //結(jié)束條件
return ;
fun();
printf("%d ", a); //遞歸返回時完成輸出即可實現(xiàn)后進先出
}
int main()
{
int i, j;
while(~scanf("%d", &n))
{
fun();
printf("%d\n",n);
}
return 0;
}
#include<cstdio>
#include<iostream>
using namespace std;
#define inf 0x3f3f3f3f
int n;
int sta[110];
int main()
{
int i, top = 0;
do
{
scanf("%d", &sta[top ++]);
}while(sta[top - 1]); //總是指向下一個還未輸入的位置
top -= 2; //減一后指向0, 再減一
while(top)
{
printf("%d ", sta[top --]);
}
printf("%d\n", sta[top]);
return 0;
}