首页 > 试题广场 >

Zero-complexity Transposition

[编程题]Zero-complexity Transposition
  • 热度指数:12805 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
You are given a sequence of integer numbers. Zero-complexity transposition of the sequence is the reverse of this sequence. Your task is to write a program that prints zero-complexity transposition of the given sequence.

输入描述:
For each case, the first line of the input file contains one integer n-length of the sequence (0 < n ≤ 10 000). The second line contains n integers numbers-a1, a2, …, an (-1 000 000 000 000 000 ≤ ai ≤ 1 000 000 000 000 000).


输出描述:
For each case, on the first line of the output file print the sequence in the reverse order.
示例1

输入

5
-3 4 6 -8 9

输出

9 -8 6 4 -3

用C语言链栈实现

#include<stdio.h>
#include<stdlib.h>
#define Status int
#define dataType int

typedef struct node {
    dataType data;
    struct node *next;
}*LinkStack,node;

LinkStack createStack() {
    LinkStack p = (node*)malloc(sizeof(node));
    p->next = NULL;
    return p;
}
//判空
Status isEmpty(LinkStack ls) {
    if (!ls->next)
        return 1;
    else
        return 0;
}
//入栈
void push(LinkStack ls, dataType data) {
    node* p = (node*)malloc(sizeof(node));
    p->data = data;
    p->next = ls->next;
    ls->next = p;
}
//出栈 需要返回出栈状态 栈为空则失败
Status pop(LinkStack ls) {
    if (!isEmpty(ls)) {
        node* p = (node*)malloc(sizeof(node));
        p = ls->next;
        ls->next = ls->next->next;
        return 1;
    }
    return 0;
}
//获取栈顶元素
dataType top(LinkStack ls) {
    if (!isEmpty(ls))
        return ls->next->data;
    return NULL;    //栈空则返回null
}

//求栈中元素个数

int size(LinkStack ls) {
    int n = 0;
    node *p = ls->next;
    while (p) {
        n++;
        p = p->next;
    }
    return n;
}

int main() {
    LinkStack ls = createStack();
    int n,t;
    while(~scanf("%d",&n)){
        while(n--){
            scanf("%d",&t);
            push(ls,t);
        }
        while(!isEmpty(ls)){
            printf("%d ",top(ls));
            pop(ls);
        }
        printf("\n");
    }
}
发表于 2022-02-12 19:57:03 回复(0)