源代碼:
創(chuàng)新互聯(lián)公司于2013年創(chuàng)立,先為東乃等服務(wù)建站,東乃等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為東乃企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
/**
*本程序完成的功能是利用漢遞規(guī)算法實(shí)現(xiàn)漢諾塔的動態(tài)演示程序
*/
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.awt.*;
public class Hanio extends JApplet implements ActionListener, Runnable
{
/**
*diskNum是盤子的數(shù)量
*/
private int diskNum ;
/**
*各個組件的句柄
*/
private JButton begin, stop;
private JLabel lDiskNum;
private JTextField text;
JPanel pane;
/**
*定義一個線程句柄
*/
private Thread animate;
/**
*定義a,b,c三個柱子上是否有盤子,有哪些盤子
*/
private int adisk[];
private int bdisk[];
private int cdisk[];
public void init()
{
Container content = getContentPane();
content.setLayout(new BorderLayout());
lDiskNum = new JLabel(盤子的數(shù)目);
text = new JTextField(8);
begin = new JButton(開始);
begin.addActionListener(this);
stop = new JButton(停止);
stop.addActionListener(this);
pane = new JPanel();
pane.setLayout(new FlowLayout());
pane.add(lDiskNum);
pane.add(text);
pane.add(begin);
pane.add(stop);
content.add(pane, BorderLayout.SOUTH);
}
public void paint(Graphics g)
{
Graphics2D g2D = (Graphics2D)g;
Ellipse2D.Double ellipse;
g2D.setPaint(getBackground());
if(adisk != null)
{
/**
*消除以前畫的盤子
*/
for(int j=adisk.length, i=0; --j=0; i++ )
{
ellipse = new Ellipse2D.Double(20+i*5, 180-i*10, 180-i*10, 20);
g2D.fill(ellipse);
ellipse = new Ellipse2D.Double(220+i*5, 180-i*10, 180-i*10, 20);
g2D.fill(ellipse);
ellipse = new Ellipse2D.Double(420+i*5, 180-i*10, 180-i*10, 20);
g2D.fill(ellipse);
}
drawEllipse(g, 20, adisk);//畫A組盤子
drawEllipse(g, 220, bdisk);//畫B組盤子
drawEllipse(g, 420, cdisk);//畫C組盤子
}
pane.repaint();?
}
public void update(Graphics g)
{
paint(g);
}
/**畫出橢圓代表盤子,g是圖形環(huán)境,x是最下面的盤子的橫坐標(biāo),
*arr是柱子數(shù)組
*/
public void drawEllipse(Graphics g,int x,int arr[])
{
Graphics2D g2D = (Graphics2D)g;
Ellipse2D.Double ellipse;
g2D.setPaint(Color.gray);
g2D.draw(new Line2D.Double(x+90, 10, x+90, 180));
for(int j=arr.length, i=0; --j=0; i++ )
if(arr[j] != 0)
{
if(i%2 == 0)
g2D.setPaint(Color.blue);
else
g2D.setPaint(Color.red);
ellipse = new Ellipse2D.Double(x+i*5, 180-i*10, 180-i*10, 20);
g2D.fill(ellipse);
}
}
public void actionPerformed(ActionEvent e)
{
String command = e.getActionCommand();
if(command.equals(開始))
{
/**
*進(jìn)行初始化,開始的時候只有a柱子上有盤子,其他柱子都沒有
*/
diskNum = Integer.parseInt(text.getText());
adisk = new int[diskNum];
for(int i=0; iadisk.length; i++)
adisk[i] = 1;
bdisk = new int[diskNum];
for(int k=0; kbdisk.length; k++)
bdisk[k] = 0;
cdisk = new int[diskNum];
for(int i=0; icdisk.length; i++)
cdisk[i] = 0;
repaint();
if(animate == null || !animate.isAlive())//創(chuàng)建一個線程
{
animate = new Thread(this);
animate.start();
}
}
if(command.equals(停止))
{
for(int k=0; kbdisk.length; k++)
bdisk[k] = 0;
for(int i=0; icdisk.length; i++)
cdisk[i] = 0;
repaint();
text.setText();
animate = null;
}
}
/**
*線程方法,在此調(diào)用漢諾塔執(zhí)行移動盤子操作
*/
public void run()
{
hanio(diskNum, 'A', 'B', 'C');
repaint();
}
/**
*漢諾塔遞規(guī)調(diào)用程序,n是盤子的數(shù)量,A,B,C分別代表三個柱子
*/
public void hanio(int n, char A, char B, char C)
{
if(n 1)
{
hanio(n-1, A, C, B);
pause();//停頓幾秒在執(zhí)行
switch(A)
{
case 'A':adisk[n-1] = 0;break;
case 'B':bdisk[n-1] = 0;break;
case 'C':cdisk[n-1] = 0;break;
default:break;
}
switch(C)
{
case 'A':adisk[n-1] = 1;break;
case 'B':bdisk[n-1] = 1;break;
case 'C':cdisk[n-1] = 1;break;
default:break;
}
repaint();
hanio(n-1, B, A, C);
}
pause();
switch(A)
{
case 'A':adisk[n-1] = 0;break;
case 'B':bdisk[n-1] = 0;break;
case 'C':cdisk[n-1] = 0;break;
default:break;
}
switch(C)
{
case 'A':adisk[n-1] = 1;break;
case 'B':bdisk[n-1] = 1;break;
case 'C':cdisk[n-1] = 1;break;
default:break;
}
repaint();
}
/**
*每隔半妙鐘移動一個盤子
*/
public void pause()
{
try{
Thread.sleep(500);//可以修改此值加快盤子移動的速度
}catch(InterruptedException e){}
}
}
import java.security.*;
import javax.crypto.*;
/**
* 本例解釋如何利用DES私鑰加密算法加解密
*
* @author Devon
* @version 1.0 04/03/10
*/
public class SingleKeyExample {
public static void main(String[] args) {
try {
String algorithm = "DES"; //定義加密算法,可用 DES,DESede,Blowfish
String message = "Hello World. 這是待加密的信息";
// 生成個DES密鑰
KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
keyGenerator.init(56); //選擇DES算法,密鑰長度必須為56位
Key key = keyGenerator.generateKey(); //生成密鑰
// 生成Cipher對象
Cipher cipher = Cipher.getInstance("DES");
//用密鑰加密明文(message),生成密文(cipherText)
cipher.init(Cipher.ENCRYPT_MODE, key); //操作模式為加密(Cipher.ENCRYPT_MODE),key為密鑰
byte[] cipherText = cipher.doFinal(message.getBytes()); //得到加密后的字節(jié)數(shù)組
System.out.println("加密后的信息: " + new String(cipherText));
//用密鑰加密明文(plainText),生成密文(cipherByte)
cipher.init(Cipher.DECRYPT_MODE, key); //操作模式為解密,key為密鑰
byte[] sourceText = cipher.doFinal(cipherText); //獲得解密后字節(jié)數(shù)組
System.out.println("解密后的信息: " + new String(sourceText));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
/**
* @author Devon
*/
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
public class PairKeyExample {
public static void main(String argv[]) {
try {
String algorithm = "RSA"; //定義加密算法,可用 DES,DESede,Blowfish
String message = "張三,你好,我是李四";
//產(chǎn)生張三的密鑰對(keyPairZhang)
KeyPairGenerator keyGeneratorZhang =
KeyPairGenerator.getInstance(algorithm); //指定采用的算法
keyGeneratorZhang.initialize(1024); //指定密鑰長度為1024位
KeyPair keyPairZhang = keyGeneratorZhang.generateKeyPair(); //產(chǎn)生密鑰對
System.out.println("生成張三的公鑰對");
// 張三生成公鑰(publicKeyZhang)并發(fā)送給李四,這里發(fā)送的是公鑰的數(shù)組字節(jié)
byte[] publicKeyZhangEncode = keyPairZhang.getPublic().getEncoded();
//通過網(wǎng)絡(luò)或磁盤等方式,把公鑰編碼傳送給李四
//李四接收到張三編碼后的公鑰,將其解碼
KeyFactory keyFacoryLi = KeyFactory.getInstance(algorithm); //得到KeyFactory對象
X509EncodedKeySpec x509KeySpec =
new X509EncodedKeySpec(publicKeyZhangEncode); //公鑰采用X.509編碼
PublicKey publicKeyZhang = keyFacoryLi.generatePublic(x509KeySpec); //將公鑰的KeySpec對象轉(zhuǎn)換為公鑰
System.out.println("李四成功解碼,得到張三的公鑰");
//李四用張三的公鑰加密信息,并發(fā)送給李四
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); //得到Cipher對象
cipher.init(Cipher.ENCRYPT_MODE, publicKeyZhang); //用張三的公鑰初始化Cipher對象
byte[] cipherMessage = cipher.doFinal(message.getBytes()); //得到加密信息
System.out.println("加密后信息:" + new String(cipherMessage));
System.out.println("加密完成,發(fā)送給李四...");
//張三用自己的私鑰解密從李四處收到的信息
cipher.init(Cipher.DECRYPT_MODE, keyPairZhang.getPrivate()); //張三用其私鑰初始化Cipher對象
byte[] originalMessage = cipher.doFinal(cipherMessage); //得到解密后信息
System.out.println("張三收到信息,解密后為:" + new String(originalMessage));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
package com.test01;
import java.util.Scanner;
public class oop5 { public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// int x = 9;
// int y = 1;
int x = sc.nextInt();
int y = sc.nextInt();
int z;
z = add(x, y);
System.out.println("x的值為:" + x);
System.out.println("y的值為:" + y);
System.out.println("二者之和為:" + z);
}
/** 四種小算法 */
// 加法運(yùn)算
public static int add(int a, int b) {
int c;
c = a + b;
return c;
}
// 減法運(yùn)算
public static int jian(int d, int v) {
int m;
m = d - v;
return m;
}
// 乘積運(yùn)算
public static int addAdd(int q, int w) {
int e;
e = q * w;
return e;
}
// 除法運(yùn)算
public static int chu(int p, int k) {
int f;
f = p / k;
return f;
}
}