首页 > 试题广场 >

神秘石像的镜像序列

[编程题]神秘石像的镜像序列
  • 热度指数:2442 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}在远古遗迹中,探险家们在石碑上发现了一系列关键信息,这些信息被刻录成一串整数序列,以数字 0 作为结束标志。
\hspace{15pt}为了触发机关,需要将这串刻碑数字倒序读取(不包括结束标志 0)。
\hspace{15pt}现在,请你编写程序,帮助探险家完成这一倒序读取的任务。

输入描述:
\hspace{15pt}在一行内输入若干非负整数,以 0 作为结束标志,每两个整数之间用空格分隔。 
\hspace{15pt}保证整数个数不超过 100,且满足 \left(0 \leqq a_i \leqq 2^{31}-1\right)


输出描述:
\hspace{15pt}在一行内按空格分隔输出倒序后的整数序列(不包含结束标志 0)。
示例1

输入

8 15 3 42 7 0

输出

7 42 3 15 8

说明

在第一个样例中: 
\hspace{8pt}\bullet\,去掉结束标志 0,原序列为 [8,15,3,42,7]
\hspace{8pt}\bullet\,倒序后输出 \text{7 42 3 15 8}
示例2

输入

10 20 30 40 50 0

输出

50 40 30 20 10

备注:
对于 100\% 的数据,保证 0 \leq a_i \leq 2^{31} - 1,数字个数不超过 100
a = list(map(int,input().split()))
a.remove(0)
print(*a[::-1])
发表于 2025-07-10 11:13:05 回复(0)
num=list(map(int,input().split()))
del num[-1]
num.reverse()
for n in num:
    print(n,end=' ')
发表于 2025-07-13 15:09:27 回复(0)
print(' '.join(input().split()[-2::-1]))
发表于 2025-07-12 23:03:02 回复(0)
#include <stdio.h>
int main()
{
    int arr[100] = { 0 };
    int i = 0;
    int n = 0;
    //输入
    while (1)
    {
        scanf("%d", &arr[i]);
        if (arr[i] == 0)
        {
            break;
        }
        i++;
    }
    n = i;//n是0的下标
    //倒序
    int left = 0;
    int right = n-1;
    int tmp = 0;
    while (left < right)
    {
        tmp = arr[left];
        arr[left] = arr[right];
        arr[right] = tmp;
        left++;
        right--;
    }
    for (i = 0;i < n;i++)
    {
        printf("%d ", arr[i]);
    }



    return 0;
}

发表于 2025-07-11 15:03:36 回复(0)
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,m=10,n=0,i;
    int *sz=(int *)malloc(m*sizeof(int));
    if(!sz)return 0;
    while(scanf("%d",&a)&&a)
    {
        if(n>=m)
        {
            m*=2;
            int *tmp=(int *)realloc(sz,m*sizeof(int));
            if(!tmp){printf("error");free(sz);return 0;}
            sz=tmp;
        }
        sz[n++]=a;
    }
    for(i=n-1;i>=0;i--)
    printf("%d ",sz[i]);
    free(sz);
    return 0;
}#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,m=10,n=0,i;
    int *sz=(int *)malloc(m*sizeof(int));
    if(!sz)return 0;
    while(scanf("%d",&a)&&a)
    {
        if(n>=m)
        {
            m*=2;
            int *tmp=(int *)realloc(sz,m*sizeof(int));
            if(!tmp){printf("error");free(sz);return 0;}
            sz=tmp;
        }
        sz[n++]=a;
    }
    for(i=n-1;i>=0;i--)
    printf("%d ",sz[i]);
    free(sz);
    return 0;
}
发表于 2025-07-09 18:32:14 回复(0)
#include <stdio.h>

int main() {
    int num, a = 0;
    int n[100];
    while (scanf("%d", &num)==1 && num != 0&&a<=100)
    {
        n[a] = num;
        a++;
    }  
    while(a!=0)
    {
        a--;
        printf("%d ", n[a]);
    }

    return 0;

}
发表于 2025-07-06 22:26:51 回复(0)
a = list(map(int,input().split()))
a.pop(-1)
a.reverse()
for i in a:
    print(i,end=' ')

发表于 2025-07-02 15:39:16 回复(0)
L=list(map(int,input().split()))
new_list=L[-2::-1]
for item in new_list:
    print(item,end=" ")

发表于 2025-06-27 21:50:29 回复(0)
import sys

for line in sys.stdin:
    a = line.split()
    del a[-1]
    a.reverse()
    print(" ".join(a))
发表于 2025-06-26 14:42:41 回复(0)
n=input().split()
n=[x for x in n if x and x != '0']
n1=n[::-1]
print (' '.join(n1))
发表于 2025-06-23 16:17:51 回复(0)
#include <stdio.h>

int main() {
    int nums[1000]; // 假设序列长度不超过1000
    int count = 0;
    int num;
    
    // 读取整数序列,直到遇到0
    while (1) {
        scanf("%d", &num);
        if (num == 0) break;
        nums[count++] = num;
    }
    
    // 逆序输出序列
    for (int i = count - 1; i >= 0; i--) {
        printf("%d", nums[i]);
        if (i > 0) printf(" "); // 最后一个数字后面不输出空格
    }
    printf("\n");
    
    return 0;
}

发表于 2025-06-12 13:38:27 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int[] arr = new int[100];//定义数组最大长度
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int count = 0;//输入数字的个数
            while(true){
                int num = in.nextInt();
                if(num == 0)break;//数字为0时跳出循环
                arr[count] = num;
                count++;
            }
            for(int i = count - 1;i >= 0;i--){//从第0位到第count-1位
                System.out.printf("%d ",arr[i]);//逆序输出不含0的数组元素
            }
        }
    }
}
发表于 2025-06-09 17:06:06 回复(0)