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. Androidz之Activity概要学习

    Androidz之Activity概要学习 1.     Activity类概述 Activity(活动)是一个单独的.能获取焦点的,且能与用户交互的东西.所以我们通常在Activity类中的onCr ...

  2. Ext2.0之Tabpanel AJAX远程加载多标签页面模式开发技巧

    目前开发的方式是采用远程load页面来实现多页面效果,类似于126邮箱多标签页效果.但是比126邮箱的方式更好,因为页面打开后是load到本地的,126似乎还会重新请求.在近期项目该开发方式已经基本成 ...

  3. 转:Android 设置屏幕不待机

    本文转载于:http://blog.csdn.net/yudajun/article/details/7748760 Android设置支部待机有两种方法 第一种简单通过设置WindowManager ...

  4. Android系列之Fragment(一)----Fragment加载到Activity当中

    Android上 的界面展示都是通过Activity实现的,Activity实在是太常用了.但是Activity也有它的局限性,同样的界面在手机上显示可能很好看, 在平板上就未必了,因为平板的屏幕非常 ...

  5. Go Deeper(2010成都现场赛题)(2-sat)

    G - Go Deeper Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description ...

  6. pinyin4j使用示例

    pinyin4j的主页:http://pinyin4j.sourceforge.net/pinyin4j能够根据中文字符获取其对应的拼音,而且拼音的格式可以定制pinyin4j是一个支持将中文转换到拼 ...

  7. eclipse无法与手机连上的解决方案

    在eclipse上开发android应用,有时候会遇到eclipse无法识别手机的问题,就算把数据线拔了又插,插了又拔,哪怕是重启eclipse 甚至是重启电脑,这个问题也依然是解决不了.这时候就非常 ...

  8. eclipse启动出现“An Error has Occurred. See the log file”解决方法

    最近在启动eclipse时出现了“An Error has Occurred. See the log file”的错误,点击确定后也不能启动eclipse.查看log文件,出现类似: java.la ...

  9. Android 实现emoji表情的demo

    Android 实现emoji表情的例子,网上看到的,记录一下. 请看下图 : 项目下载地址:http://download.csdn.net/detail/abc13939746593/741397 ...

  10. ASP.NET服务器控件对应的HTML标签

    了解ASP.NET的控件最终解析成什么HTML代码,有助于我们对ASP.NET更深的了解,在使用JS交互时也知道如何操作. ASP.NET 服务器控件渲染到客户端之后对应的HTML标签讲解. labe ...