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 + " ");
            }
        }
    }

  • 相关阅读:
    我喜欢的电影
    QObject
    python-类
    pycharm活动模板
    pyqt5模块介绍
    第九章第四节 流体压强与流速的关系
    开源的推荐系统
    VNote: 一个舒适的Markdown笔记软件
    jira项目管理平台搭建
    Win10环境下,告别MarkdownPad,用Notepad++搭建编写md文档的环境
  • 原文地址:https://www.cnblogs.com/qubo520/p/6955815.html
Copyright © 2011-2022 走看看