given an initially empty hash table ht with length 7, together with a hash function h(k)=k%7. let us use linear probing to solve collisions. what is the average search length for successful searches after inserting 22, 43, 15 one by one into ht?
时间: 2023-05-01 10:05:35 浏览: 689
给定一个长度为7的初始为空的哈希表ht,哈希函数为h(k)=k%7。我们使用线性探测来解决哈希冲突问题。在依次将22、43、15插入ht后,成功搜索的平均搜索长度是多少?
答:成功搜索的平均搜索长度为1.5。
相关问题
Use java to write a program to display a progress meter as follows. The meter consists of a windowCreate functionality to simulate steps of 10% progress. For this the user repeatedly presses return on the console inside Eclipse (use a Scanner object for this). Every time, the top-most red label is changed to green and the progress label value is incremented by 10 until that value is 100%. Make sure that no errors occur if the user tries to go past 100%. with a column of 11 labels. The first 10 labels indicate progress steps of 10% and are initially red. The bottom label shows the amount of activity remaining as a textual percentage, and is initially set to “0%”.
Sure, here's an example program in Java that implements the progress meter functionality you described:
```java
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.GridLayout;
public class ProgressMeter {
private static final int NUM_STEPS = 10;
private static final String[] STEP_LABELS = {"0%", "10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"};
private static final Color RED = Color.RED;
private static final Color GREEN = Color.GREEN;
private JFrame frame;
private JLabel[] stepLabels;
private JLabel progressLabel;
private int currentStep;
public ProgressMeter() {
frame = new JFrame("Progress Meter");
JPanel panel = new JPanel(new GridLayout(NUM_STEPS + 1, 1));
stepLabels = new JLabel[NUM_STEPS];
for (int i = 0; i < NUM_STEPS; i++) {
JLabel label = new JLabel(STEP_LABELS[i], JLabel.CENTER);
label.setForeground(RED);
panel.add(label);
stepLabels[i] = label;
}
progressLabel = new JLabel("0%", JLabel.CENTER);
panel.add(progressLabel);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
currentStep = 0;
}
public void updateProgress() {
if (currentStep < NUM_STEPS) {
stepLabels[currentStep].setForeground(GREEN);
currentStep++;
progressLabel.setText(currentStep * 10 + "%");
}
}
public static void main(String[] args) {
ProgressMeter meter = new ProgressMeter();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Press enter to simulate progress...");
scanner.nextLine();
meter.updateProgress();
}
}
}
```
This program creates a `ProgressMeter` class that encapsulates the progress meter GUI and functionality. The constructor initializes the GUI with 11 `JLabel` components arranged in a `GridLayout`, with the first 10 labels representing the progress steps and the bottom label representing the progress percentage. The `updateProgress` method updates the GUI by changing the color of the current step label to green, incrementing the current step count, and updating the progress label text. The `main` method creates a new `ProgressMeter` instance and repeatedly waits for the user to press enter before calling the `updateProgress` method to simulate progress.
Many GUI applications have meters that indicate the progress made so far towards completing an activity as a percentage of the anticipated total activity. Write a program to display a progress meter as follows. The meter consists of a window with a column of 11 labels. The first 10 labels indicate progress steps of 10% and are initially red. The bottom label shows the amount of activity remaining as a textual percentage, and is initially set to “0%”. Then create functionality to simulate steps of 10% progress. For this the user repeatedly presses return on the console inside Eclipse (use a Scanner object for this). Every time, the top-most red label is changed to green and the progress label value is incremented by 10 until that value is 100%. Make sure that no errors occur if the user tries to go past 100%.
Sure, here's a sample Java program that implements the progress meter as described:
```java
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.Scanner;
public class ProgressMeter {
private static final int NUM_STEPS = 10;
private static final String TITLE = "Progress Meter";
private static final int WIDTH = 200;
private static final int HEIGHT = 300;
private static final Color RED = Color.RED;
private static final Color GREEN = Color.GREEN;
private static final String REMAINING_LABEL = "Remaining: ";
private JFrame frame;
private JLabel[] stepLabels;
private JLabel remainingLabel;
private int currentStep;
public ProgressMeter() {
frame = new JFrame(TITLE);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(NUM_STEPS + 1, 1));
stepLabels = new JLabel[NUM_STEPS];
for (int i = 0; i < NUM_STEPS; i++) {
stepLabels[i] = new JLabel(i*10 + "%", JLabel.CENTER);
stepLabels[i].setOpaque(true);
stepLabels[i].setBackground(RED);
frame.add(stepLabels[i]);
}
remainingLabel = new JLabel(REMAINING_LABEL + "0%", JLabel.CENTER);
frame.add(remainingLabel);
currentStep = 0;
}
public void setVisible(boolean visible) {
frame.setVisible(visible);
}
public void simulateProgress() {
Scanner scanner = new Scanner(System.in);
while (currentStep < NUM_STEPS) {
String input = scanner.nextLine();
if (input.equals("")) {
currentStep++;
stepLabels[currentStep - 1].setBackground(GREEN);
remainingLabel.setText(REMAINING_LABEL + currentStep*10 + "%");
}
if (currentStep == NUM_STEPS) {
System.out.println("Progress completed.");
}
}
}
public static void main(String[] args) {
ProgressMeter pm = new ProgressMeter();
pm.setVisible(true);
pm.simulateProgress();
}
}
```
The program creates a `JFrame` with a `GridLayout` containing 11 `JLabel` components, where the first 10 labels are the progress steps of 10% and the bottom label shows the remaining percentage. The labels are initialized to red, and as the user presses the enter key, the top-most red label is changed to green and the remaining percentage label is updated accordingly. The program uses a `Scanner` object to read user input from the console inside Eclipse. The program terminates when the progress reaches 100%.
阅读全文