zoukankan      html  css  js  c++  java
  • Java经典编程题50道之三十五

    有一个数组,将其最大的元素与第一个元素交换,最小的元素与最后一个元素交换,然后输出数组。

    public class Example35 {
        public static void main(String[] args) {
            int[] a = { 5, 7, 6, 1, 9, 4, 2, 3, 8 };
            convertElement(a);
        }

        public static void convertElement(int[] a) {
            System.out.print("测试数组为:");
            for (int r : a) {
                System.out.print(r + " ");
            }
            int max = 0;
            for (int i = 0; i < a.length; i++) {
                if (a[i] > a[max]) {
                    max = i;
                }
            }
            int temp = a[0];
            a[0] = a[max];
            a[max] = temp;
            int min = 0;
            for (int i = 0; i < a.length; i++) {
                if (a[i] < a[min]) {
                    min = i;
                }
            }
            int temp1 = a[min];
            a[min] = a[a.length - 1];
            a[a.length - 1] = temp1;
            System.out.print(" 变换后的数组为:");
            for (int r : a) {
                System.out.print(r + " ");
            }
        }
    }

  • 相关阅读:
    顺序容器
    forward_list
    array
    第十一章 关联容器
    C++数组
    C++标准库算法
    第十章 泛型算法
    第九章 顺序容器
    操作系统概述
    文件输入输出
  • 原文地址:https://www.cnblogs.com/qubo520/p/6955815.html
Copyright © 2011-2022 走看看