java算法演示系统代码的简单介绍( 二 )


}
三 利用动态规划的思想求排列和组合
复制代码代码如下: package Acm; //强大的求组合数 public class MainApp { public static void main(String[] args) { int[] num=new int[]{ }; String str=""; //求 个数的组合个数 // count( str num ); // 求 n个数的组合个数 count ( str num); }
private static void count (int i String str int[] num) { if(i==num length){ System out println(str); return; } count (i+ str num); count (i+ str+num[i]+" " num); }
private static void count(int i String str int[] num int n) { if(n== ){ System out println(str); return; } if(i==num length){ return; } count(i+ str+num[i]+" " num n ); count(i+ str num n); } }
下面是求排列
复制代码代码如下: lishixinzhi/Article/program/Java/JSP/201311/20148
Java算法演示系统//哈哈,我做了一种排序的GUI演示
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SortX extends JFrame{
private JLabel[]n;
private JButton start;
private JTextField in;
public SortX(){
this.getContentPane().setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(488,200);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setAlwaysOnTop(true);
start = new JButton("start sort");
JLabel l = new JLabel("输入10组内数字:");
l.setBounds(10,18,120,20);
add(l);
in=new JTextField("10,212,7,456,33,2,55,6,50,97");
in.setBounds(118,18,256,20);
add(in);
start.setBounds(380,18,88,20);
add(start);
start.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){sort();}});
n=new JLabel[10];
for(int i=0; in.length; i++){
n[i]=new JLabel();
n[i].setFont(new Font("",Font.BOLD,14));
n[i].setForeground(Color.gray);
n[i].setHorizontalAlignment(JLabel.CENTER);
add(n[i]);
}
}
private void sort(){
setTitle("排序演示");
try{
String src = https://www.04ip.com/post/in.getText();
src=https://www.04ip.com/post/src.replaceAll("[^0-9,]","");
String[] sp = src.split(",");
final int[] v = new int[sp.length];
int left=0,w=40,offx=0,offy=95;
for(int i=0; in.length;i++)n[i].setText(null);
for(int i=0; isp.length; i++){
n[i].setText(sp[i]);
v[i] = Integer.valueOf(sp[i]);
n[i].setSize(w,20);
n[i].setLocation((left+=w)+offx,offy);
}
new Thread(){
public void run(){
start.setEnabled(false);
s(500);
try{
for(int i=0; in.length-1; i++){
if(v[i]v[i+1]){
JLabel l=n[i];
n[i]=n[i+1];
n[i+1]=l;
n[i].setForeground(Color.blue);
for(int k=0; k7; k++){
n[i].setVisible(k%2==0);
s(123);
}
swap(n[i],n[i+1]);
n[i].setForeground(Color.gray);
v[i]+=v[i+1];
v[i+1]=v[i]-v[i+1];
v[i]-=v[i+1];
i-=i==0?1:2;
}
}
}catch(Exception e){}
start.setEnabled(true);
}
private void swap(JLabel a, JLabel b) {
JLabel t = a;
a=b;
b=t;
Point pa=a.getLocation();
Point pb=b.getLocation();
int x1,x2,y1,y2;
x1=pa.x;
x2=pb.x;
y1=pa.y;
y2=pb.y;
int delay=10;
while(x1(x1+x2)/2){
a.setLocation(++x1,y1++);
b.setLocation(--x2,y2--);
s(delay);
}
while(x1pb.x){
a.setLocation(++x1,y1--);
b.setLocation(--x2,y2++);
s(delay);
}
a.setLocation(pb);
b.setLocation(pa);
}
private void s(int i) {
try {
sleep(i);
} catch (Exception e) {}
}
}.start();
}catch(Exception e){e.printStackTrace();setTitle("请检查输入的数据,只能输入10组哦");}
}
public static void main(String[] args) {
new SortX().setVisible(true);

推荐阅读