任务时间表.docx
- 文档编号:12273392
- 上传时间:2023-04-17
- 格式:DOCX
- 页数:28
- 大小:1.83MB
任务时间表.docx
《任务时间表.docx》由会员分享,可在线阅读,更多相关《任务时间表.docx(28页珍藏版)》请在冰豆网上搜索。
任务时间表
Microsoft
任务时间表问题
[键入文档副标题]
Windows用户
[选取日期]
[在此处键入文档摘要。
摘要通常为文档内容的简短概括。
在此处键入文档摘要。
摘要通常为文档内容的简短概括。
]
package任务时间表问题;
publicclassGreedyjob{
publicstaticintgreedyJob(int[]d,int[]w,int[]job)
{
intn=d.length-1;
d[0]=0;
job[0]=0;
intk=1;
job[1]=1;
for(inti=2;i<=n;i++)
{
intr=k;
while((d[job[r]])>d[i]&&(d[job[r]]!
=r))r--;
if((d[job[r]]<=d[i])&&(d[i]>r)){
for(intm=k;m>r;m--)
job[m+1]=job[m];
job[r+1]=i;
k++;
}
}
for(inti=1;i<=k;i++)
w[job[i]]=0;
intsum=0;
for(inti=1;i<=n;i++)
if(w[i]>0){
job[++k]=i;
sum+=w[i];
}
returnsum;
}
publicstaticintfasterJob(intd[],intw[],int[]job,ints[])
//核心算法
//d[]为期限数组,w[]为延迟惩罚数组,job[]为所选及时任务数组,s[]为任务安排顺序
{
intn=d.length-1;
int[]f=newint[n+1];
for(inti=0;i<=n;i++)f[i]=i;
FastUnionFindU=newFastUnionFind(n);//创建并差集
intk=0,t=0;
for(inti=1;i<=n;i++)
{
intm=(n U.Find(n): U.Find(d[i]);//选取任务所在等价类,即任务可放置位置 if(f[m]>0)//任务可放入及时任务中 { k=k+1; job[k]=i; s[m]=i;//记录及时任务放置位置 if(f[m]>1){//同等价类的任务(同期限的任务)还有放置空间,合并等价类,确定下一等价类任务的放置位置 t=U.Find(f[m]-1); U.Union(t,m); } elset=0;//同等价类的任务(同期限的任务)已无放置空间,下一等价类任务将为非及时任务 f[m]=f[t]; } } for(inti=1;i<=k;i++) w[job[i]]=0;//及时任务惩罚清零 intsum=0; for(inti=1;i<=n;i++) if(w[i]>0){ sum+=w[i]; for(intj=1;j<=n;j++) if(s[j]==0){s[j]=i;break;}//将非及时任务加入安排队列中 } for(inti=1;i<=n;i++) System.out.print(s[i]); System.out.println(); returnsum;//返回最小惩罚数 } } publicclassFastUnionFind{//并差集 privateintparent[];//父节点 intsize;//并差集大小 publicFastUnionFind(ints) { size=s; parent=newint[size+1]; for(inti=0;i<=size;i++) parent[i]=-1;//用负数代表集合元素个数 } publicintFind(intx)//查找元素所在的等价类所在树的根节点 { intp=x; while(parent[p]>0) p=parent[p]; while(x! =p) { inttemp=parent[x]; parent[x]=p; x=temp; } returnx; } publicvoidUnion(introot1,introot2)//将量元素集合并到同一并差集下(同一树下) { inttemp=parent[root1]+parent[root2];//孩子结点个数 if(root2 { parent[root1]=root2; parent[root2]=temp; } else { parent[root2]=root1; parent[root1]=temp; } } } publicclassSort{//合并排序类 publicvoidmergeSort(workwo[]){//对work数组按延期惩罚由大到小排列 ints=1; while(s { mergePass(wo,s); s+=s; } } publicvoidmergePass(workwo[],ints) { inti=1; while(i { merge(wo,i,i+s-1,i+2*s-1); i=i+2*s; } if(i+s merge(wo,i,i+s-1,wo.length-1); } publicvoidmerge(workwo[],intl,intm,intr) { inti=l; intj=m+1; while(i { if(wo[i].compareTo(wo[j])==-1) { workk=wo[j]; for(intn=j-1;n>=i;n--) { wo[n+1]=wo[n]; } wo[i]=k; } i++; } } } importjavax.swing.*; importjava.util.*; publicclassInitialTree{//窗口中的年月树 publicstaticJTreeInitial(){ String[][]y=newString[5][12]; for(inti=0;i<=4;i++) for(intj=0;j<=11;j++) y[i][j]=(2012+i)+"年"+(j+1)+"月";//子(月份)结点集 Hashtablehastable=newHashtable(); for(inti=0;i<=4;i++) hastable.put((2012+i)+"年",y[i]);//添加结点 JTreetree=newJTree(hastable); tree.setSize(300,600); returntree; } } publicclassworkimplementsComparable{//任物类 publicStringname;//任务名称 publicintwast;//延迟惩罚 publicintdeathline;//任务期限 publicStringmonth;//任务所属年月份 publicwork(Stringname,Stringmonth,intdeathline,intwast) { this.name=name; this.month=month; this.deathline=deathline; this.wast=wast; } publicintcompareTo(Objectx)//任务延迟惩罚比较 { intxt=((work)x).wast; if(wast if(wast==xt)return0; elsereturn1; } } importjavax.swing.*; importjava.awt.*; importjava.awt.event.*; importjava.sql.*; importjavax.swing.tree.*; importjavax.swing.event.*; //创建简单窗口,动态显示任务时间表问题 publicclassTimeThingextendsJFrameimplementsActionListener,TreeSelectionListener,Runnable { publicJDesktopPanedesktopPane=newJDesktopPane();//桌面 JInternalFramejnew2=newJInternalFrame("完成顺序",true,true,true,true);//动态显示页面 privateStatementstmt1;//用于数据库查询 privateResultSetrs1; workww[];//任务数组 publicTimeThing(workww[],JDesktopPanedesktopPane,JInternalFramejnew2){ //此构造方法用于创建线程,经行任务动态选择 this.ww=ww; this.desktopPane=desktopPane; this.jnew2=jnew2; } publicTimeThing()//创建窗口 { super("任务时间表"); JMenuBarbar=newJMenuBar(); JPanelp1=newJPanel();p1.setMaximumSize(newDimension(300,600)); JScrollPanescrollPane=newJScrollPane(); JTreetree=InitialTree.Initial();//生成年月树 tree.addTreeSelectionListener(this);//TreeSelectionEvent监听 scrollPane.setViewportView(tree); p1.setLayout(newBorderLayout()); p1.add(newJLabel("年中任务"),BorderLayout.NORTH); p1.add(scrollPane,BorderLayout.CENTER); bar.setOpaque(true); JMenumfile=buildFileMenu();//创建菜单列表 bar.add(mfile); setJMenuBar(bar); ContainercontentPane=getContentPane(); BoxbaseBox=Box.createHorizontalBox();//水平Box布局 contentPane.add(baseBox); BoxvBox=Box.createVerticalBox(); vBox.add(p1);//年月树 BoxvBox1=Box.createVerticalBox(); desktopPane.setAlignmentX(Component.CENTER_ALIGNMENT); desktopPane.setMaximumSize(newDimension(1500,600)); vBox1.add(desktopPane);//桌面 desktopPane.setBackground(Color.gray); baseBox.add(vBox); baseBox.add(vBox1); setSize(950,600); setVisible(true); setLocationRelativeTo(null); } publicJMenubuildFileMenu(){ //开始菜单 JMenuthefile=newJMenu("开始"); JMenuItemnewf=newJMenuItem("New");//新建一个任务安排 JMenuItemquit=newJMenuItem("Exit"); thefile.add(newf); thefile.addSeparator(); thefile.add(quit); newf.addActionListener(this); quit.addActionListener(newActionListener(){ publicvoidactionPerformed(ActionEvente) { System.exit(0); } }); returnthefile; } publicvoidactionPerformed(ActionEvente) {//新建任务信息窗口 if(e.getActionCommand().equals("New")){ finalJInternalFramejnew=newJInternalFrame("new",true,true,true,true); JLabelj=newJLabel("年月"); JLabelj1=newJLabel("任务数"); JButtonb=newJButton("确定"); finalJTextFieldt2=newJTextField(10); finalJTextFieldt1=newJTextField(10); jnew.setLayout(newFlowLayout()); jnew.add(j);jnew.add(t1); jnew.add(j1);jnew.add(t2); jnew.add(b); b.addActionListener(newActionListener(){ //输入任务名,期限,惩罚窗口 publicvoidactionPerformed(ActionEventex){ finalStringym=t1.getText(); finalStringn=t2.getText(); jnew.setVisible(false); finalJInternalFramejnew1=newJInternalFrame("new",true,true,true,true); jnew1.setLayout(newGridLayout(4,Integer.parseInt(n)+1)); finalJTextFieldtt[][]=newJTextField[3][Integer.parseInt(n)+1]; for(inti=0;i<=2;i++) for(intj=0;j<=Integer.parseInt(n);j++) { tt[i][j]=newJTextField();jnew1.add(tt[i][j]); } tt[0][0].setText("任务");tt[0][0].setEditable(false); tt[1][0].setText("截止日期");tt[1][0].setEditable(false); tt[2][0].setText("延期惩罚");tt[2][0].setEditable(false); for(inti=0;i jnew1.add(newJLabel()); JButtonb=newJButton("OK"); jnew1.add(b); b.addActionListener(newActionListener(){ //读取任务信息 publicvoidactionPerformed(ActionEventes) { //形成期限数组和惩罚数组 intd[]=newint[Integer.parseInt(n)+1]; intw[]=newint[Integer.parseInt(n)+1]; workwo[]=newwork[Integer.parseInt(n)+1]; ww=newwork[Integer.parseInt(n)+1]; intjob[]=newint[d.length]; for(inti=1;i<=Integer.parseInt(n);i++) { wo[i]=newwork(tt[0][i].getText(),ym,Integer.parseInt(tt[1][i].getText()),Integer.parseInt(tt[2][i].getText())); ww[i]=wo[i]; } newSort().mergeSort(wo);//任务按惩罚力度排序 for(inti=1;i<=w.length-1;i++) { w[i]=wo[i].wast; d[i]=wo[i].deathline; } for(inti=1;i<=w.length-1;i++) { System.out.print(wo[i].wast+""); System.out.println(wo[i].deathline); } ints[]=newint[d.length]; intsum=Greedyjob.fasterJob(d,w,job,s);//及时任务选择与安排 System.out.println(sum); jnew1.setVisible(false); jnew2=newJInternalFrame("完成顺序",true,true,true,true); jnew2.setLayout(newGridLayout(4,Integer.parseInt(n)+1)); finalJTextFieldtt1[][]=newJTextField[3][Integer.parseInt(n)+1]; for(inti=0;i<=2;i++) for(intj=0;j<=Integer.parseInt(n);j++) { tt1[i][j]=newJTextField(); tt1[i][j].setEnabled(false); jnew2.add(tt1[i][j]); } tt1[0][0].setText("任务"); tt1[1][0].setText("截止日期"); tt1[2][0].setText("延期惩罚"); for(intj=1;j<=Integer.parseInt(n);j++) { tt1[0][j].setText(wo[s[j]].name); tt1[1][j].setText(wo[s[j]].deathline+""); tt1[2][j].setText(wo[s[j]].wast+""); } //显示及时任务 for(intj=1;j<=Integer.parseInt(n);j++) { if(job[j]! =0) for(inti=1;i<=Integer.parseInt(n);i++) if(Integer.parseInt(tt1[0][i].getText())==job[j]) { tt1[0][i].setBackground(Color.blue); tt1[1][i].setBackground(Color.blue); tt1[2][i].setBackground(Color.blue); } } //显示结果 for(inti=1;i<=Integer.parseInt(n);i++) {if(job[i]! =0) { if(i==1) jnew2.add(newJLabel("选取过程: "+wo[job[i]].name+"----->")); else jnew2.add(newJLabel(wo[job[i]].name+"------>")); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 任务 时间表
![提示](https://static.bdocx.com/images/bang_tan.gif)