homework09-虐心的现程设终于要告一段落了
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-虐心的现程设终于要告一段落了的更多相关文章
- 如何在MAC下安装Myeclipse2015真的很虐心!!!!!!!!!!
最近笔者换了一个新的笔记本,mac pro 因为同学说大公司的web开发都是写在Linux操作系统下的,所以我决定搞一个,但是装软件就是很困难啊!找了大量资料发现都不全,最后,终于搞好了,分享给同样虐 ...
- Python基础(十三) 为什么说python多线程没有真正实现多现程
Python中的多线程没有真正实现多现程! 为什么这么说,我们了解一个概念,全局解释器锁(GIL). Python代码的执行由Python虚拟机(解释器)来控制. Python在设计之初就考虑要在主循 ...
- 程设刷题 | 程序设计实践II-2017(部分)
目录 1165-算术题 题目描述 代码实现 1184-Tourist 1 题目描述 代码实现 1186-Tourist 2 题目描述 代码实现 1224-LOVE 题目描述 代码实现 1256-湘潭大 ...
- 程设大作业xjb写——魔方复原
鸽了那么久总算期中过[爆]去[炸]了...该是时候写写大作业了 [总不能丢给他们不会写的来做吧 一.三阶魔方的几个基本定义 ↑就像这样,可以定义面的称呼:上U下D左L右R前F后B UD之间的叫E,LR ...
- TYVJ P1051 选课 Label:多叉转二叉&&树形dp(虐心♥)
描述 学校实行学分制.每门的必修课都有固定的学分,同时还必须获得相应的选修课程学分.学校开设了N(N<300)门的选修课程,每个学生可选课程的数量M是给定的.学生选修了这M门课并考核通过就能获得 ...
- POJ 1170 Shopping Offers -- 动态规划(虐心的六重循环啊!!!)
题目地址:http://poj.org/problem?id=1170 Description In a shop each kind of product has a price. For exam ...
- DM8168 环境搭建(2) ------ 虐心之旅
续上 ... ... ... (5)安装minicom minicom类似于windows下的超级终端,用于与串口设备通信 参考命令:sudo apt-get install minicom ...
- 程设刷题 | 编译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> ...
- C语言------程设设计入门
仅供借鉴.仅供借鉴.仅供借鉴(整理了一下大一C语言每个章节的练习题.没得题目.只有程序了) 文章目录 1:程序设计入门 2 .实训目的及要求 3.代码实验(包含运行结果) 4 .实验总结 1:程序设计 ...
随机推荐
- [转]useradd 与adduser的区别
转自:Deit_Aaron的专栏 添加用户:useradd -m 用户名 然后设置密码 passwd 用户名 删除用户:userdel -r 用户名 1. 在root权限下,useradd只是 ...
- GridLayoutManager
GridLayoutManager Class Overview A RecyclerView.LayoutManager implementations that lays out items in ...
- Image.FrameDimensionsList 属性备注
Image.FrameDimensionsList 属性 .NET Framework 2.0 获取 GUID 的数组,这些 GUID 表示此 Image 中帧的维数. 命名空间:System.D ...
- (转)博弈问题与SG函数
博弈问题若你想仔细学习博弈论,我强烈推荐加利福尼亚大学的Thomas S. Ferguson教授精心撰写并免费提供的这份教材,它使我受益太多.(如果你的英文水平不足以阅读它,我只能说,恐怕你还没到需要 ...
- bzoj2245: [SDOI2011]工作安排
费用流. 这道题的模型比较明显,拆点也是很容易看出来的. #include<cstdio> #include<algorithm> #include<cstring> ...
- ActivityManager: Warning: Activity not started, its current task has been brought to the front 的的问题
运行android程序的时候提示:ActivityManager: Warning: Activity not started, its current task has been brought t ...
- Builder模式在Java中的应用(转)
在设计模式中对Builder模式的定义是用于构建复杂对象的一种模式,所构建的对象往往需要多步初始化或赋值才能完成.那么,在实际的开发过程中,我们哪些地方适合用到Builder模式呢?其中使用Build ...
- 让你的 Node.js 应用跑得更快的 10 个技巧(转)
Node.js 受益于它的事件驱动和异步的特征,已经很快了.但是,在现代网络中只是快是不行的.如果你打算用 Node.js 开发你的下一个Web 应用的话,那么你就应该无所不用其极,让你的应用更快,异 ...
- 【C#学习笔记】播放wav文件
using System; using System.Media; namespace ConsoleApplication { class Program { static void Main(st ...
- android的R.java
R.java是个好东西,在Android程序开发过程中为你统一管理资源,添加ID,不可谓不犀利.不过有的时候好东西就越是娇贵,在写Android代码的时候,R.java频繁出错,搞得我是身心俱疲.数次 ...