导航:首页 > 源码编译 > ac算法java

ac算法java

发布时间:2022-11-05 16:32:41

java中递归算法是什么怎么算的

一、递归算法基本思路:

Java递归算法是基于Java语言实现的递归算法。递归算法是一种直接或者间接调用自身函数或者方法的算法。递归算法实质是把问题分解成规模缩小的同类问题的子问题,然后递归调用方法表示问题的解。递归往往能给我们带来非常简洁非常直观的代码形式,从而使我们的编码大大简化,然而递归的思维确实跟我们的常规思维相逆的,通常都是从上而下的思维问题,而递归趋势从下往上的进行思维。

二、递归算法解决问题的特点:

【1】递归就是方法里调用自身。

【2】在使用递归策略时,必须有一个明确的递归结束条件,称为递归出口。

【3】递归算法代码显得很简洁,但递归算法解题的运行效率较低。所以不提倡用递归设计程序。

【4】在递归调用的过程中系统为每一层的返回点、局部量等开辟了栈来存储。递归次数过多容易造成栈溢出等,所以一般不提倡用递归算法设计程序。

【5】在做递归算法的时候,一定把握出口,也就是做递归算法必须要有一个明确的递归结束条件。这一点是非常重要的。其实这个出口就是一个条件,当满足了这个条件的时候我们就不再递归了。

三、代码示例:

publicclassFactorial{

//thisisarecursivefunction

intfact(intn){

if(n==1)return1;

returnfact(n-1)*n;

}}
publicclassTestFactorial{publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

Factorialfactorial=newFactorial();

System.out.println("factorial(5)="+factorial.fact(5));

}
}

代码执行流程图如下:

此程序中n=5就是程序的出口。

Ⅱ 如何用java编程计算

你是问算法 还是带界面?

package Counter;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
class pbl extends Frame
{
double x,y,a,b;
int z;

GridLayout gl1,gl2,gl3,gl4;

Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn13,btn14,btn15,btn16,btn17;
JTextField tf1;

StringBuffer str;
Panel p1,p2,p3,p4;
public pbl()
{
JFrame jf=new JFrame("计算器");
jf.setSize(180,280);
jf.setLocation(150,150);

gl1=new GridLayout(1,1,5,5);
gl2=new GridLayout(5,3,5,5);
gl3=new GridLayout(3,1,5,5);
gl4=new GridLayout(1,1,5,5);

p1=new Panel();
p1.setLayout(gl1);
tf1=new JTextField("0");
tf1.setHorizontalAlignment(JTextField.RIGHT);
p1.add(tf1);
p1.setBounds(10, 20,153, 30);
tf1.setEditable(false);

str=new StringBuffer();

btn1=new Button("1");
btn1.setForeground(Color.BLUE);
btn1.addActionListener(new ac());

btn2=new Button("2");
btn2.setForeground(Color.BLUE);
btn2.addActionListener(new ac());

btn3=new Button("3");
btn3.setForeground(Color.BLUE);
btn3.addActionListener(new ac());

btn4=new Button("4");
btn4.setForeground(Color.BLUE);
btn4.addActionListener(new ac());

btn5=new Button("5");
btn5.setForeground(Color.BLUE);
btn5.addActionListener(new ac());

btn6=new Button("6");
btn6.setForeground(Color.BLUE);
btn6.addActionListener(new ac());

btn7=new Button("7");
btn7.setForeground(Color.BLUE);
btn7.addActionListener(new ac());

btn8=new Button("8");
btn8.setForeground(Color.BLUE);
btn8.addActionListener(new ac());

btn9=new Button("9");
btn9.setForeground(Color.BLUE);
btn9.addActionListener(new ac());

btn0=new Button("0");
btn0.setForeground(Color.BLUE);
btn0.addActionListener(new ac());

btn10=new Button("+");
btn10.setForeground(Color.RED);
btn10.addActionListener(new ac());

btn11=new Button("-");
btn11.setForeground(Color.RED);
btn11.addActionListener(new ac());

btn12=new Button("*");
btn12.setForeground(Color.red);
btn12.addActionListener(new ac());

btn13=new Button("/");
btn13.setForeground(Color.RED);
btn13.addActionListener(new ac());

btn14=new Button(".");
btn14.setForeground(Color.RED);
btn14.addActionListener(new ac());

btn15=new Button("+/-");
btn15.setForeground(Color.RED);
btn15.addActionListener(new ac());

btn16=new Button("CE");
btn16.setForeground(Color.RED);
btn16.addActionListener(new ac());

btn17=new Button("=");
btn17.setForeground(Color.RED);
btn17.addActionListener(new ac());

p2=new Panel();

p2.setLayout(gl2);
p2.add(btn16);
p2.add(btn13);
p2.add(btn12);
p2.add(btn1);
p2.add(btn2);
p2.add(btn3);
p2.add(btn4);
p2.add(btn5);
p2.add(btn6);
p2.add(btn7);
p2.add(btn8);
p2.add(btn9);
p2.add(btn0);
p2.add(btn14);
p2.add(btn15);
p2.setBounds(10, 75, 120, 150);

p3=new Panel();
p3.setLayout(gl3);
p3.add(btn10);
p3.add(btn17);
p3.setBounds(133,105,30,182);

p4=new Panel();
p4.setLayout(gl4);
p4.add(btn11);
p4.setBounds(133, 75, 30, 26);

jf.setLayout(null);
jf.setResizable(false);

jf.add(p1);
jf.add(p2);
jf.add(p3);
jf.add(p4);

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
class ac implements ActionListener
{

public void actionPerformed(ActionEvent ce)
{

if(ce.getSource()==btn16)
{
tf1.setText("0");//ce
str.setLength(0);

}
else if(ce.getSource()==btn15)//(+/-)
{
a=Double.parseDouble(tf1.getText());
//a*=-1;
tf1.setText(""+(-a));
}
else if(ce.getSource()==btn14)//(.)
{
if(tf1.getText().trim().indexOf(".")!=-1)
{

}
else if(tf1.getText().trim().equals("0"))
{

tf1.setText("0"+str.append(ce.getActionCommand()).toString());
}
else
{

tf1.setText(str.append(ce.getActionCommand()).toString());
}

}
else if(ce.getSource()==btn10)//(+)
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0;
z=1;
}

else if(ce.getSource()==btn11)//(-)
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0;
z=2;
}
else if(ce.getSource()==btn12)//(*)
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0;
z=3;

}
else if(ce.getSource()==btn13)//(/)
{
x=Double.parseDouble(tf1.getText().trim());
str.setLength(0);
y=0;
z=4;
}
else if(ce.getSource()==btn17)//(=)
{
str.setLength(0);
y=Double.parseDouble(tf1.getText().trim());

switch (z)
{
case 1: tf1.setText(""+(x+y)) ;break;
case 2: tf1.setText(""+(x-y)) ;break;
case 3: tf1.setText(""+(x*y)) ;break;
case 4: tf1.setText(""+(x/y)) ;break;

}
z=0;
}
else
{

tf1.setText(str.append(ce.getActionCommand()).toString());
}
}

}
public static void main(String[] args)
{
new pbl();
}
}

Ⅲ java 如何判断一段文字后处理信息然后再接下去用另外的方法处理下一段文字

如果你是把你输入的A is C这一整句话存到数组去的话 那你可以判断数组的长度是否为0 或者数组是否为空 然后再进行你下面的操作

Ⅳ java构建一个数组,值从ab,ac,...az,ba,bb,...,bz,...到zz,输出指定数目比如输出87个

packageTestHanLp;

importjava.util.Scanner;

publicclassTest{

publicstaticvoidmain(String[]args){

String[]str={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

Scannerin=newScanner(System.in);
System.out.println("请输入一个数:");

inta=in.nextInt();
intfirst=a/26;
intlast=a%26;

System.out.println("前"+a+"个值为:");

for(inti=0;i<=first;i++){
for(intj=0;j<26;j++){
if(i+1<=first){
System.out.print(str[i]+str[j]+"");
}else{
if(j+1<=last){
System.out.print(str[i]+str[j]+"");
}
}
}
}
}

}


这个可以不用数组的,不过既然你要求了,就加了一个

Ⅳ java多态的实现主要体现在哪些方面

多态可分为:
1.编译多态:主要是体现在重载,系统在编译时就能确定调用重载函数的哪个版本。
2.运行多态:主要体现在OO设计的继承性上,子类的对象也是父类的对象,即上溯造型,所以子类对象可以作为父类对象使用,父类的对象变量可以指向子类对象。因此通过一个父类发出的方法调用可能执行的是方法在父类中的实现,也可能是某个子类中的实现,它是由运行时刻具体的对象类型决定的。

Ⅵ java实现,从a,b,c,d,e,f,g,h,i...aa,ab,ac,...az,ba,bb,.,bz,..到zz,输出指定数目比如输出前87个

String az = "abcdefghijklmnopqrstuvwxyz";
ArrayList<String> al = new ArrayList<String>();
for(int j=0; j<az.length(); j++) al.add(az.substring(j,j+1));
for(int i=0; i<az.length(); i++){
for(int j=0; j<az.length(); j++) al.add(az.substring(i,i+1)+az.substring(j,j+1));
}
for(int j=0; j<87; j++) System.out.println(al.get(j));
运行结果:
a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb bc bd be bf bg bh bi bj bk bl bm bn bo bp bq br bs bt bu bv bw bx by bz ca cb cc cd ce cf cg ch ci

Ⅶ java的md5的加密算法代码

import java.lang.reflect.*;

/*******************************************************************************
* keyBean 类实现了RSA Data Security, Inc.在提交给IETF 的RFC1321中的keyBean message-digest
* 算法。
******************************************************************************/
public class keyBean {
/*
* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的, 这里把它们实现成为static
* final是表示了只读,切能在同一个进程空间内的多个 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个核心数据,在原始的C实现中 被定义到keyBean_CTX结构中
*/
private long[] state = new long[4]; // state (ABCD)

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

// first)

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

/*
* digestHexStr是keyBean的唯一一个公共成员,是最新一次计算结果的 16进制ASCII表示.
*/

public String digestHexStr;

/*
* digest,是最新一次计算结果的2进制内部表示,表示128bit的keyBean值.
*/
private byte[] digest = new byte[16];

/*
* getkeyBeanofStr是类keyBean最主要的公共方法,入口参数是你想要进行keyBean变换的字符串
* 返回的是变换完的结果,这个结果是从公共成员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这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数
public keyBean() {
keyBeanInit();
return;
}

/* keyBeanInit是一个初始化函数,初始化核心变量,装入标准的幻数 */
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函数,在原始的keyBean的C实现中,由于它们是
* 简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们 实现成了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将调用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是要变换的字节串,inputlen是长度,这个
* 函数由getkeyBeanofStr调用,调用之前需要调用keyBeaninit,因此把它设计成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整理和填写输出结果
*/
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是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的
* 字节拷贝到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调用,block是分块的原始字节
*/
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数组按顺序拆成byte数组,因为java的long类型是64bit的, 只拆低32bit,以适应原始C实现的用途
*/
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数组按顺序合成成long数组,因为java的long类型是64bit的,
* 只合成低32bit,高32bit清零,以适应原始C实现的用途
*/
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类型的数转换成十六进制的ASCII表示,
* 因为java中的byte的toString无法实现这一点,我们又没有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) { // 如果没有参数,执行标准的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(\"\"):"
+ m
.getkeyBeanofStr(""));
} else
System.out.println("keyBean(" + args[0] + ")="
+ m.getkeyBeanofStr(args[0]));

}
}

Ⅷ 如何用JAVA实现距离向量算法急求!!!!!!

一个实例代表一个路由器(结点)。 npk )= S
实例之间利用UDP交换路由表。 wtw%)db
能够打印出邻居列表和输出路由表!

Ⅸ 猴子过河 java算法

importjava.util.Arrays;
importjava.util.List;
importjava.util.Stack;

publicclassMonkey{
publicString[]monkeys={"a","b","c","A","B","C"};

Rulerule1=newRule1();

Rulerule2=newRule2();

publicintcount=0;

publicStringBuffersuccess=newStringBuffer(2000);

publicvoidboat(){
Stack<String>left=newStack<String>();
Stack<String>right=newStack<String>();
for(inti=0;i<monkeys.length;i++){
left.add(monkeys[i]);
}
go(left,right,"");
System.out.println("***********************共"+count+"种成功方案***********************");
System.out.println(success.toString());
System.out.println("***********************共"+count+"种成功方案***********************");

}

privatevoidgo(Stack<String>left,Stack<String>right,Strings){
for(inti=0;i<left.size();i++){
Stringmonkey1=left.get(i);
for(intj=i+1;j<left.size();j++){
StringBuffersb=newStringBuffer();
Stringmonkey2=left.get(j);
sb.append(s);
sb.append(monkey1);
sb.append(monkey2);
sb.append("->");
sb.append("");
if((rule1.isCanBoat(monkey1,monkey2,sb))&&(rule2.isCanBoat(monkey1,monkey2,sb))){
Stack<String>nextLeft=newStack<String>();
Stack<String>nextRight=newStack<String>();
nextLeft.addAll(left);
nextRight.addAll(right);
nextLeft.remove(monkey1);
nextLeft.remove(monkey2);

nextRight.push(monkey1);
nextRight.push(monkey2);

if(nextLeft.size()==0){
success.append(sb.toString()+nextLeft.toString()+nextRight.toString());
success.append(" ");
count++;
continue;
}
back(nextLeft,nextRight,sb.toString());
}
}
}

}

privatevoidback(Stack<String>left,Stack<String>right,Strings){
for(inti=0;i<right.size();i++){
Stringmonkey1=right.get(i);
StringBuffersb=newStringBuffer();
sb.append(s);
sb.append(monkey1);
sb.append("<-");
sb.append("");
if(rule2.isCanBoat(monkey1,monkey1,sb)){
Stack<String>nextLeft=newStack<String>();
Stack<String>nextRight=newStack<String>();
nextLeft.addAll(left);
nextRight.addAll(right);
nextLeft.push(monkey1);
nextRight.remove(monkey1);
go(nextLeft,nextRight,sb.toString());
}
}
}

publicstaticvoidmain(String[]args){
Monkeymonkey=newMonkey();
monkey.boat();
}
}

interfaceRule{
booleanisCanBoat(Stringm1,Stringm2,StringBuffersb);
}

classRule1implementsRule{

String[]childMonkeys={"a","b","c"};

String[]monkeys={"A","B","C"};

publicbooleanisCanBoat(Stringm1,Stringm2,StringBuffersb){

if(m1.toLowerCase().equals(m2.toLowerCase())){
returntrue;
}
List<String>childMonkeysList=Arrays.asList(childMonkeys);
List<String>monkeysList=Arrays.asList(monkeys);
if((monkeysList.contains(m1)&&monkeysList.contains(m2))
||(childMonkeysList.contains(m1)&&childMonkeysList.contains(m2))){
returntrue;
}
sb.append("大猴欺负小猴!");
System.out.println(sb.toString());
returnfalse;
}

}

classRule2implementsRule{

String[]smartMonkeys={"A","B","C","a"};

publicbooleanisCanBoat(Stringm1,Stringm2,StringBuffersb){
List<String>smartMonkeysList=Arrays.asList(smartMonkeys);

if(smartMonkeysList.contains(m1)||smartMonkeysList.contains(m2)){
returntrue;
}
sb.append("没有会划船的猴子!");
System.out.println(sb.toString());
returnfalse;
}

}

Ⅹ 蚁群算法JAVA版

说明:信息素权重,路径权重和信息素蒸发率对最后的结果影响很大,需要微调。
目前发现2 / 5 / 0.5 能达到稍微让人满意的效果。本程序离完美的ACO还差很远,仅供参考。
本蚁群算法为AS算法。

用法:

1.new一个对象
ACOforTSP tsp = new ACPforTSP(tsp数据文件名,迭代次数,蚂蚁数量,信息素权重,路径权重,信息素蒸发率);
2.用go()方法运行
tsp.go();

ACOforTSP.java
___________________________________________________________________
import java.io.File;
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;
import static java.lang.Math.random;
import java.util.HashMap;
import java.io.FileReader;
import java.io.BufferedReader;
/**
*
* @author dvdface
*/

public class ACOforTSP {

//城市的距离表
private double[][] distance;
//距离的倒数表
private double[][] heuristic;
//启发信息表
private double[][] pheromone;
//权重
private int alpha, beta;
//迭代的次数
private int iterationTimes;
//蚂蚁的数量
private int numbersOfAnt;
//蒸发率
private double rate;

ACOforTSP (String file, int iterationTimes, int numbersOfAnt, int alpha, int beta, double rate) {

//加载文件
this.initializeData(file);
//初始化参数
this.iterationTimes = iterationTimes;
//设置蚂蚁数量
this.numbersOfAnt = numbersOfAnt;
//设置权重
this.alpha = alpha;
this.beta = beta;
//设置蒸发率
this.rate = rate;
}

private void initializeData(String filename) {

//定义内部类
class City {

int no;
double x;
double y;

City(int no, double x, double y) {

this.no = no;
this.x = x;
this.y = y;
}

private double getDistance(City city) {

return sqrt(pow((x - city.x), 2) + pow((y - city.y), 2));
}
}

try {
//定义HashMap保存读取的坐标信息
HashMap<Integer, City> map = new HashMap<Integer, City>();
//读取文件
BufferedReader reader = new BufferedReader(new FileReader(new File(filename)));
for (String str = reader.readLine(); str != null; str = reader.readLine()) {
//将读到的信息保存入HashMap
if (str.matches("([0-9]+)(\\s*)([0-9]+)(.?)([0-9]*)(\\s*)([0-9]+)(.?)([0-9]*)")) {

String[] data = str.split("(\\s+)");
City city = new City(Integer.parseInt(data[0]),
Double.parseDouble(data[1]),
Double.parseDouble(data[2]));

map.put(city.no, city);
}
}
//分配距离矩阵存储空间
distance = new double[map.size() + 1][map.size() + 1];
//分配距离倒数矩阵存储空间
heuristic = new double[map.size() + 1][map.size() + 1];
//分配信息素矩阵存储空间
pheromone = new double[map.size() + 1][map.size() + 1];
for (int i = 1; i < map.size() + 1; i++) {
for (int j = 1; j < map.size() + 1; j++) {
//计算城市间的距离,并存入距离矩阵
distance[i][j] = map.get(i).getDistance(map.get(j));
//计算距离倒数,并存入距离倒数矩阵
heuristic[i][j] = 1 / distance[i][j];
//初始化信息素矩阵
pheromone[i][j] = 1;
}
}

} catch (Exception exception) {

System.out.println("初始化数据失败!");
}
}

class Ant {

//已访问城市列表
private boolean[] visited;
//访问顺序表
private int[] tour;
//已访问城市的个数
private int n;
//总的距离
private double total;

Ant() {
//给访问顺序表分配空间
tour = new int[distance.length+1];
//已存入城市数量为n,刚开始为0
n = 0;
//将起始城市1,放入访问结点顺序表第一项
tour[++n] = 1;
//给已访问城市结点分配空间
visited = new boolean[distance.length];
//第一个城市为出发城市,设置为已访问
visited[tour[n]] = true;
}

private int chooseCity() {

//用来random的随机数
double m = 0;
//获得当前所在的城市号放入j,如果和j相邻的城市没有被访问,那么加入m
for (int i = 1, j = tour[n]; i < pheromone.length; i++) {

if (!visited[i]) {
m += pow(pheromone[j][i], alpha) * pow(heuristic[j][i], beta);
}
}

//保存随机到的数
double p = m * random();
//寻找被随机到的城市
double k = 0;
//保存找到的城市
int q = 0;
for (int i = 1, j = tour[n]; k < p; i++) {

if (!visited[i]) {

k += pow(pheromone[j][i], alpha) * pow(heuristic[j][i], beta);
q = i;
}
}

return q;
}

private void constructSolution () {

while (n != (distance.length-1) ) {

//选取下一个城市
int p = chooseCity();
//计算总的距离
total += distance[tour[n]][p];
//将选取到的城市放入已访问列表
tour[++n] = p;
//将选取到的城市标记为已访问
visited[p] = true;
}

//回到起点
total += distance[tour[1]][tour[n]];
//将起点加入访问顺序表的最后
tour[++n] = tour[1];
}

private void releasePheromone() {

//释放信息素的大小
double t = 1/total;
//释放信息素
for (int i=1;i<tour.length-1;i++) {

pheromone[tour[i]][tour[i+1]] += t;
pheromone[tour[i+1]][tour[i]] += t;
}
}

}

public void go() {

//保存最好的路径和路径长度
double bestTotal = Double.MAX_VALUE;
int[] bestTour = new int[distance.length+1];

//新建蚂蚁数组,用来引用所创建的蚂蚁
Ant[] ant = new Ant[numbersOfAnt];

//进行iterationTimes次迭代
while (iterationTimes != 0) {
//初始化新的一批蚂蚁(这里用构造新的蚂蚁代替重置蚂蚁状态)
for (int i=0; i<numbersOfAnt; i++) {
ant[i] = new Ant();
}

//进行一次迭代(即让所有的蚂蚁构建一条路径)
for (int i=0; i<numbersOfAnt; i++) {

ant[i].constructSolution();
//如果蚂蚁构建的路径长度比上次最好的还好,那么保存这个长度和它所走的路径
if (ant[i].total<bestTotal) {

bestTotal = ant[i].total;
System.array(ant[i].tour, 1, bestTour, 1, bestTour.length-1);
}
}

//蒸发信息素
evaporatePheromone();

//释放信息素
for (int i=0; i<numbersOfAnt; i++) {

ant[i].releasePheromone();
}

//报告本次迭代的信息
System.out.format("本次为倒数第%d次迭代,当前最优路径长度为%10.2f\n",iterationTimes,bestTotal);

//迭代总数减去1,进行下次迭代
iterationTimes--;
}

//输出最好的路径长度
System.out.format("得到的最优的路径长度为:%10.2f\n",bestTotal);
//输出最好的路径
System.out.println("最优路径如下:");
for (int i=1; i<bestTour.length; i++) {

System.out.print("→"+bestTour[i]);
}
}

private void evaporatePheromone() {

for (int i = 1; i < pheromone.length; i++)
for (int j = 1; j < pheromone.length; j++) {

pheromone[i][j] *= 1-rate;
}

}
}

阅读全文

与ac算法java相关的资料

热点内容
什么app可以将电量变色 浏览:690
解放出你的解压抖音小游戏 浏览:343
什么方式解压比较好 浏览:264
erp是什么服务器 浏览:184
python中tmp 浏览:21
说明wpf加密过程 浏览:143
java读取list 浏览:703
iis7gzip压缩 浏览:39
有什么安卓机打吃鸡好 浏览:597
三星u盘加密狗 浏览:473
php函数的返回值吗 浏览:586
国企稳定程序员 浏览:328
编程猫如何使用教程视频 浏览:218
安卓远端网页如何打日志 浏览:218
压缩flash大小 浏览:993
解压的玩具教程可爱版 浏览:366
哪个求职app比较靠谱 浏览:888
java的读法 浏览:61
nod32局域网服务器地址 浏览:1003
数码科技解压 浏览:236