「java选举算法实现」master选举算法

博主:adminadmin 2022-11-23 16:49:06 63

今天给各位分享java选举算法实现的知识,其中也会对master选举算法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

求用JAVA实现的DES算法

package des;

import java.io.*;

import java.nio.*;

import java.nio.channels.FileChannel;

public class FileDES{

private static final boolean enc=true; //加密

private static final boolean dec=false; //解密

private String srcFileName;

private String destFileName;

private String inKey;

private boolean actionType;

private File srcFile;

private File destFile;

private Des des;

private void analyzePath(){

String dirName;

int pos=srcFileName.lastIndexOf("/");

dirName=srcFileName.substring(0,pos);

File dir=new File(dirName);

if (!dir.exists()){

System.err.println(dirName+" is not exist");

System.exit(1);

}else if(!dir.isDirectory()){

System.err.println(dirName+" is not a directory");

System.exit(1);

}

pos=destFileName.lastIndexOf("/");

dirName=destFileName.substring(0,pos);

dir=new File(dirName);

if (!dir.exists()){

if(!dir.mkdirs()){

System.out.println ("can not creat directory:"+dirName);

System.exit(1);

}

}else if(!dir.isDirectory()){

System.err.println(dirName+" is not a directory");

System.exit(1);

}

}

private static int replenish(FileChannel channel,ByteBuffer buf) throws IOException{

long byteLeft=channel.size()-channel.position();

if(byteLeft==0L)

return -1;

buf.position(0);

buf.limit(buf.position()+(byteLeft8 ? (int)byteLeft :8));

return channel.read(buf);

}

private void file_operate(boolean flag){

des=new Des(inKey);

FileOutputStream outputFile=null;

try {

outputFile=new FileOutputStream(srcFile,true);

}catch (java.io.FileNotFoundException e) {

e.printStackTrace(System.err);

}

FileChannel outChannel=outputFile.getChannel();

try{

if(outChannel.size()%2!=0){

ByteBuffer bufTemp=ByteBuffer.allocate(1);

bufTemp.put((byte)32);

bufTemp.flip();

outChannel.position(outChannel.size());

outChannel.write(bufTemp);

bufTemp.clear();

}

}catch(Exception ex){

ex.printStackTrace(System.err);

System.exit(1);

}

FileInputStream inFile=null;

try{

inFile=new FileInputStream(srcFile);

}catch(java.io.FileNotFoundException e){

e.printStackTrace(System.err);

//System.exit(1);

}

outputFile=null;

try {

outputFile=new FileOutputStream(destFile,true);

}catch (java.io.FileNotFoundException e) {

e.printStackTrace(System.err);

}

FileChannel inChannel=inFile.getChannel();

outChannel=outputFile.getChannel();

ByteBuffer inBuf=ByteBuffer.allocate(8);

ByteBuffer outBuf=ByteBuffer.allocate(8);

try{

String srcStr;

String destStr;

while(true){

if (replenish(inChannel,inBuf)==-1) break;

srcStr=((ByteBuffer)(inBuf.flip())).asCharBuffer().toString();

inBuf.clear();

if (flag)

destStr=des.enc(srcStr,srcStr.length());

else

destStr=des.dec(srcStr,srcStr.length());

outBuf.clear();

if (destStr.length()==4){

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

outBuf.putChar(destStr.charAt(i));

}

outBuf.flip();

}else{

outBuf.position(0);

outBuf.limit(2*destStr.length());

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

outBuf.putChar(destStr.charAt(i));

}

outBuf.flip();

}

try {

outChannel.write(outBuf);

outBuf.clear();

}catch (java.io.IOException ex) {

ex.printStackTrace(System.err);

}

}

System.out.println (inChannel.size());

System.out.println (outChannel.size());

System.out.println ("EoF reached.");

inFile.close();

outputFile.close();

}catch(java.io.IOException e){

e.printStackTrace(System.err);

System.exit(1);

}

}

public FileDES(String srcFileName,String destFileName,String inKey,boolean actionType){

this.srcFileName=srcFileName;

this.destFileName=destFileName;

this.actionType=actionType;

analyzePath();

srcFile=new File(srcFileName);

destFile=new File(destFileName);

this.inKey=inKey;

if (actionType==enc)

file_operate(enc);

else

file_operate(dec);

}

public static void main(String[] args){

String file1=System.getProperty("user.dir")+"/111.doc";

String file2=System.getProperty("user.dir")+"/222.doc";

String file3=System.getProperty("user.dir")+"/333.doc";

String passWord="1234ABCD";

FileDES fileDes=new FileDES(file1,file2,passWord,true);

FileDES fileDes1=new FileDES(file2,file3,passWord,false);

}

用Java怎么实现KWIC算法

//假设读取到的是 ArrayListString words

ArrayList temp= new ArrayList(words);

for(int i=0;iwords.size();i++){

temp = onetoback(temp);

// sysout temp 输出temp

}

ArrayList onetoback(ArrayList temp){

String c;

for(Iteraror a = temp.iterator();a.hasNext;){

c = a.next();

a.remove();

break;

}

temp.add(c);

return temp;

}

JAVA里如何实现动态算法?

如果只是简单的加减乘除,采用递归方式,按照运算符优先级计算,最后得到结果如果复杂一点,可以采用动态编译,你写的字符串算式,就变成java代码,最后采用反射执行两种方式。

java实现以下算法:有6个数组a,b,c,d,e,f,从每个数组中取出一个数值,按顺序放进指定的数组q中,

package Baidu_17;

public class NumberSort {

public static void sort(int[][] root, int[] number){

int[] result = new int[number.length];

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

result[i] = root[i][number[i]];

}

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

for(int j= i;jresult.length-1;j++){

if(result[j]result[j+1]){

int tmp = result[j];

result[j]=result[j+1];

result[j+1]=result[j];

}

}

}

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

System.out.print(result[i]);

}

}

public static void main(String args[]){

int myvalue[][] = new int[6][6];

int[] number = {5,4,3,2,1,0};

//int count =0;

for(int i =0;imyvalue.length*myvalue[0].length;i++){

//myvalue[0] = {1,2,3,4,5,6};

myvalue[i/6][i%6]=i;

}

sort(myvalue,number);

}

}

基本的思路。。不过还要debug。。里面的myvalue[][] 定义了你的六个数组。

然后number[]对应的就是从myvalue里面index来取值。这程序是从第一个数组里面的第5个,第二个数组的第四个。。。。取出来之后排序。打印

如果不会debug的话,要等以后了。。

关于java选举算法实现和master选举算法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-23,除非注明,否则均为首码项目网原创文章,转载请注明出处。