首页 > 试题广场 >

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
import java.util.Scanner;
//双指针的思路
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int len = in.nextInt();
        int[] nums = new int[len];
        for(int i = 0 ;i<len;i++){
            nums[i] = in.nextInt();
        }
        int left = 0 ;
        int right = len-1;
        while(left<=right){
            int temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
            left++;
            right--;
        }
        for(int i:nums){
            System.out.print(i+" ");
        }
    }
}
发表于 2024-03-17 10:48:32 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = br.readLine()) != null) {
            int n = Integer.parseInt(s);
            String[] ss = br.readLine().split(" ");

            for (int i = n - 1; i >= 0; --i) {
                System.out.print(ss[i] + " ");
            }

        }
    }
}



发表于 2021-04-02 14:46:49 回复(0)