题目描述:
小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字ai(长度不一定,以0结束),记住了然后反着念出来(表示结束的数字就不要念出来)。这对小鱼的那点记忆力实在是太难了,你也不想想小鱼的整个脑袋有多大,其中一部分还是好吃的肉!所以请你帮小鱼编程解决这个问题。
输入格式:
一行内输入一串整数,以0结束,以空格间隔。
输出格式:
一行内倒着输出这串整数,以空格间隔。
样例:
输入#1
3 65 23 5 34 1 30 0
输出#1
30 1 34 5 23 65 3
提示与说明:
对于所有数据,保证在0 <= a <= 2^31-1,数字个数不超过100.
import java.util.Scanner;
public class Test05 {
static int[] nums = new int[102];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = 0;
while (true) {
nums[i] = sc.nextInt();
if (nums[i] == 0) break;
i++;
}
dfs(i-1);
}
public static void dfs(int n){
if (n < 0) return;
System.out.print(nums[n] + " ");
dfs(n-1);
}
}