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());//又是可恶的重画!!!!!!!!!!!!!
}

homework09-虐心的现程设终于要告一段落了的更多相关文章

  1. 如何在MAC下安装Myeclipse2015真的很虐心!!!!!!!!!!

    最近笔者换了一个新的笔记本,mac pro 因为同学说大公司的web开发都是写在Linux操作系统下的,所以我决定搞一个,但是装软件就是很困难啊!找了大量资料发现都不全,最后,终于搞好了,分享给同样虐 ...

  2. Python基础(十三) 为什么说python多线程没有真正实现多现程

    Python中的多线程没有真正实现多现程! 为什么这么说,我们了解一个概念,全局解释器锁(GIL). Python代码的执行由Python虚拟机(解释器)来控制. Python在设计之初就考虑要在主循 ...

  3. 程设刷题 | 程序设计实践II-2017(部分)

    目录 1165-算术题 题目描述 代码实现 1184-Tourist 1 题目描述 代码实现 1186-Tourist 2 题目描述 代码实现 1224-LOVE 题目描述 代码实现 1256-湘潭大 ...

  4. 程设大作业xjb写——魔方复原

    鸽了那么久总算期中过[爆]去[炸]了...该是时候写写大作业了 [总不能丢给他们不会写的来做吧 一.三阶魔方的几个基本定义 ↑就像这样,可以定义面的称呼:上U下D左L右R前F后B UD之间的叫E,LR ...

  5. TYVJ P1051 选课 Label:多叉转二叉&&树形dp(虐心♥)

    描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修了这M门课并考核通过就能获得 ...

  6. POJ 1170 Shopping Offers -- 动态规划(虐心的六重循环啊!!!)

    题目地址:http://poj.org/problem?id=1170 Description In a shop each kind of product has a price. For exam ...

  7. DM8168 环境搭建(2) ------ 虐心之旅

    续上  ... ... ... (5)安装minicom minicom类似于windows下的超级终端,用于与串口设备通信    参考命令:sudo apt-get install minicom ...

  8. 程设刷题 | 编译C++文件出现to_string is not a member of std 或者 to_string was not declared in this scope的解决方法

    写在前面 原文链接:Enabling string conversion functions in MinGW C++在将整型.浮点型.长整型等数据类型转换为字符串时,可使用<string> ...

  9. C语言------程设设计入门

    仅供借鉴.仅供借鉴.仅供借鉴(整理了一下大一C语言每个章节的练习题.没得题目.只有程序了) 文章目录 1:程序设计入门 2 .实训目的及要求 3.代码实验(包含运行结果) 4 .实验总结 1:程序设计 ...

随机推荐

  1. makefile使用

    linux make手册:http://www.gnu.org/software/make/manual/make.html 一篇文章: 假设我们有一个程序由5个文件组成,源代码如下:/*main.c ...

  2. Linux进程调度和切换过程分析

    内容: (1):从schedule()开始,几种不同类型的进程之间的调度选择;在相同类型的进程之间的调度选择算法 (2):从CPU的IP值的变化上,说明在switch_to宏执行后,执行分析 (3): ...

  3. CMMI 配置管理

    配置库的相关知识 通过建立物理配置库的设立规范.各配置库目录的设立原则,确保配置库的统一与规范,确保项目产品得到有效的管理与运用,提高资源的共享与利用:通过 变更管理活动,保证产品的完整.正确.一致, ...

  4. Android L 使用ART能提高多少性能?

    点击打开链接 刚刚结束的 Google I/O 大会上,Android 下一代操作系统「L」带来不少惊喜.新系统运行更快.更省电. 然而开发者对这个新系统也有颇多疑问,比如新的运行模式 ART 对开发 ...

  5. Hearthstone-Deck-Tracker汉化处理技巧

    https://github.com/chucklu/Hearthstone-Deck-Tracker 首先本地需要有自己的远端chucklu以及作者的远端epix37 $ git remote -v ...

  6. tomcat web.xml配置

    关于Tomcat 中 web.xml 文件的配置问题: 1.下面的配置是合法的     <servlet>        <servlet-name>test</serv ...

  7. python - 回溯继承树 - 自己实现

    # -*- coding: utf-8 -*- class test(object): pass class test1(test): pass class test2(test1): pass pr ...

  8. poj 2553 The Bottom of a Graph

    求解的是有向图中满足“自己可达的顶点都能到达自己”的顶点个数如果强连通分量中某个顶点,还能到达分量外的顶点,则该连通分量不满足要求// 因此,本题要求的是将强连通分量缩点后所构造的新图中出度为0的顶点 ...

  9. 《C++ Primer 4th》读书笔记 第8章-标准IO库

    原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3936457.html

  10. 自己动手写路由器之ioctl获取网络接口信息

    最近打算写一个简单路由器,里面有用到ioctl获取网络接口信息,那就先把这部分单独拿出来说一说吧! ioctl这个函数,可以用来对特殊文件的基础设备参数进行操作,它们可以完成与打开文件描述符相关联的控 ...