真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java霍夫算法源代碼,java霍夫曼編碼

高分求java的RSA 和IDEA 加密解密算法

RSA算法非常簡單,概述如下:

我們提供的服務(wù)有:成都做網(wǎng)站、網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、連江ssl等。為近千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的連江網(wǎng)站制作公司

找兩素數(shù)p和q

取n=p*q

取t=(p-1)*(q-1)

取任何一個數(shù)e,要求滿足et并且e與t互素(就是最大公因數(shù)為1)

取d*e%t==1

這樣最終得到三個數(shù): n d e

設(shè)消息為數(shù)M (M n)

設(shè)c=(M**d)%n就得到了加密后的消息c

設(shè)m=(c**e)%n則 m == M,從而完成對c的解密。

注:**表示次方,上面兩式中的d和e可以互換。

在對稱加密中:

n d兩個數(shù)構(gòu)成公鑰,可以告訴別人;

n e兩個數(shù)構(gòu)成私鑰,e自己保留,不讓任何人知道。

給別人發(fā)送的信息使用e加密,只要別人能用d解開就證明信息是由你發(fā)送的,構(gòu)成了簽名機制。

別人給你發(fā)送信息時使用d加密,這樣只有擁有e的你能夠?qū)ζ浣饷堋?/p>

rsa的安全性在于對于一個大數(shù)n,沒有有效的方法能夠?qū)⑵浞纸?/p>

從而在已知n d的情況下無法獲得e;同樣在已知n e的情況下無法

求得d。

二實踐

接下來我們來一個實踐,看看實際的操作:

找兩個素數(shù):

p=47

q=59

這樣

n=p*q=2773

t=(p-1)*(q-1)=2668

取e=63,滿足et并且e和t互素

用perl簡單窮舉可以獲得滿主 e*d%t ==1的數(shù)d:

C:\Tempperl -e "foreach $i (1..9999){ print($i),last if $i*63%2668==1 }"

847

即d=847

最終我們獲得關(guān)鍵的

n=2773

d=847

e=63

取消息M=244我們看看

加密:

c=M**d%n = 244**847%2773

用perl的大數(shù)計算來算一下:

C:\Tempperl -Mbigint -e "print 244**847%2773"

465

即用d對M加密后獲得加密信息c=465

解密:

我們可以用e來對加密后的c進行解密,還原M:

m=c**e%n=465**63%2773 :

C:\Tempperl -Mbigint -e "print 465**63%2773"

244

即用e對c解密后獲得m=244 , 該值和原始信息M相等。

三字符串加密

把上面的過程集成一下我們就能實現(xiàn)一個對字符串加密解密的示例了。

每次取字符串中的一個字符的ascii值作為M進行計算,其輸出為加密后16進制

的數(shù)的字符串形式,按3字節(jié)表示,如01F

代碼如下:

#!/usr/bin/perl -w

#RSA 計算過程學(xué)習(xí)程序編寫的測試程序

#watercloud 2003-8-12

#

use strict;

use Math::BigInt;

my %RSA_CORE = (n=2773,e=63,d=847); #p=47,q=59

my $N=new Math::BigInt($RSA_CORE{n});

my $E=new Math::BigInt($RSA_CORE{e});

my $D=new Math::BigInt($RSA_COREsqu6kqw);

print "N=$N D=$D E=$E\n";

sub RSA_ENCRYPT

{

my $r_mess = shift @_;

my ($c,$i,$M,$C,$cmess);

for($i=0;$i length($$r_mess);$i++)

{

$c=ord(substr($$r_mess,$i,1));

$M=Math::BigInt-new($c);

$C=$M-copy(); $C-bmodpow($D,$N);

$c=sprintf "%03X",$C;

$cmess.=$c;

}

return \$cmess;

}

sub RSA_DECRYPT

{

my $r_mess = shift @_;

my ($c,$i,$M,$C,$dmess);

for($i=0;$i length($$r_mess);$i+=3)

{

$c=substr($$r_mess,$i,3);

$c=hex($c);

$M=Math::BigInt-new($c);

$C=$M-copy(); $C-bmodpow($E,$N);

$c=chr($C);

$dmess.=$c;

}

return \$dmess;

}

my $mess="RSA 娃哈哈哈~~~";

$mess=$ARGV[0] if @ARGV = 1;

print "原始串:",$mess,"\n";

my $r_cmess = RSA_ENCRYPT(\$mess);

print "加密串:",$$r_cmess,"\n";

my $r_dmess = RSA_DECRYPT($r_cmess);

print "解密串:",$$r_dmess,"\n";

#EOF

測試一下:

C:\Tempperl rsa-test.pl

N=2773 D=847 E=63

原始串:RSA 娃哈哈哈~~~

加密串:5CB6CD6BC58A7709470AA74A0AA74A0AA74A6C70A46C70A46C70A4

解密串:RSA 娃哈哈哈~~~

C:\Tempperl rsa-test.pl 安全焦點(xfocus)

N=2773 D=847 E=63

原始串:安全焦點(xfocus)

加密串:3393EC12F0A466E0AA9510D025D7BA0712DC3379F47D51C325D67B

解密串:安全焦點(xfocus)

四提高

前面已經(jīng)提到,rsa的安全來源于n足夠大,我們測試中使用的n是非常小的,根本不能保障安全性,

我們可以通過RSAKit、RSATool之類的工具獲得足夠大的N 及D E。

通過工具,我們獲得1024位的N及D E來測試一下:

n=0x328C74784DF31119C526D18098EBEBB943B0032B599CEE13CC2BCE7B5FCD15F90B66EC3A85F5005D

BDCDED9BDFCB3C4C265AF164AD55884D8278F791C7A6BFDAD55EDBC4F017F9CCF1538D4C2013433B383B

47D80EC74B51276CA05B5D6346B9EE5AD2D7BE7ABFB36E37108DD60438941D2ED173CCA50E114705D7E2

BC511951

d=0x10001

e=0xE760A3804ACDE1E8E3D7DC0197F9CEF6282EF552E8CEBBB7434B01CB19A9D87A3106DD28C523C2995

4C5D86B36E943080E4919CA8CE08718C3B0930867A98F635EB9EA9200B25906D91B80A47B77324E66AFF2

C4D70D8B1C69C50A9D8B4B7A3C9EE05FFF3A16AFC023731D80634763DA1DCABE9861A4789BD782A592D2B

1965

設(shè)原始信息

M=0x11111111111122222222222233333333333

完成這么大數(shù)字的計算依賴于大數(shù)運算庫,用perl來運算非常簡單:

A) 用d對M進行加密如下:

c=M**d%n :

C:\Tempperl -Mbigint -e " $x=Math::BigInt-bmodpow(0x11111111111122222222222233

333333333, 0x10001, 0x328C74784DF31119C526D18098EBEBB943B0032B599CEE13CC2BCE7B5F

CD15F90B66EC3A85F5005DBDCDED9BDFCB3C4C265AF164AD55884D8278F791C7A6BFDAD55EDBC4F0

17F9CCF1538D4C2013433B383B47D80EC74B51276CA05B5D6346B9EE5AD2D7BE7ABFB36E37108DD6

0438941D2ED173CCA50E114705D7E2BC511951);print $x-as_hex"

0x17b287be418c69ecd7c39227ab681ac422fcc84bb35d8a632543b304de288a8d4434b73d2576bd

45692b007f3a2f7c5f5aa1d99ef3866af26a8e876712ed1d4cc4b293e26bc0a1dc67e247715caa6b

3028f9461a3b1533ec0cb476441465f10d8ad47452a12db0601c5e8beda686dd96d2acd59ea89b91

f1834580c3f6d90898

即用d對M加密后信息為:

c=0x17b287be418c69ecd7c39227ab681ac422fcc84bb35d8a632543b304de288a8d4434b73d2576bd

45692b007f3a2f7c5f5aa1d99ef3866af26a8e876712ed1d4cc4b293e26bc0a1dc67e247715caa6b

3028f9461a3b1533ec0cb476441465f10d8ad47452a12db0601c5e8beda686dd96d2acd59ea89b91

f1834580c3f6d90898

B) 用e對c進行解密如下:

m=c**e%n :

C:\Tempperl -Mbigint -e " $x=Math::BigInt-bmodpow(0x17b287be418c69ecd7c39227ab

681ac422fcc84bb35d8a632543b304de288a8d4434b73d2576bd45692b007f3a2f7c5f5aa1d99ef3

866af26a8e876712ed1d4cc4b293e26bc0a1dc67e247715caa6b3028f9461a3b1533ec0cb4764414

65f10d8ad47452a12db0601c5e8beda686dd96d2acd59ea89b91f1834580c3f6d90898, 0xE760A

3804ACDE1E8E3D7DC0197F9CEF6282EF552E8CEBBB7434B01CB19A9D87A3106DD28C523C29954C5D

86B36E943080E4919CA8CE08718C3B0930867A98F635EB9EA9200B25906D91B80A47B77324E66AFF

2C4D70D8B1C69C50A9D8B4B7A3C9EE05FFF3A16AFC023731D80634763DA1DCABE9861A4789BD782A

592D2B1965, 0x328C74784DF31119C526D18098EBEBB943B0032B599CEE13CC2BCE7B5FCD15F90

B66EC3A85F5005DBDCDED9BDFCB3C4C265AF164AD55884D8278F791C7A6BFDAD55EDBC4F017F9CCF

1538D4C2013433B383B47D80EC74B51276CA05B5D6346B9EE5AD2D7BE7ABFB36E37108DD60438941

D2ED173CCA50E114705D7E2BC511951);print $x-as_hex"

0x11111111111122222222222233333333333

(我的P4 1.6G的機器上計算了約5秒鐘)

得到用e解密后的m=0x11111111111122222222222233333333333 == M

C) RSA通常的實現(xiàn)

RSA簡潔幽雅,但計算速度比較慢,通常加密中并不是直接使用RSA 來對所有的信息進行加密,

最常見的情況是隨機產(chǎn)生一個對稱加密的密鑰,然后使用對稱加密算法對信息加密,之后用

RSA對剛才的加密密鑰進行加密。

最后需要說明的是,當前小于1024位的N已經(jīng)被證明是不安全的

自己使用中不要使用小于1024位的RSA,最好使用2048位的。

----------------------------------------------------------

一個簡單的RSA算法實現(xiàn)JAVA源代碼:

filename:RSA.java

/*

* Created on Mar 3, 2005

*

* TODO To change the template for this generated file go to

* Window - Preferences - Java - Code Style - Code Templates

*/

import java.math.BigInteger;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.FileWriter;

import java.io.FileReader;

import java.io.BufferedReader;

import java.util.StringTokenizer;

/**

* @author Steve

*

* TODO To change the template for this generated type comment go to

* Window - Preferences - Java - Code Style - Code Templates

*/

public class RSA {

/**

* BigInteger.ZERO

*/

private static final BigInteger ZERO = BigInteger.ZERO;

/**

* BigInteger.ONE

*/

private static final BigInteger ONE = BigInteger.ONE;

/**

* Pseudo BigInteger.TWO

*/

private static final BigInteger TWO = new BigInteger("2");

private BigInteger myKey;

private BigInteger myMod;

private int blockSize;

public RSA (BigInteger key, BigInteger n, int b) {

myKey = key;

myMod = n;

blockSize = b;

}

public void encodeFile (String filename) {

byte[] bytes = new byte[blockSize / 8 + 1];

byte[] temp;

int tempLen;

InputStream is = null;

FileWriter writer = null;

try {

is = new FileInputStream(filename);

writer = new FileWriter(filename + ".enc");

}

catch (FileNotFoundException e1){

System.out.println("File not found: " + filename);

}

catch (IOException e1){

System.out.println("File not found: " + filename + ".enc");

}

/**

* Write encoded message to 'filename'.enc

*/

try {

while ((tempLen = is.read(bytes, 1, blockSize / 8)) 0) {

for (int i = tempLen + 1; i bytes.length; ++i) {

bytes[i] = 0;

}

writer.write(encodeDecode(new BigInteger(bytes)) + " ");

}

}

catch (IOException e1) {

System.out.println("error writing to file");

}

/**

* Close input stream and file writer

*/

try {

is.close();

writer.close();

}

catch (IOException e1) {

System.out.println("Error closing file.");

}

}

public void decodeFile (String filename) {

FileReader reader = null;

OutputStream os = null;

try {

reader = new FileReader(filename);

os = new FileOutputStream(filename.replaceAll(".enc", ".dec"));

}

catch (FileNotFoundException e1) {

if (reader == null)

System.out.println("File not found: " + filename);

else

System.out.println("File not found: " + filename.replaceAll(".enc", "dec"));

}

BufferedReader br = new BufferedReader(reader);

int offset;

byte[] temp, toFile;

StringTokenizer st = null;

try {

while (br.ready()) {

st = new StringTokenizer(br.readLine());

while (st.hasMoreTokens()){

toFile = encodeDecode(new BigInteger(st.nextToken())).toByteArray();

System.out.println(toFile.length + " x " + (blockSize / 8));

if (toFile[0] == 0 toFile.length != (blockSize / 8)) {

temp = new byte[blockSize / 8];

offset = temp.length - toFile.length;

for (int i = toFile.length - 1; (i = 0) ((i + offset) = 0); --i) {

temp[i + offset] = toFile[i];

}

toFile = temp;

}

/*if (toFile.length != ((blockSize / 8) + 1)){

temp = new byte[(blockSize / 8) + 1];

System.out.println(toFile.length + " x " + temp.length);

for (int i = 1; i temp.length; i++) {

temp[i] = toFile[i - 1];

}

toFile = temp;

}

else

System.out.println(toFile.length + " " + ((blockSize / 8) + 1));*/

os.write(toFile);

}

}

}

catch (IOException e1) {

System.out.println("Something went wrong");

}

/**

* close data streams

*/

try {

os.close();

reader.close();

}

catch (IOException e1) {

System.out.println("Error closing file.");

}

}

/**

* Performs ttbase/tt^supttpow/tt/sup within the modular

* domain of ttmod/tt.

*

* @param base the base to be raised

* @param pow the power to which the base will be raisded

* @param mod the modular domain over which to perform this operation

* @return ttbase/tt^supttpow/tt/sup within the modular

* domain of ttmod/tt.

*/

public BigInteger encodeDecode(BigInteger base) {

BigInteger a = ONE;

BigInteger s = base;

BigInteger n = myKey;

while (!n.equals(ZERO)) {

if(!n.mod(TWO).equals(ZERO))

a = a.multiply(s).mod(myMod);

s = s.pow(2).mod(myMod);

n = n.divide(TWO);

}

return a;

}

}

在這里提供兩個版本的RSA算法JAVA實現(xiàn)的代碼下載:

1. 來自于 的RSA算法實現(xiàn)源代碼包:

2. 來自于 的實現(xiàn):

- 源代碼包

- 編譯好的jar包

另外關(guān)于RSA算法的php實現(xiàn)請參見文章:

php下的RSA算法實現(xiàn)

關(guān)于使用VB實現(xiàn)RSA算法的源代碼下載(此程序采用了psc1算法來實現(xiàn)快速的RSA加密):

RSA加密的JavaScript實現(xiàn):

JAVA 求助一段算法代碼!求大神~~~~~

看完你的,自己寫了一個。很簡陋。你的改動比較大。一時半會改不了。

你的寫好了。改動有點大。鼠標事件mousePressed()中實現(xiàn)移動。由于時間沒做優(yōu)化,主要處理方法是判斷當前l(fā)istenerPanel的上下左右是否存在上面是0的listenerPanel,存在則交換上面數(shù)字及背景顏色。自己可以優(yōu)化下里面代碼,

思路:

PuzzleTile jb = (PuzzleTile) e.getSource();

for(int i=0;ilistenerPanel.length;i++){

if(jb.equals(listenerPanel[i])){

//判斷當前l(fā)istenerPanel[i]上下左右是否存有l(wèi)istenerPanel的上面數(shù)字是0的,如果存在

則把當前的listenerPanel[i]的背景顏色及數(shù)字與上面是0 的交換。判斷周圍是否存在有點及是否交換有點復(fù)雜。

}

}

代碼修改如下:少量注釋

import java.awt.Color;

import java.awt.Font;

import java.awt.FontMetrics;

import java.awt.Graphics;

import javax.swing.*;

public class PuzzleTile extends JPanel{

private String tileNumber;

public PuzzleTile(int number) {

super();

if (number == 0) {

this.setBackground(Color.white);

}

else {

this.setBackground(Color.darkGray);

}

this.tileNumber = "" + number;

}

public void setTitleNumber(int tileNumber){//設(shè)置上面的數(shù)字

this.tileNumber=tileNumber+"";

}

public int getTitleNumber(){//獲得上面的數(shù)字

return Integer.parseInt(tileNumber);

}

public void paintComponent(Graphics graphics) {

Font a=new Font("Arial",Font.BOLD,30);

graphics.setFont(a);

graphics.setColor(Color.white);

super.paintComponent(graphics);

FontMetrics b=graphics.getFontMetrics(a);

int c=b.stringWidth(tileNumber);

int d=b.getAscent();

int e=getWidth()/2-c/2;

int f=getHeight()/2+d/2;

graphics.drawString(tileNumber,e,f);

}

}

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.util.ArrayList;

import java.util.Random;

public class SlidingPuzzle extends JFrame implements MouseListener

{

public static void main(String[] args){

SlidingPuzzle frame=new SlidingPuzzle();

frame.TestPanel();

frame.setTitle("Numeric Sliding Puzzle");

frame.setSize(400,400);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

PuzzleTile[] listenerPanel;

public void TestPanel(){

Container container=getContentPane();

container.setLayout(new GridLayout(3,3,5,5));

listenerPanel=new PuzzleTile[9];

ArrayListInteger myList=new ArrayListInteger();

int m;

for(int i=0;i9;i++){

m=new Random().nextInt(9);

if(!myList.contains(m))

myList.add(m);

else

i--;

}

for(int i=0;ilistenerPanel.length;i++){

listenerPanel[i]=new PuzzleTile(myList.get(i));

container.add(listenerPanel[i]);

listenerPanel[i].addMouseListener(this);

}

}

public void mousePressed(MouseEvent e){

PuzzleTile jb = (PuzzleTile) e.getSource();

int m=jb.getTitleNumber();

//依次判斷每一個listenerPanel上下左右是否存在上面數(shù)字為0的listenerPanel

if(jb.equals(listenerPanel[0])){

if(listenerPanel[1].getTitleNumber()==0){

listenerPanel[0].setBackground(Color.white);

listenerPanel[0].setTitleNumber(0);

listenerPanel[1].setTitleNumber(m);

listenerPanel[1].setBackground(Color.darkGray);

}

if(listenerPanel[3].getTitleNumber()==0){

listenerPanel[0].setBackground(Color.white);

listenerPanel[0].setTitleNumber(0);

listenerPanel[3].setTitleNumber(m);

listenerPanel[3].setBackground(Color.darkGray);

}

}else if(jb.equals(listenerPanel[1])){

if(listenerPanel[0].getTitleNumber()==0){

listenerPanel[1].setBackground(Color.white);

listenerPanel[1].setTitleNumber(0);

listenerPanel[0].setTitleNumber(m);

listenerPanel[0].setBackground(Color.darkGray);

}

if(listenerPanel[2].getTitleNumber()==0){

listenerPanel[1].setBackground(Color.white);

listenerPanel[1].setTitleNumber(0);

listenerPanel[2].setTitleNumber(m);

listenerPanel[2].setBackground(Color.darkGray);

}

if(listenerPanel[4].getTitleNumber()==0){

listenerPanel[1].setBackground(Color.white);

listenerPanel[1].setTitleNumber(0);

listenerPanel[4].setTitleNumber(m);

listenerPanel[4].setBackground(Color.darkGray);

}

}else if(jb.equals(listenerPanel[2])){

if(listenerPanel[1].getTitleNumber()==0){

listenerPanel[2].setBackground(Color.white);

listenerPanel[2].setTitleNumber(0);

listenerPanel[1].setTitleNumber(m);

listenerPanel[1].setBackground(Color.darkGray);

}

if(listenerPanel[5].getTitleNumber()==0){

listenerPanel[2].setBackground(Color.white);

listenerPanel[2].setTitleNumber(0);

listenerPanel[5].setTitleNumber(m);

listenerPanel[5].setBackground(Color.darkGray);

}

}else if(jb.equals(listenerPanel[3])){

if(listenerPanel[0].getTitleNumber()==0){

listenerPanel[3].setBackground(Color.white);

listenerPanel[3].setTitleNumber(0);

listenerPanel[0].setTitleNumber(m);

listenerPanel[0].setBackground(Color.darkGray);

}

if(listenerPanel[4].getTitleNumber()==0){

listenerPanel[3].setBackground(Color.white);

listenerPanel[3].setTitleNumber(0);

listenerPanel[4].setTitleNumber(m);

listenerPanel[4].setBackground(Color.darkGray);

}

if(listenerPanel[6].getTitleNumber()==0){

listenerPanel[3].setBackground(Color.white);

listenerPanel[3].setTitleNumber(0);

listenerPanel[6].setTitleNumber(m);

listenerPanel[6].setBackground(Color.darkGray);

}

}else if(jb.equals(listenerPanel[4])){

if(listenerPanel[1].getTitleNumber()==0){

listenerPanel[4].setBackground(Color.white);

listenerPanel[4].setTitleNumber(0);

listenerPanel[1].setTitleNumber(m);

listenerPanel[1].setBackground(Color.darkGray);

}

if(listenerPanel[7].getTitleNumber()==0){

listenerPanel[4].setBackground(Color.white);

listenerPanel[4].setTitleNumber(0);

listenerPanel[7].setTitleNumber(m);

listenerPanel[7].setBackground(Color.darkGray);

}

if(listenerPanel[3].getTitleNumber()==0){

listenerPanel[4].setBackground(Color.white);

listenerPanel[4].setTitleNumber(0);

listenerPanel[3].setTitleNumber(m);

listenerPanel[3].setBackground(Color.darkGray);

}

if(listenerPanel[5].getTitleNumber()==0){

listenerPanel[4].setBackground(Color.white);

listenerPanel[4].setTitleNumber(0);

listenerPanel[5].setTitleNumber(m);

listenerPanel[5].setBackground(Color.darkGray);

}

}else if(jb.equals(listenerPanel[5])){

if(listenerPanel[4].getTitleNumber()==0){

listenerPanel[5].setBackground(Color.white);

listenerPanel[5].setTitleNumber(0);

listenerPanel[4].setTitleNumber(m);

listenerPanel[4].setBackground(Color.darkGray);

}

if(listenerPanel[2].getTitleNumber()==0){

listenerPanel[5].setBackground(Color.white);

listenerPanel[5].setTitleNumber(0);

listenerPanel[2].setTitleNumber(m);

listenerPanel[2].setBackground(Color.darkGray);

}

if(listenerPanel[8].getTitleNumber()==0){

listenerPanel[5].setBackground(Color.white);

listenerPanel[5].setTitleNumber(0);

listenerPanel[8].setTitleNumber(m);

listenerPanel[8].setBackground(Color.darkGray);

}

}else if(jb.equals(listenerPanel[6])){

if(listenerPanel[3].getTitleNumber()==0){

listenerPanel[6].setBackground(Color.white);

listenerPanel[6].setTitleNumber(0);

listenerPanel[3].setTitleNumber(m);

listenerPanel[3].setBackground(Color.darkGray);

}

if(listenerPanel[7].getTitleNumber()==0){

listenerPanel[6].setBackground(Color.white);

listenerPanel[6].setTitleNumber(0);

listenerPanel[7].setTitleNumber(m);

listenerPanel[7].setBackground(Color.darkGray);

}

}else if(jb.equals(listenerPanel[7])){

if(listenerPanel[6].getTitleNumber()==0){

listenerPanel[7].setBackground(Color.white);

listenerPanel[7].setTitleNumber(0);

listenerPanel[6].setTitleNumber(m);

listenerPanel[6].setBackground(Color.darkGray);

}

if(listenerPanel[8].getTitleNumber()==0){

listenerPanel[7].setBackground(Color.white);

listenerPanel[7].setTitleNumber(0);

listenerPanel[8].setTitleNumber(m);

listenerPanel[8].setBackground(Color.darkGray);

}

if(listenerPanel[4].getTitleNumber()==0){

listenerPanel[7].setBackground(Color.white);

listenerPanel[7].setTitleNumber(0);

listenerPanel[4].setTitleNumber(m);

listenerPanel[4].setBackground(Color.darkGray);

}

}else {

if(listenerPanel[5].getTitleNumber()==0){

listenerPanel[8].setBackground(Color.white);

listenerPanel[8].setTitleNumber(0);

listenerPanel[5].setTitleNumber(m);

listenerPanel[5].setBackground(Color.darkGray);

}

if(listenerPanel[7].getTitleNumber()==0){

listenerPanel[8].setBackground(Color.white);

listenerPanel[8].setTitleNumber(0);

listenerPanel[7].setTitleNumber(m);

listenerPanel[7].setBackground(Color.darkGray);

}

}

boolean b=true;//是否完成標記

for(int i=0;ilistenerPanel.length;i++){//判斷l(xiāng)istenerPanel[0]~listenerPanel[8]上的數(shù)字是從0~8.若是完成拼圖

if(listenerPanel[i].getTitleNumber()!=i)

b=false;

}

if(b==true){

int i=JOptionPane.showConfirmDialog(null, "would you paly agin?");

if(i==0){

if(i==0){

Rectangle re=this.getBounds();

this.dispose();

SlidingPuzzle slidingPuzzle=new SlidingPuzzle();

slidingPuzzle.setBounds(re);

}

else if(i==1)

System.exit(0);

else ;

}

}

}

public void mouseReleased(MouseEvent e){}

public void mouseClicked(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

}

如果運行過程什么問題追問或者hi

java的md5的加密算法代碼

import java.lang.reflect.*;

/*******************************************************************************

* keyBean 類實現(xiàn)了RSA Data Security, Inc.在提交給IETF 的RFC1321中的keyBean message-digest

* 算法。

******************************************************************************/

public class keyBean {

/*

* 下面這些S11-S44實際上是一個4*4的矩陣,在原始的C實現(xiàn)中是用#define 實現(xiàn)的, 這里把它們實現(xiàn)成為static

* final是表示了只讀,切能在同一個進程空間內(nèi)的多個 Instance間共享

*/

static final int S11 = 7;

static final int S12 = 12;

static final int S13 = 17;

static final int S14 = 22;

static final int S21 = 5;

static final int S22 = 9;

static final int S23 = 14;

static final int S24 = 20;

static final int S31 = 4;

static final int S32 = 11;

static final int S33 = 16;

static final int S34 = 23;

static final int S41 = 6;

static final int S42 = 10;

static final int S43 = 15;

static final int S44 = 21;

static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0 };

/*

* 下面的三個成員是keyBean計算過程中用到的3個核心數(shù)據(jù),在原始的C實現(xiàn)中 被定義到keyBean_CTX結(jié)構(gòu)中

*/

private long[] state = new long[4]; // state (ABCD)

private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb

// first)

private byte[] buffer = new byte[64]; // input buffer

/*

* digestHexStr是keyBean的唯一一個公共成員,是最新一次計算結(jié)果的 16進制ASCII表示.

*/

public String digestHexStr;

/*

* digest,是最新一次計算結(jié)果的2進制內(nèi)部表示,表示128bit的keyBean值.

*/

private byte[] digest = new byte[16];

/*

* getkeyBeanofStr是類keyBean最主要的公共方法,入口參數(shù)是你想要進行keyBean變換的字符串

* 返回的是變換完的結(jié)果,這個結(jié)果是從公共成員digestHexStr取得的.

*/

public String getkeyBeanofStr(String inbuf) {

keyBeanInit();

keyBeanUpdate(inbuf.getBytes(), inbuf.length());

keyBeanFinal();

digestHexStr = "";

for (int i = 0; i 16; i++) {

digestHexStr += byteHEX(digest[i]);

}

return digestHexStr;

}

// 這是keyBean這個類的標準構(gòu)造函數(shù),JavaBean要求有一個public的并且沒有參數(shù)的構(gòu)造函數(shù)

public keyBean() {

keyBeanInit();

return;

}

/* keyBeanInit是一個初始化函數(shù),初始化核心變量,裝入標準的幻數(shù) */

private void keyBeanInit() {

count[0] = 0L;

count[1] = 0L;

// /* Load magic initialization constants.

state[0] = 0x67452301L;

state[1] = 0xefcdab89L;

state[2] = 0x98badcfeL;

state[3] = 0x10325476L;

return;

}

/*

* F, G, H ,I 是4個基本的keyBean函數(shù),在原始的keyBean的C實現(xiàn)中,由于它們是

* 簡單的位運算,可能出于效率的考慮把它們實現(xiàn)成了宏,在java中,我們把它們 實現(xiàn)成了private方法,名字保持了原來C中的。

*/

private long F(long x, long y, long z) {

return (x y) | ((~x) z);

}

private long G(long x, long y, long z) {

return (x z) | (y (~z));

}

private long H(long x, long y, long z) {

return x ^ y ^ z;

}

private long I(long x, long y, long z) {

return y ^ (x | (~z));

}

/*

* FF,GG,HH和II將調(diào)用F,G,H,I進行近一步變換 FF, GG, HH, and II transformations for

* rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent

* recomputation.

*/

private long FF(long a, long b, long c, long d, long x, long s, long ac) {

a += F(b, c, d) + x + ac;

a = ((int) a s) | ((int) a (32 - s));

a += b;

return a;

}

private long GG(long a, long b, long c, long d, long x, long s, long ac) {

a += G(b, c, d) + x + ac;

a = ((int) a s) | ((int) a (32 - s));

a += b;

return a;

}

private long HH(long a, long b, long c, long d, long x, long s, long ac) {

a += H(b, c, d) + x + ac;

a = ((int) a s) | ((int) a (32 - s));

a += b;

return a;

}

private long II(long a, long b, long c, long d, long x, long s, long ac) {

a += I(b, c, d) + x + ac;

a = ((int) a s) | ((int) a (32 - s));

a += b;

return a;

}

/*

* keyBeanUpdate是keyBean的主計算過程,inbuf是要變換的字節(jié)串,inputlen是長度,這個

* 函數(shù)由getkeyBeanofStr調(diào)用,調(diào)用之前需要調(diào)用keyBeaninit,因此把它設(shè)計成private的

*/

private void keyBeanUpdate(byte[] inbuf, int inputLen) {

int i, index, partLen;

byte[] block = new byte[64];

index = (int) (count[0] 3) 0x3F;

// /* Update number of bits */

if ((count[0] += (inputLen 3)) (inputLen 3))

count[1]++;

count[1] += (inputLen 29);

partLen = 64 - index;

// Transform as many times as possible.

if (inputLen = partLen) {

keyBeanMemcpy(buffer, inbuf, index, 0, partLen);

keyBeanTransform(buffer);

for (i = partLen; i + 63 inputLen; i += 64) {

keyBeanMemcpy(block, inbuf, 0, i, 64);

keyBeanTransform(block);

}

index = 0;

} else

i = 0;

// /* Buffer remaining input */

keyBeanMemcpy(buffer, inbuf, index, i, inputLen - i);

}

/*

* keyBeanFinal整理和填寫輸出結(jié)果

*/

private void keyBeanFinal() {

byte[] bits = new byte[8];

int index, padLen;

// /* Save number of bits */

Encode(bits, count, 8);

// /* Pad out to 56 mod 64.

index = (int) (count[0] 3) 0x3f;

padLen = (index 56) ? (56 - index) : (120 - index);

keyBeanUpdate(PADDING, padLen);

// /* Append length (before padding) */

keyBeanUpdate(bits, 8);

// /* Store state in digest */

Encode(digest, state, 16);

}

/*

* keyBeanMemcpy是一個內(nèi)部使用的byte數(shù)組的塊拷貝函數(shù),從input的inpos開始把len長度的

* 字節(jié)拷貝到output的outpos位置開始

*/

private void keyBeanMemcpy(byte[] output, byte[] input, int outpos,

int inpos, int len) {

int i;

for (i = 0; i len; i++)

output[outpos + i] = input[inpos + i];

}

/*

* keyBeanTransform是keyBean核心變換程序,有keyBeanUpdate調(diào)用,block是分塊的原始字節(jié)

*/

private void keyBeanTransform(byte block[]) {

long a = state[0], b = state[1], c = state[2], d = state[3];

long[] x = new long[16];

Decode(x, block, 64);

/* Round 1 */

a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */

d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */

c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */

b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */

a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */

d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */

c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */

b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */

a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */

d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */

c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */

b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */

a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */

d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */

c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */

b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */

/* Round 2 */

a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */

d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */

c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */

b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */

a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */

d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */

c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */

b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */

a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */

d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */

c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */

b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */

a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */

d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */

c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */

b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */

/* Round 3 */

a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */

d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */

c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */

b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */

a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */

d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */

c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */

b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */

a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */

d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */

c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */

b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */

a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */

d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */

c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */

b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */

/* Round 4 */

a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */

d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */

c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */

b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */

a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */

d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */

c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */

b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */

a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */

d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */

c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */

b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */

a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */

d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */

c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */

b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */

state[0] += a;

state[1] += b;

state[2] += c;

state[3] += d;

}

/*

* Encode把long數(shù)組按順序拆成byte數(shù)組,因為java的long類型是64bit的, 只拆低32bit,以適應(yīng)原始C實現(xiàn)的用途

*/

private void Encode(byte[] output, long[] input, int len) {

int i, j;

for (i = 0, j = 0; j len; i++, j += 4) {

output[j] = (byte) (input[i] 0xffL);

output[j + 1] = (byte) ((input[i] 8) 0xffL);

output[j + 2] = (byte) ((input[i] 16) 0xffL);

output[j + 3] = (byte) ((input[i] 24) 0xffL);

}

}

/*

* Decode把byte數(shù)組按順序合成成long數(shù)組,因為java的long類型是64bit的,

* 只合成低32bit,高32bit清零,以適應(yīng)原始C實現(xiàn)的用途

*/

private void Decode(long[] output, byte[] input, int len) {

int i, j;

for (i = 0, j = 0; j len; i++, j += 4)

output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) 8)

| (b2iu(input[j + 2]) 16) | (b2iu(input[j + 3]) 24);

return;

}

/*

* b2iu是我寫的一個把byte按照不考慮正負號的原則的”升位”程序,因為java沒有unsigned運算

*/

public static long b2iu(byte b) {

return b 0 ? b 0x7F + 128 : b;

}

/*

* byteHEX(),用來把一個byte類型的數(shù)轉(zhuǎn)換成十六進制的ASCII表示,

* 因為java中的byte的toString無法實現(xiàn)這一點,我們又沒有C語言中的 sprintf(outbuf,"%02X",ib)

*/

public static String byteHEX(byte ib) {

char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',

'B', 'C', 'D', 'E', 'F' };

char[] ob = new char[2];

ob[0] = Digit[(ib 4) 0X0F];

ob[1] = Digit[ib 0X0F];

String s = new String(ob);

return s;

}

public static void main(String args[]) {

keyBean m = new keyBean();

if (Array.getLength(args) == 0) { // 如果沒有參數(shù),執(zhí)行標準的Test Suite

System.out.println("keyBean Test suite:");

System.out.println("keyBean(\"):" + m.getkeyBeanofStr(""));

System.out.println("keyBean(\"a\"):" + m.getkeyBeanofStr("a"));

System.out.println("keyBean(\"abc\"):" + m.getkeyBeanofStr("abc"));

System.out.println("keyBean(\"message digest\"):"

+ m.getkeyBeanofStr("message digest"));

System.out.println("keyBean(\"abcdefghijklmnopqrstuvwxyz\"):"

+ m.getkeyBeanofStr("abcdefghijklmnopqrstuvwxyz"));

System.out

.println("keyBean(\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\"):"

+ m

.getkeyBeanofStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"));

} else

System.out.println("keyBean(" + args[0] + ")="

+ m.getkeyBeanofStr(args[0]));

}

}

用java實現(xiàn)哈夫曼編碼

只要自己再加個類Tree就可以了。

代碼如下:

public class Tree {

double lChild, rChild, parent;

public Tree (double lChild, double rChild, double parent) {

this.lChild = lChild;

this.rChild = rChild;

this.parent = parent;

}

public double getLchild() {

return lChild;

}

public void setLchild(double lChild) {

this.lChild = lChild;

}

public double getRchild() {

return rChild;

}

public void setRchild(double rChild) {

this.rChild = rChild;

}

public double getParents() {

return parent;

}

public void setParents(double root) {

this.parent = root;

}

}

用java求最短路徑問題,求源程序

import java.util.Vector;

public class Link {

private Vector link = new Vector();

// private Link next = null;

public Link() {

}

public boolean addNode(Node setNode){//增加一個節(jié)點

setNode = checkNode(setNode);

if(setNode != null){

this.link.addElement((Node)setNode);

return true;

}

return false;

}

public void delNode(Node setNode){ //刪除一個節(jié)點

if(!this.link.isEmpty()){

for(int i=0;i this.link.size(); i++)

{

if(setNode.getPos() == ((Node)this.link.elementAt(i)).getPos()){

this.link.remove(i);

//System.out.println("asdfasdfas:"+this.link.size());

break;

}

}

}

}

public Node checkNode(Node setNode){//判斷節(jié)點是否在鏈表里面并取得兩者的最佳值

if(!this.link.isEmpty() setNode!=null){

for(int i=0;i this.link.size(); i++)

{

if(setNode.getPos() == ((Node)this.link.elementAt(i)).getPos()){

if(setNode.getStep() ((Node)this.link.elementAt(i)).getStep()){

setNode = (Node)this.link.elementAt(i);

this.link.remove(i);

}

else

return null;

break;

}

}

}

return setNode;

}

public boolean isEmpty(){

return this.link.isEmpty();

}

public Node getBestNode(){ //得到最好的節(jié)點

Node tmpNode = null;

if(!this.link.isEmpty()){

tmpNode = (Node)this.link.elementAt(0);

//System.out.println("tmpNodeStep:"+tmpNode.getStep());

//System.out.print("OpenNode(pos,step):");

for(int i=1;i this.link.size(); i++)

{

//System.out.print("("+((Node)this.link.elementAt(i)).getPos()+","+((Node)this.link.elementAt(i)).getStep()+")");

if(tmpNode.getJudgeNum() = ((Node)this.link.elementAt(i)).getJudgeNum()){

tmpNode = (Node)this.link.elementAt(i);

}

}

}

return tmpNode;

}

}

public class FindBestPath {

private char[][] map = null;//地圖

private int maxX,maxY;//最大的地圖邊界大小

Node startNode = null;//入口

Node endNode = null;//出口

private int endX,endY;

/*初始化

*@param setMap 地圖

*@param setX,setY 邊界值

//////////*@param startNode 入口

//////////*param endNode 出口

*@param sX,sY:開始點

*@param eX,eY:結(jié)束點

*/

public FindBestPath(char[][] setMap,int setX,int setY,int sX,int sY,int eX,int eY) {

this.map = setMap;

this.maxY = setX - 1; //x,y互換

this.maxX = setY - 1; //x,y互換

//this.startNode = sNode;

//this.endNode = eNode;

Node sNode = new Node();

Node eNode = new Node();

sNode.setFarther(null);

sNode.setPos(posToNum(sX,sY));

sNode.setStep(0);

eNode.setPos(posToNum(eX,eY));

this.startNode = sNode;

this.endNode = eNode;

this.endX = eX;//numToX(eNode.getPos());

this.endY = eY;//numToY(eNode.getPos());

}

public int posToNum(int x,int y){//從xy坐標獲得編號

return (x+y*(this.maxY+1));

}

public int numToX(int num){//從編號獲得x坐標

return (num%(this.maxY+1));

}

public int numToY(int num){//從編號獲得y坐標

return (int)(num/(this.maxY+1));

}

public boolean checkVal(int x,int y){//判斷是否為障礙

//System.out.println("map["+x+"]["+y+"]="+map[x][y]);

if(this.map[x][y] == 'N')

return false;

else

return true;

}

public int judge(Node nowNode){//一定要比實際距離小

//System.out.println("nowNodePos:"+nowNode.getPos());

int nowX = numToX(nowNode.getPos());

int nowY = numToY(nowNode.getPos());

int distance = Math.abs((nowX-this.endX))+Math.abs((nowY-this.endY));

// System.out.println("distance:"+distance);

return distance;

}

public Node getLeft(Node nowNode){//取得左節(jié)點

int nowX = numToX(nowNode.getPos());

int nowY = numToY(nowNode.getPos());

Node tmpNode = new Node();

if(nowY 0){//判斷節(jié)點是否到最左

if(checkVal(nowX,nowY-1)){

tmpNode.setFarther(nowNode);

tmpNode.setPos(posToNum(nowX,nowY-1));

tmpNode.setStep(nowNode.getStep()+1);

tmpNode.setJudgeNum(tmpNode.getStep()+judge(tmpNode));

return tmpNode;

}

}

return null;

}

public Node getRight(Node nowNode){//取得右節(jié)點

int nowX = numToX(nowNode.getPos());

int nowY = numToY(nowNode.getPos());

Node tmpNode = new Node();

if(nowY this.maxX){//判斷節(jié)點是否到最左

if(checkVal(nowX,nowY+1)){

tmpNode.setFarther(nowNode);

tmpNode.setPos(posToNum(nowX,nowY+1));

tmpNode.setStep(nowNode.getStep()+1);

tmpNode.setJudgeNum(tmpNode.getStep()+judge(tmpNode));

return tmpNode;

}

}

return null;

}

public Node getTop(Node nowNode){//取得上節(jié)點

int nowX = numToX(nowNode.getPos());

int nowY = numToY(nowNode.getPos());

Node tmpNode = new Node();

if(nowX 0){//判斷節(jié)點是否到最左

if(checkVal(nowX-1,nowY)){

tmpNode.setFarther(nowNode);

tmpNode.setPos(posToNum(nowX-1,nowY));

tmpNode.setStep(nowNode.getStep()+1);

tmpNode.setJudgeNum(tmpNode.getStep()+judge(tmpNode));

return tmpNode;

}

}

return null;

}

public Node getBottom(Node nowNode){//取得下節(jié)點

int nowX = numToX(nowNode.getPos());

int nowY = numToY(nowNode.getPos());

Node tmpNode = new Node();

if(nowX this.maxY){//判斷節(jié)點是否到最左

if(checkVal(nowX+1,nowY)){

tmpNode.setFarther(nowNode);

tmpNode.setPos(posToNum(nowX+1,nowY));

tmpNode.setStep(nowNode.getStep()+1);

tmpNode.setJudgeNum(tmpNode.getStep()+judge(tmpNode));

return tmpNode;

}

}

return null;

}

public Link getBestPath(){//尋找路徑

Link openLink = new Link();//沒有訪問的路徑

Link closeLink = new Link();//訪問過的路徑

Link path = null;//最短路徑

Node bestNode = null;

Node tmpNode = null;

openLink.addNode(this.startNode);

while(!openLink.isEmpty())//openLink is not null

{

bestNode = openLink.getBestNode();//取得最好的節(jié)點

//System.out.println("bestNode:("+numToX(bestNode.getPos())+","+numToY(bestNode.getPos())+")step:"+bestNode.getJudgeNum());

if(bestNode.getPos()==this.endNode.getPos())

{

/*this.endNode.setStep(bestNode.getStep()+1);

this.endNode.setFarther(bestNode);

this.endNode.setJudgeNum(bestNode.getStep()+1);*/

path = makePath(bestNode);

break;

}

else

{

tmpNode = closeLink.checkNode(getLeft(bestNode));

if(tmpNode != null)

//System.out.println("("+numToY(tmpNode.getPos())+","+numToX(tmpNode.getPos())+")");

openLink.addNode(tmpNode);

tmpNode = closeLink.checkNode(getRight(bestNode));

if(tmpNode != null)

// System.out.println("("+numToY(tmpNode.getPos())+","+numToX(tmpNode.getPos())+")");

openLink.addNode(tmpNode);

tmpNode = closeLink.checkNode(getTop(bestNode));

if(tmpNode != null)

// System.out.println("("+numToY(tmpNode.getPos())+","+numToX(tmpNode.getPos())+")");

openLink.addNode(tmpNode);

tmpNode = closeLink.checkNode(getBottom(bestNode));

if(tmpNode != null)

// System.out.println("("+numToY(tmpNode.getPos())+","+numToX(tmpNode.getPos())+")");

openLink.addNode(tmpNode);

openLink.delNode(bestNode);

closeLink.addNode(bestNode);

}

}

return path;

}

public Link makePath(Node lastNode){//制造路徑

Link tmpLink = new Link();

Node tmpNode = new Node();

int x,y;

tmpNode = lastNode;

if(tmpNode != null){

do{

x=numToX(tmpNode.getPos());

y=numToY(tmpNode.getPos());

System.out.println("map["+x+"]["+y+"]="+map[x][y]);

tmpLink.addNode(tmpNode);

tmpNode = tmpNode.getFarther();

}while(tmpNode != null);

}else

{

System.out.println("Couldn't find the path!");

}

return tmpLink;

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

char[][] map ={

{'Y', 'N', 'z', 'y', 'x', 'w', 'v', 'N', 'N', 'N'},

{'Y', 'N', '1', 'N', 'N', 'N', 'u', 't', 'N', 'N'},

{'N', '1', '2', '1', '1', '1', 'N', 's', 'N', 'N'},

{'N', 'N', '1', 'N', '9', 'N', 'q', 'r', 'N', 'N'},

{'N', 'N', '1', 'N', 'n', 'o', 'p', 'N', 'N', 'N'},

{'N', '4', '5', '6', 'm', 'N', 'N', 'N', 'N', 'N'},

{'N', '3', 'N', '5', 'l', 'k', 'j', 'N', 'N', 'N'},

{'N', 'N', '3', '4', 'N', 'd', 'i', 'd', 'N', 'N'},

{'N', '1', 'N', 'N', '1', 'N', 'h', 'N', 'N', 'N'},

{'N', '1', 'N', 'N', '1', 'N', 'g', 'N', 'N', 'N'},

{'N', 'a', 'b', 'c', 'd', 'e', 'f', 'N', 'N', 'N'}

};

/*map[x][y]

*如上所示:maxY=10 maxX=11 橫的代表maxY,豎的代表maxX 可以自己替換

*地圖的讀取是

*for(i=1;i行的最大值;i++)

* for(j=1;j列的最大值;j++)

* map[i][j] = 地圖[i][j]

*/

Link bestPath = new Link();

/*startNode.setFarther(null);

startNode.setPos(21);

startNode.setStep(0);

//endNode.setFarther(startNode);

endNode.setPos(79);

//endNode.setStep(0);*/

FindBestPath path = new FindBestPath(map, 11, 10, 10, 1, 0, 2);

//FindBestPath path = new FindBestPath(map, 11, 10, startNode, endNode);

bestPath = path.getBestPath();

//bestPath.printLink();

}

}

public class Node {

private int step;//從入口到該節(jié)點經(jīng)歷的步數(shù)

private int pos;//位置

private Node farther;//上一個結(jié)點

private int judgeNum;

public Node() {

}

public void setStep(int setStep){

this.step = setStep;

}

public int getStep(){

return this.step;

}

public void setPos(int setPos){

this.pos = setPos;

}

public int getPos(){

return this.pos;

}

public void setFarther(Node setNode){

this.farther = setNode;;

}

public Node getFarther(){

return this.farther;

}

public void setJudgeNum (int setInt){

this.judgeNum = setInt;;

}

public int getJudgeNum(){

return this.judgeNum;

}

}

匈牙利算法 java

#includestdio.h

#includestring.h

bool g[201][201];

int n,m,ans;

bool b[201];

int link[201];

bool init()

{

int _x,_y;

memset(g,0,sizeof(g));

memset(link,0,sizeof(link));

ans=0;

if(scanf("%d%d",n,m)==EOF)return false;

for(int i=1;i=n;i++)

{

scanf("%d",_x);

for(int j=0;j_x;j++)

{

scanf("%d",_y);

g[ i ][_y]=true;

}

}

return true;

}

bool find(int a)

{

for(int i=1;i=m;i++)

{

if(g[a][ i ]==1!b[ i ])

{

b[ i ]=true;

if(link[ i ]==0||find(link[ i ]))

{

link[ i ]=a;

return true;

}

}

}

return false;

}

int main()

{

while(init())

{

for(int i=1;i=n;i++)

{

memset(b,0,sizeof(b));

if(find(i))ans++;

}

printf("%d\n",ans);

}

}

Pascal:

Program matching;

Const

max = 1000;

Var

map : array [1..max, 1..max] of boolean; {鄰接矩陣}

match: array [1..max] of integer; {記錄當前連接方式}

chk : array [1..max] of boolean; {記錄是否遍歷過,防止死循環(huán)}

m, n, i, t1, t2, ans,k: integer;

Function dfs(p: integer): boolean;

var

i, t: integer;

begin

for i:=1 to k do

if map[p, i] and not chk[ i ] then

begin

chk[ i ] := true;

if (match[ i ] = 0) or dfs(match[ i ]) then {沒有被連過 或 尋找到增廣路}

begin

match[ i ] := p;

exit(true);

end;{if}

end;{for}

exit(false);

end;{function}

begin{main}

readln(n, m); {N 為二分圖左側(cè)點數(shù) M為可連接的邊總數(shù)}

fillchar(map, sizeof(map), 0);

k:=0;

for i:=1 to m do{initalize}

begin

readln(t1, t2);

map[t1, t2] := true;

if kt2 then k:=t2;

end;{for}

fillchar(match, sizeof(match), 0);

ans := 0;

for i:=1 to n do

begin

fillchar(chk, sizeof(chk), 0);

if dfs(i) then inc(ans);

end;

writeln(ans);

for i:=1 to 1000 do

if match[ i ] 0 then

writeln(match[ i ], '--', i);

end.


網(wǎng)頁名稱:java霍夫算法源代碼,java霍夫曼編碼
分享地址:http://weahome.cn/article/dssphed.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部