「java做算法」java实现算法

博主:adminadmin 2023-01-23 01:54:07 379

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

本文目录一览:

用java实现des算法

分类: 电脑/网络 程序设计 其他编程语言

问题描述:

各位好,请求各位java学习者帮助钉解决这个问题。

我想用des算法对我的名字进行加密

我也在网上下载了des算法,包括FileDES,SubKey,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 *** yzePath(){

String dirName;

int pos=srcFileNamestIndexOf("/");

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=destFileNamestIndexOf("/");

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;

*** yzePath();

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里边怎么做,例

(一) 问题描述

给定由n个整数(可能为负整数)组成的序列a1,a2,a3,···,an,求该序列的子段和的最大值。当所有整数均为负整数是定义其最大子段和为0,一次定义,所求的最优质值为:max{0、max子段和}。

(二) 算法描述

动态规划法的基本思想:

动态规划算法通常用于求解具有某种最优性质的问题。在这类问题中,可能会有许多可行解。每一个解都对应于一个值,我们希望找到具有最优值的解。

算法设计:

#include "stdafx.h"

int MaxSum(int a[],int n,int Start,intEnd){

intsum=0;

int*b,t;

b=newint[n+1];

b[0]=0;

for(inti=1;i=n;i++){

if(b[i-1]0){

b[i]=b[i-1]+a[i];

}

else {

b[i]=a[i];t=i;

}

if(b[i]sum){

sum=b[i];

Start=t;

End=i;

}

}

delete[]b;

returnsum;

}

int main(int argc, char* argv[])

{

inta[7]={0,-2,11,-4,13,-5,-2},sum,Start,End,i;

sum=MaxSum(a,6,Start,End);

for(i=Start;i=End;i++){

printf("%d ",a[i]);

}

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

getchar();

getchar();

return0;

用java做算法好吗

java是建立在许多软件基础上的编程语言,对于算法程序的优化手段受到很大限制,这些都不利于做算法

可以考虑用c做算法,可以在兼顾可移植性的同时尽量优化,然后在java中调用。

关于各种排列组合java算法实现方法

一 利用二进制状态法求排列组合 此种方法比较容易懂 但是运行效率不高 小数据排列组合可以使用

复制代码 代码如下: import java util Arrays;

//利用二进制算法进行全排列 //count : //count :

public class test { public static void main(String[] args) { long start=System currentTimeMillis(); count (); long end=System currentTimeMillis(); System out println(end start); } private static void count (){ int[] num=new int []{ }; for(int i= ;iMath pow( );i++){ String str=Integer toString(i ); int sz=str length(); for(int j= ;j sz;j++){ str=" "+str; } char[] temp=str toCharArray(); Arrays sort(temp); String gl=new String(temp); if(!gl equals(" ")){ continue; } String result=""; for(int m= ;mstr length();m++){ result+=num[Integer parseInt(str charAt(m)+"")]; } System out println(result); } } public static void count (){ int[] num=new int []{ }; int[] ss=new int []{ }; int[] temp=new int[ ]; while(temp[ ] ){ temp[temp length ]++; for(int i=temp length ;i ;i ){ if(temp[i]== ){ temp[i]= ; temp[i ]++; } } int []tt=temp clone(); Arrays sort(tt); if(!Arrays equals(tt ss)){ continue; } String result=""; for(int i= ;inum length;i++){ result+=num[temp[i]]; } System out println(result); } } }

二 用递归的思想来求排列跟组合 代码量比较大

复制代码 代码如下: package practice;

import java util ArrayList; import java util List;

public class Test {

/** * @param args */ public static void main(String[] args) { // TODO Auto generated method stub Object[] tmp={ }; // ArrayListObject[] rs=RandomC(tmp); ArrayListObject[] rs=cmn(tmp ); for(int i= ;irs size();i++) { // System out print(i+"="); for(int j= ;jrs get(i) length;j++) { System out print(rs get(i)[j]+" "); } System out println(); } }

// 求一个数组的任意组合 static ArrayListObject[] RandomC(Object[] source) { ArrayListObject[] result=new ArrayListObject[](); if(source length== ) { result add(source); } else { Object[] psource=new Object[source length ]; for(int i= ;ipsource length;i++) { psource[i]=source[i]; } result=RandomC(psource); int len=result size();//fn组合的长度 result add((new Object[]{source[source length ]})); for(int i= ;ilen;i++) { Object[] tmp=new Object[result get(i) length+ ]; for(int j= ;jtmp length ;j++) { tmp[j]=result get(i)[j]; } tmp[tmp length ]=source[source length ]; result add(tmp); } } return result; } static ArrayListObject[] cmn(Object[] source int n) { ArrayListObject[] result=new ArrayListObject[](); if(n== ) { for(int i= ;isource length;i++) { result add(new Object[]{source[i]}); } } else if(source length==n) { result add(source); } else { Object[] psource=new Object[source length ]; for(int i= ;ipsource length;i++) { psource[i]=source[i]; } result=cmn(psource n); ArrayListObject[] tmp=cmn(psource n ); for(int i= ;itmp size();i++) { Object[] rs=new Object[n]; for(int j= ;jn ;j++) { rs[j]=tmp get(i)[j]; } rs[n ]=source[source length ]; result add(rs); } } return result; }

}

三 利用动态规划的思想求排列和组合

复制代码 代码如下: 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做算法的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现算法、java做算法的信息别忘了在本站进行查找喔。