⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise5_21.java

📁 Introduction to java programming 一书中所有编程练习部分的源码
💻 JAVA
字号:
public class Exercise5_21 {  /** Main method */  public static void main(String args[]) {    // Declare, create, and initialized array    double[][] workHours = {      {2, 4, 3, 4, 5, 8, 8},      {7, 3, 4, 3, 3, 4, 4},      {3, 3, 4, 3, 3, 2, 2},      {9, 3, 4, 7, 3, 4, 1},      {3, 5, 4, 3, 6, 3, 8},      {3, 4, 4, 6, 3, 4, 4},      {3, 7, 4, 8, 3, 8, 4},      {6, 3, 5, 9, 2, 7, 9}};    // Create an array to store total weekly hours    int[] weeklyHours = new int[workHours.length];    for (int i = 0; i < workHours.length; i++)      for (int j = 0; j < workHours[i].length; j++)        weeklyHours[i] += workHours[i][j];    int[] indexList = new int[weeklyHours.length];    // Sort weeklyHours    sortAndKeepIndex(weeklyHours, indexList);    // Display result    for (int i = weeklyHours.length - 1; i >= 0; i--)      System.out.println("Employee " + indexList[i] + ": " +        weeklyHours[i]);  }  /** The method for sorting the numbers */  static void sortAndKeepIndex(int[] list, int[] indexList) {    int currentMax;    int currentMaxIndex;    // Initialize indexList    for (int i = 0; i < indexList.length; i++)      indexList[i] = i;    for (int i = list.length - 1; i >= 1; i--) {      // Find the maximum in the list[0..i]      currentMax = list[i];      currentMaxIndex = i;      for (int j = i - 1; j >= 0; j--) {        if (currentMax < list[j]) {          currentMax = list[j];          currentMaxIndex = j;        }      }      // Swap list[i] with list[currentMaxIndex] if necessary;      if (currentMaxIndex != i) {        list[currentMaxIndex] = list[i];        list[i] = currentMax;        // Swap the index in indexList too        int temp = indexList[i];        indexList[i] = indexList[currentMaxIndex];        indexList[currentMaxIndex] = temp;      }    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -