1、任务时间表Microsoft任务时间表问题键入文档副标题Windows 用户选取日期在此处键入文档摘要。摘要通常为文档内容的简短概括。在此处键入文档摘要。摘要通常为文档内容的简短概括。package 任务时间表问题;public class Greedyjob public static int greedyJob(int d,int w,int job) int n=d.length-1; d0=0; job0=0; int k =1; job1=1; for(int i=2;idi&(djobr!=r) r-; if(djobrr) for(int m=k;mr;m-) jobm+1=jo
2、bm; jobr+1=i; k+; for(int i=1;i=k;i+) wjobi=0; int sum=0; for(int i=1;i0) job+k=i; sum+=wi; return sum; public static int fasterJob(int d,int w,int job,int s) /核心算法 /d为期限数组 ,w为延迟惩罚数组,job为所选及时任务数组,s为任务安排顺序 int n=d.length-1; int f=new intn+1; for(int i=0;i=n;i+)fi=i; FastUnionFind U=new FastUnionFind(
3、n); /创建并差集 int k=0,t=0; for(int i=1;i=n;i+) int m=(n0)/任务可放入及时任务中 k=k+1; jobk=i; sm=i;/记录及时任务放置位置 if(fm1)/同等价类的任务(同期限的任务)还有放置空间,合并等价类,确定下一等价类任务的放置位置 t=U.Find(fm-1); U.Union(t, m); else t=0;/同等价类的任务(同期限的任务)已无放置空间,下一等价类任务将为非及时任务 fm=ft; for(int i=1;i=k;i+) wjobi=0;/及时任务惩罚清零 int sum=0; for(int i=1;i0) s
4、um+=wi; for(int j=1;j=n;j+) if(sj=0) sj=i;break; /将非及时任务加入安排队列中 for(int i=1;i=n;i+) System.out.print(si); System.out.println(); return sum;/返回最小惩罚数 public class FastUnionFind /并差集 private int parent; /父节点 int size; /并差集大小 public FastUnionFind(int s) size=s; parent=new int size+1; for(int i=0;i0) p=p
5、arentp; while(x!=p) int temp=parentx; parentx=p; x=temp; return x; public void Union(int root1,int root2) /将量元素集合并到同一并差集下 (同一树下) int temp=parentroot1+parentroot2; /孩子结点个数 if(root2root1) parentroot1=root2; parentroot2=temp; else parentroot2=root1; parentroot1=temp; public class Sort /合并排序类 public voi
6、d mergeSort(work wo) /对work数组按延期惩罚由大到小排列 int s=1; while(swo.length) mergePass(wo,s); s+=s; public void mergePass(work wo,int s) int i=1; while(iwo.length-2*s) merge(wo,i,i+s-1,i+2*s-1); i=i+2*s; if(i+swo.length) merge(wo,i,i+s-1,wo.length-1); public void merge(work wo,int l,int m,int r) int i=l; int
7、 j=m+1; while(ij&j=i;n-) won+1=won; woi=k; i+; import javax.swing.*;import java.util.*;public class InitialTree /窗口中的年月树 public static JTree Initial() String y=new String512; for(int i=0;i=4;i+) for(int j=0;j=11;j+) yij=(2012+i)+年+(j+1)+月;/子(月份)结点集 Hashtable hastable=new Hashtable(); for(int i=0;i=4
8、;i+) hastable.put(2012+i)+年,yi);/添加结点 JTree tree=new JTree(hastable); tree.setSize(300, 600); return tree; public class work implements Comparable /任物类 public String name; /任务名称 public int wast; /延迟惩罚 public int deathline; /任务期限 public String month; /任务所属年月份 public work(String name, String month,int
9、 deathline,int wast) this.name=name; this.month=month; this.deathline=deathline; this.wast=wast; public int compareTo(Object x)/任务延迟惩罚比较 int xt=(work)x).wast; if(wastxt) return -1; if(wast=xt) return 0; else return 1; import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.sql.*;i
10、mport javax.swing.tree.*;import javax.swing.event.*;/创建简单窗口,动态显示任务时间表问题public class TimeThing extends JFrame implements ActionListener,TreeSelectionListener,Runnable public JDesktopPane desktopPane=new JDesktopPane();/桌面 JInternalFrame jnew2=new JInternalFrame(完成顺序,true,true,true,true);/动态显示页面 priva
11、te Statement stmt1; /用于数据库查询 private ResultSet rs1; work ww;/任务数组 public TimeThing(work ww,JDesktopPane desktopPane,JInternalFrame jnew2) /此构造方法用于创建线程,经行任务动态选择 this.ww=ww; this.desktopPane=desktopPane; this.jnew2=jnew2; public TimeThing()/创建窗口 super(任务时间表); JMenuBar bar=new JMenuBar(); JPanel p1=new
12、 JPanel();p1.setMaximumSize(new Dimension(300,600); JScrollPane scrollPane=new JScrollPane(); JTree tree=InitialTree.Initial();/生成年月树 tree.addTreeSelectionListener(this);/TreeSelectionEvent监听 scrollPane.setViewportView(tree); p1.setLayout(new BorderLayout(); p1.add(new JLabel(年中任务),BorderLayout.NORT
13、H); p1.add(scrollPane,BorderLayout.CENTER); bar.setOpaque(true); JMenu mfile=buildFileMenu();/创建菜单列表 bar.add(mfile); setJMenuBar(bar); Container contentPane=getContentPane(); Box baseBox=Box.createHorizontalBox(); /水平Box布局 contentPane.add(baseBox); Box vBox=Box.createVerticalBox(); vBox.add(p1);/年月树
14、 Box vBox1=Box.createVerticalBox(); desktopPane.setAlignmentX(Component.CENTER_ALIGNMENT); desktopPane.setMaximumSize(new Dimension(1500,600); vBox1.add(desktopPane);/桌面 desktopPane.setBackground(Color.gray); baseBox.add(vBox); baseBox.add(vBox1); setSize(950,600); setVisible(true); setLocationRelat
15、iveTo(null); public JMenu buildFileMenu() /开始菜单 JMenu thefile= new JMenu(开始); JMenuItem newf=new JMenuItem(New);/新建一个任务安排 JMenuItem quit=new JMenuItem(Exit); thefile.add(newf); thefile.addSeparator(); thefile.add(quit); newf.addActionListener(this); quit.addActionListener(new ActionListener() public
16、 void actionPerformed(ActionEvent e) System.exit(0); ); return thefile; public void actionPerformed(ActionEvent e) /新建任务信息窗口 if(e.getActionCommand().equals(New) final JInternalFrame jnew=new JInternalFrame(new,true,true,true,true); JLabel j=new JLabel(年月); JLabel j1=new JLabel(任务数); JButton b=new JB
17、utton(确定); final JTextField t2=new JTextField(10); final JTextField t1=new JTextField(10); jnew.setLayout(new FlowLayout(); jnew.add(j);jnew.add(t1); jnew.add(j1);jnew.add(t2); jnew.add(b); b.addActionListener(new ActionListener() /输入任务名,期限,惩罚窗口 public void actionPerformed(ActionEvent ex) final Stri
18、ng ym=t1.getText(); final String n=t2.getText(); jnew.setVisible(false); final JInternalFrame jnew1=new JInternalFrame(new,true,true,true,true); jnew1.setLayout(new GridLayout(4,Integer.parseInt(n)+1); final JTextField tt=new JTextField3Integer.parseInt(n)+1; for(int i=0;i=2;i+) for(int j=0;j=Intege
19、r.parseInt(n);j+) ttij=new JTextField();jnew1.add(ttij); tt00.setText(任务);tt00.setEditable(false); tt10.setText(截止日期);tt10.setEditable(false); tt20.setText(延期惩罚);tt20.setEditable(false); for(int i=0;iInteger.parseInt(n);i+) jnew1.add(new JLabel(); JButton b=new JButton(OK); jnew1.add(b); b.addAction
20、Listener(new ActionListener() /读取任务信息 public void actionPerformed(ActionEvent es) /形成期限数组和惩罚数组 int d=new intInteger.parseInt(n)+1; int w=new intInteger.parseInt(n)+1; work wo=new workInteger.parseInt(n)+1; ww=new workInteger.parseInt(n)+1; int job=new intd.length; for(int i=1;i=Integer.parseInt(n);i
21、+) woi=new work(tt0i.getText(),ym,Integer.parseInt(tt1i.getText(),Integer.parseInt(tt2i.getText(); wwi=woi; new Sort().mergeSort(wo);/任务按惩罚力度排序 for(int i=1;i=w.length-1;i+) wi=woi.wast; di=woi.deathline; for(int i=1;i=w.length-1;i+) System.out.print(woi.wast+ ); System.out.println(woi.deathline); in
22、t s=new int d.length; int sum=Greedyjob.fasterJob(d,w,job,s);/及时任务选择与安排 System.out.println(sum); jnew1.setVisible(false); jnew2=new JInternalFrame(完成顺序,true,true,true,true); jnew2.setLayout(new GridLayout(4,Integer.parseInt(n)+1); final JTextField tt1=new JTextField3Integer.parseInt(n)+1; for(int i=
23、0;i=2;i+) for(int j=0;j=Integer.parseInt(n);j+) tt1ij=new JTextField(); tt1ij.setEnabled(false); jnew2.add(tt1ij); tt100.setText(任务); tt110.setText(截止日期); tt120.setText(延期惩罚); for(int j=1;j=Integer.parseInt(n);j+) tt10j.setText(wosj.name); tt11j.setText(wosj.deathline+ ); tt12j.setText(wosj.wast+ );
24、 /显示及时任务 for(int j=1;j=Integer.parseInt(n);j+) if(jobj!=0) for(int i=1;i=Integer.parseInt(n);i+) if(Integer.parseInt(tt10i.getText()=jobj) tt10i.setBackground(Color.blue); tt11i.setBackground(Color.blue); tt12i.setBackground(Color.blue); /显示结果 for(int i=1;i); else jnew2.add(new JLabel(wojobi.name+-);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1