程序填空题:计算每个雇员每周工作的小时数并排序
假定所有雇员每周工作的小时数存储在一个二维数组中。1行包含7列,记录了一个雇员7天的工作小时数。编写一个程序,按照总工时降序的方式显示雇员和他们的总工时。
Java
public class Main {
/** 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] += ;
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] + ": " +
);
}
/** The method for sorting the numbers */
static void sortAndKeepIndex(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 = ;
for (int j = i - 1; j >= 0; j--) {
if (currentMax < list[j]) {
currentMax = ;
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[currentMaxIndex];
indexList[currentMaxIndex] = temp;
}
}
}
}
答案:
第1空:workHours[i][j]
第2空:weeklyHours[i]
第3空:list
第4空: int[]
第5空:i
第6空:list[j]
第7空:indexList[i]
第8空: indexList[i]
Java
public class Main {
/** 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] += ;
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] + ": " +
);
}
/** The method for sorting the numbers */
static void sortAndKeepIndex(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 = ;
for (int j = i - 1; j >= 0; j--) {
if (currentMax < list[j]) {
currentMax = ;
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[currentMaxIndex];
indexList[currentMaxIndex] = temp;
}
}
}
}
答案:
第1空:workHours[i][j]
第2空:weeklyHours[i]
第3空:list
第4空: int[]
第5空:i
第6空:list[j]
第7空:indexList[i]
第8空: indexList[i]