V3.0版本今天凌晨出炉
添加了随机生成
添加了文件打开
完全按照老师的要求搞定了
V2.0版本更新
添加了中间数组变量显示
这次作业写了整整一天,把以前能用的代码都改了一个遍
最后变成了网页版的小程序
这是由第三次作业的小程序改编而成
添加了自动执行
下一步
上一步
还有一个textarea文本区域来显示结果和过程信息
200行代码。。
import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class frame extends JApplet implements ActionListener{ public static int count=0; int b[][]=new int[6][6]; int c[][]=new int[100][100]; public int list[]=new int[6]; private FlowLayout fl=new FlowLayout(FlowLayout.CENTER); private JButton bt=new JButton("最终结果"); private JButton btsleep=new JButton("自动执行"); private JButton bnext=new JButton("下一步"); private JButton bpre=new JButton("上一步"); private JLabel lbx=new JLabel("x长度"); private JLabel lby=new JLabel("y长度"); private JTextField tfx=new JTextField(2); private JTextField tfy=new JTextField(2); private JPanel jpl=new JPanel(); private JPanel jplb=new JPanel(); private JPanel arr=new JPanel(); private JTextField[][] a=new JTextField[6][6]; private JTextArea ta=new JTextArea(10,20); public frame() { jplb.add(bt); jplb.add(btsleep); jplb.add(bnext); jplb.add(bpre); jpl.add(lbx); jpl.add(tfx); jpl.add(lby); jpl.add(tfy); ta.setBackground(Color.yellow); ta.setText("此处显示结果"); arr.setLayout(new GridLayout(6,6)); for(int i=0;i<6;i++) for(int j=0;j<6;j++) { a[i][j]=new JTextField(2); arr.add(a[i][j]); } jpl.setSize(50,20); jplb.setSize(50,20); bt.addActionListener(this); bnext.addActionListener(this); bpre.addActionListener(this); btsleep.addActionListener(this); this.add(jplb); this.add(jpl); this.add(arr); this.add(ta); this.setLayout(fl); this.setSize(300,300); this.setVisible(true); } public void actionPerformed(ActionEvent e){ int x=Integer.parseInt(tfx.getText()); int y=Integer.parseInt(tfy.getText()); for(int i=0;i<y;i++) { for(int j=0;j<x;j++) { b[i][j]=Integer.parseInt(a[i][j].getText()); } }// 从TextFiled中得到数组 if(e.getSource().equals(bt)) { int l= find(x,y,b); JOptionPane.showMessageDialog( this ,l); } //* else if(e.getSource().equals(btsleep)) { int temp[][]=new int[6][6]; for(int i=0;i<6;i++) for(int j=0;j<6;j++) { temp[i][j]=b[i][j]; } for(int i=0;i<y;i++) { count++; ta.setText(find(Integer.parseInt(tfx.getText()),count,temp)+"\n"+"当前计算前"+ count +"行的数据"+"\n"+"矩阵区域为第"+"\n"+(list[1]+1)+"到"+(list[2]+1)+"列"+"\n"+findm(count)+"到"+list[5]+"行"); ta.paintImmediately(getBounds()); for(int j=0;j<6;j++) for(int k=0;k<6;k++) { a[k][j].setBackground(Color.white); a[k][j].paintImmediately(getBounds()); } for(int j=list[1];j<list[2]+1;j++) for(int k=list[4]-1;k<list[5];k++) { a[k][j].setBackground(Color.yellow); a[k][j].paintImmediately(getBounds()); } try {Thread.sleep(2000); }catch(InterruptedException error) { } } } else if(e.getSource().equals(bnext)) { count++; int temp[][]=new int[6][6]; for(int i=0;i<count;i++) for(int j=0;j<6;j++) { temp[i][j]=b[i][j]; } ta.setText(find(Integer.parseInt(tfx.getText()),count,temp)+"\n"+"当前计算前"+ count +"行的数据"+"\n"+"矩阵区域为第"+"\n"+(list[1]+1)+"到"+(list[2]+1)+"列"+"\n"+findm(count)+"到"+list[5]+"行"); for(int j=0;j<6;j++) for(int i=0;i<6;i++) a[i][j].setBackground(Color.white); for(int j=list[1];j<list[2]+1;j++) for(int i=list[4]-1;i<list[5];i++) a[i][j].setBackground(Color.yellow); } else if(e.getSource().equals(bpre)) { count--; if(count<=0) { ta.setText("ERROR"); for(int j=0;j<6;j++) for(int i=0;i<6;i++) a[i][j].setBackground(Color.white); } else{ int temp[][]=new int[6][6]; for(int i=0;i<=count;i++) for(int j=0;j<6;j++) { temp[i][j]=b[i][j]; } ta.setText(find(Integer.parseInt(tfx.getText()),count,temp)+"\n"+"当前计算前"+ count +"行的数据"+"\n"+"矩阵区域为第"+"\n"+(list[1]+1)+"到"+(list[2]+1)+"列"+"\n"+findm(count)+"到"+list[5]+"行"); for(int j=0;j<6;j++) for(int i=0;i<6;i++) a[i][j].setBackground(Color.white); for(int j=list[1];j<list[2]+1;j++) for(int i=list[4]-1;i<list[5];i++) a[i][j].setBackground(Color.yellow); } } } public void init() { new frame(); } public int find(int x,int y,int b[][]){ int sum,sumb,n,flagfront=0,flaglast=0; int max=-9999; for(int i=0;i<x;i++) { n=0; for(int j=0;j<y;j++) { sum=0; for(int t=j;t>=0;t--) { sum=sum+b[t][i]; c[n][i]=sum; n++; } } } for(int m=0;m<(y*(y+1)/2);m++) for(int i=0;i<x;i++) { for(int j=x-1;j>=i;j--) { sumb=0; for(int k=i;k<=j;k++) sumb=sumb+c[m][k]; if(max<sumb) { max=sumb; flagfront=i; flaglast=j; list[3]=m; System.out.println(list[3]); } } } list[0]=max; list[1]=flagfront; list[2]=flaglast; return max; } public int findm(int y) { int m=list[3]; int rowsub=0; for (int i=0;i<y;i++) if (m>=(i*(i+1)/2) && m<((i+2)*(i+1)/2)) { rowsub=m-i*(i+1)/2; list[4]=i-rowsub+1; list[5]=i+1; } return list[4]; } }
程序测试截图
上图是最终结果按钮显示
2-3列,1-2行,答案追踪无误,特别添加背景变色,更加容易分辨
中间过程截图,运行到第二步
上一步点多了,离开数据范围的错误提示
自动执行按钮按下,自动执行到第三步
代码注释
框架方面的代码,都是些基本的东西
public frame() { jplb.add(bt); jplb.add(btsleep); jplb.add(bnext); jplb.add(bpre); jpl.add(lbx); jpl.add(tfx); jpl.add(lby); jpl.add(tfy); ta.setBackground(Color.yellow); ta.setText("此处显示结果"); arr.setLayout(new GridLayout(6,6)); for(int i=0;i<6;i++) for(int j=0;j<6;j++) { a[i][j]=new JTextField(2); arr.add(a[i][j]); } jpl.setSize(50,20); jplb.setSize(50,20); bt.addActionListener(this); bnext.addActionListener(this); bpre.addActionListener(this); btsleep.addActionListener(this); this.add(jplb); this.add(jpl); this.add(arr); this.add(ta); this.setLayout(fl); this.setSize(300,300); this.setVisible(true); }
四个按钮的监听和事件响应
public void actionPerformed(ActionEvent e){ int x=Integer.parseInt(tfx.getText()); int y=Integer.parseInt(tfy.getText()); for(int i=0;i<y;i++) { for(int j=0;j<x;j++) { b[i][j]=Integer.parseInt(a[i][j].getText()); } }// 从TextFiled中得到数组 if(e.getSource().equals(bt)) { int l= find(x,y,b); JOptionPane.showMessageDialog( this ,l); } //最终结果显示,弹出一个对话框 else if(e.getSource().equals(btsleep)) { //自动执行按钮事件响应 int temp[][]=new int[6][6]; for(int i=0;i<6;i++) for(int j=0;j<6;j++) { temp[i][j]=b[i][j]; } for(int i=0;i<y;i++) { count++; ta.setText(find(Integer.parseInt(tfx.getText()),count,temp)+"\n"+"当前计算前"+ count +"行的数据"+"\n"+"矩阵区域为第"+"\n"+(list[1]+1)+"到"+(list[2]+1)+"列"+"\n"+findm(count)+"到"+list[5]+"行"); ta.paintImmediately(getBounds());//一直找不到为什么textarea无法刷新,百度了1个小时才找到方法 try
//自动执行核心步骤:线程的休眠
{Thread.sleep(2000); }catch(InterruptedException error) { } } } else if(e.getSource().equals(bnext)) { count++; int temp[][]=new int[6][6]; for(int i=0;i<count;i++) for(int j=0;j<6;j++) { temp[i][j]=b[i][j]; } ta.setText(find(Integer.parseInt(tfx.getText()),count,temp)+"\n"+"当前计算前"+ count +"行的数据"+"\n"+"矩阵区域为第"+"\n"+(list[1]+1)+"到"+(list[2]+1)+"列"+"\n"+findm(count)+"到"+list[5]+"行"); } else if(e.getSource().equals(bpre)) { count--; if(count<=0) ta.setText("ERROR"); else{ int temp[][]=new int[6][6]; for(int i=0;i<=count;i++) for(int j=0;j<6;j++) { temp[i][j]=b[i][j]; } ta.setText(find(Integer.parseInt(tfx.getText()),count,temp)+"\n"+"当前计算前"+ count +"行的数据"+"\n"+"矩阵区域为第"+"\n"+(list[1]+1)+"到"+(list[2]+1)+"列"+"\n"+findm(count)+"到"+list[5]+"行"); } } }
这些代码是写的最久的东西了,特别是那个自动执行,sleep能够让控制台自动输出,但是textarea无法刷新,百思不得其解
settext按理来说不应该会不刷新,反正不懂
百度了很久才找到一个叫做paintimmidiatly(getBounds())的函数才解决了这个问题
听说是重画,为什么要重画啊!!!!让我搞了那么久
查找函数,包含行数的答案位置查找,在最后的flagfront和flaglast标记
public int find(int x,int y,int b[][]){ int sum,sumb,n,flagfront=0,flaglast=0; int max=-9999; for(int i=0;i<x;i++) { n=0; for(int j=0;j<y;j++) { sum=0; for(int t=j;t>=0;t--) { sum=sum+b[t][i]; c[n][i]=sum; n++; } } } for(int m=0;m<(y*(y+1)/2);m++) for(int i=0;i<x;i++) { for(int j=x-1;j>=i;j--) { sumb=0; for(int k=i;k<=j;k++) sumb=sumb+c[m][k]; if(max<sumb) { max=sumb; flagfront=i; flaglast=j; list[3]=m; System.out.println(list[3]); } } } list[0]=max; list[1]=flagfront; list[2]=flaglast; return max; }
LIST[]是全局变量
0存储最大值,
1存储行的左边标记,
2是存储行的右边标记,
3是存储转换成的一维矩阵的行数,
4是原始二维矩阵的行的上标记,
5是原始矩阵行的下标记
列的答案位置查找,由于算法是把二维的矩阵压缩为一维,因此有x*(x+1)/2行矩阵,需要设计算法从中找到二维的答案位置
public int findm(int y) { int m=list[3]; int rowsub=0; for (int i=0;i<y;i++) if (m>=(i*(i+1)/2) && m<((i+2)*(i+1)/2)) { rowsub=m-i*(i+1)/2; list[4]=i-rowsub+1; list[5]=i+1; } return list[4]; }
最新添加:为答案背景染色的代码
for(int j=0;j<6;j++) for(int k=0;k<6;k++) { a[k][j].setBackground(Color.white);//先把背景全部染成白色 a[k][j].paintImmediately(getBounds());//可恶的重画!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! } for(int j=list[1];j<list[2]+1;j++) for(int k=list[4]-1;k<list[5];k++) { a[k][j].setBackground(Color.yellow);//再把追溯到的答案变成黄色背景 a[k][j].paintImmediately(getBounds());//又是可恶的重画!!!!!!!!!!!!! }