2.5.12

question:

Scheduling. Write a program SPT.java that reads job names and processing times from standard input and prints a schedule that minimizes average completion time using the shortest processing time first rule, as described on page 349.

answer:

import edu.princeton.cs.algs4.*;
import java.util.Arrays;

public class SPT
{
    private static class Job implements Comparable<Job>
    {
        String name;
        double time;
        
        public Job(String a, double b)
        {
            name = a;
            time = b;
        }
        
        public int compareTo(Job that)
        {
            return (int)(this.time - that.time);
        }
        
        public void show()
        {
            StdOut.println(name + " " + time);
        }
    }
    
    public static void main(String[] args)
    {
        int n = StdIn.readInt();
        Job[] jobs = new Job[n];
        for (int i = 0; i < n; i++) 
        {
            String name = StdIn.readString();
            double time = StdIn.readDouble();
            jobs[i] = new Job(name, time);
        }
        Arrays.sort(jobs);
        for (int i = 0; i < n; i++)
            jobs[i].show();
    }
}

猜你喜欢

转载自www.cnblogs.com/w-j-c/p/9147161.html
今日推荐