列表

详情


JAVA17. 数组倒转

描述

输入6个整数,先将其输出然后再将数组倒转,再次输出数组

输入描述

用户随机输入的6个int类型变量

输出描述

先输出用户输入顺序的数组(这一步预设代码已给出)
再输出反转后的数组(输出格式为Arrays.toString(arr),参考预设代码中的输出格式)

示例1

输入:

52 10  37 40 60 83

输出:

[52, 10, 37, 40, 60, 83]
[83, 60, 40, 37, 10, 52]

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Java 解法, 执行用时: 25ms, 内存消耗: 10816KB, 提交时间: 2022-02-10

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[] arr = new int[6];
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = scanner.nextInt();
        }
        System.out.println(Arrays.toString(arr));

        //write your code here......
        int left = 0,right = arr.length -1;
        while(left < right){
           int temp = arr[left];
           arr[left] = arr[right];
           arr[right] = temp;
           left++;  //指针向中间靠
           right--;
        }
        
        System.out.println(Arrays.toString(arr));
    }
}

Java 解法, 执行用时: 26ms, 内存消耗: 10684KB, 提交时间: 2022-02-09

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[] arr = new int[6];
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = scanner.nextInt();
        }
        System.out.println(Arrays.toString(arr));

        //write your code here......
        for (int i = 0; i < arr.length / 2; i++) {
            int temp = arr[arr.length - i - 1];
            arr[arr.length - i - 1] = arr[i];
            arr[i] = temp;
        }

        System.out.println(Arrays.toString(arr));
    }
}

Java 解法, 执行用时: 28ms, 内存消耗: 10544KB, 提交时间: 2022-02-09

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[] arr = new int[6];
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = scanner.nextInt();
        }
        System.out.println(Arrays.toString(arr));

        //write your code here......
        int[] darr = new int[6];
        for(int i=4; i>=-1; i--){
            darr[i+1]=arr[4-i];
        }
        arr=darr;

        System.out.println(Arrays.toString(arr));
    }
}

Java 解法, 执行用时: 28ms, 内存消耗: 10580KB, 提交时间: 2022-02-09

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[] arr = new int[6];
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = scanner.nextInt();
        }
        System.out.println(Arrays.toString(arr));

        //write your code here......
        int [] arr1 = new int []{0,0,0,0,0,0};
        for(int i=0,j=arr.length-1; i<6 ;i++,j--){
            arr1[i]=arr[j];
        }
        

        System.out.println(Arrays.toString(arr1));
        
    }
}

Java 解法, 执行用时: 28ms, 内存消耗: 10600KB, 提交时间: 2021-11-14

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int[] arr = new int[6];
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = scanner.nextInt();
        }
        System.out.println(Arrays.toString(arr));

        //write your code here......
        
        for (int i = 0; i < arr.length/2; i++) {
            int n = arr[arr.length-i-1];
            arr[arr.length-i-1] = arr[i];
            arr[i] = n;
            
        }

        System.out.println(Arrays.toString(arr));
    }
}

上一题