「dfsjava实现」java dfa
本篇文章给大家谈谈dfsjava实现,以及java dfa对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、fastdfs java 怎么使用
- 2、java实现两个时间相减,得到这个时间段是几年?不满1年的也算1年,依次类推,不满2年的算2年,
- 3、用java实现野人传教士过河问题
- 4、java实现二进制穷举
- 5、求一个java的随机算法,高手来
- 6、判断有向图是否连通+dfs+java
fastdfs java 怎么使用
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class FastdfsUtils {
/** fdfs初始化文件路径 **/
private Resource r = new ClassPathResource("fdfs_client.properties");
/** 日志**/
private final static Logger LOGGER = Logger.getLogger(FastdfsUtils.class);
/**
* 文件上传
* @throws MyException
* @throws IOException
* @throws FileNotFoundException
*/
public MapString, String fileUpLoad(boolean isEmpty, InputStream inputStream, String fileName)
throws Exception {
MapString, String map = new HashMapString, String();
if (isEmpty) {
throw new Exception("");
}
ClientGlobal.init(r.getFile().getAbsolutePath());
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient storageClient = new StorageClient(trackerServer, null);
byte[] file_buff = null;
if (inputStream != null) {
file_buff = new byte[inputStream.available()];
inputStream.read(file_buff);
}
String[] results = storageClient.upload_file(file_buff,
FilenameUtils.getExtension(fileName), null);
if (results == null) {
throw new Exception("");
}
map.put("fileType", FilenameUtils.getExtension(fileName));
map.put("original", fileName);
map.put("url", results[0] + "/" + results[1]);
map.put("state", "SUCCESS");
map.put("groupName", results[0]);
map.put("fileName", results[1]);
return map;
}
/**
* 文件下载
*/
public byte[] fileDownLoad(String groupName, String fileName) throws IOException, MyException {
ClientGlobal.init(r.getFile().getAbsolutePath());
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient storageClient = new StorageClient(trackerServer, null);
byte[] fileBytes = storageClient.download_file(groupName, fileName);
return fileBytes;
}
/**
* 文件删除
*/
public void fileDelete(String groupName, String fileName) throws FileNotFoundException,
IOException, MyException {
ClientGlobal.init(r.getFile().getAbsolutePath());
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient storageClient = new StorageClient(trackerServer, null);
storageClient.delete_file(groupName, fileName);
}
}
java实现两个时间相减,得到这个时间段是几年?不满1年的也算1年,依次类推,不满2年的算2年,
import java.text.ParseException;
import java.util.Calendar;
import java.util.Scanner;
/*
* 需求:java实现两个时间相减,得到这个时间段是几年?不满1年的也算1年,依次类推,不满2年的算2年
*
* PS:我是菜鸟,东拼四凑弄出来这个。大概思路是这样,健壮性判断和bug、优化代码就靠你自己了
*
* 健壮性判断:输入2015-5-6-7或2015-5-50此两类格式都不会报错,都出结果了
*
* 弄好之后,希望你能贴出来,私聊发给我,谢谢
*/
public class Demo3 {
public static void main(String[] args) throws ParseException {
// 键盘输入日期
Scanner in = new Scanner(System.in);
System.out.print("请输入时间,格式为【年-月-日】");
String s1 = in.next();
System.out.print("请输入另一个时间,格式为【年-月-日】");
String s2 = in.next();
// String的split方法切割字符串,返回字符串数组
String[] str_arr1 = new String[3];
str_arr1 = s1.split("-");
String[] str_arr2 = new String[3];
str_arr2 = s2.split("-");
int[] int_arr1 = new int[str_arr1.length];
int[] int_arr2 = new int[str_arr2.length];
// 字符串数组转为int数组,因为Calendar的set方法只能传int参数
toInt(int_arr1, str_arr1);
toInt(int_arr2, str_arr2);
Calendar c1 = Calendar.getInstance();
c1.set(int_arr1[0], int_arr1[1], int_arr1[2]);
Calendar c2 = Calendar.getInstance();
c2.set(int_arr2[0], int_arr2[1], int_arr2[2]);
if (c1.compareTo(c2) 0) {
offset(c1, c2);
} else {
Calendar temp = c1;
c1 = c2;
c2 = temp;
offset(c1, c2);
}
in.close();
}
private static void toInt(int[] int_arr, String[] str_arr) {
for (int i = 0; i int_arr.length; i++) {
int_arr[i] = Integer.parseInt(str_arr[i]);
}
}
// 计算偏差
public static void offset(Calendar c1, Calendar c2) {
int year = c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
int month_year = 0, day_year = 0;
if (c1.get(Calendar.MONTH) c2.get(Calendar.MONTH)) {
month_year = 1;
} else if (c1.get(Calendar.DAY_OF_MONTH) != c2
.get(Calendar.DAY_OF_MONTH)) {
day_year = 1;
}
System.out.println("时间差有" + (year + month_year + day_year) + "年");
}
}
用java实现野人传教士过河问题
//CrossRiverQuestion.java
import java.util.ArrayList;
import java.util.List;
public class CrossRiverQuestion {
public static void main(String[] args) {
CrossRiverQuestion q = new CrossRiverQuestion(5, 4);
q.solveQuestion();
}
private int peoNum;
private int savageNum;
private ListNode resultList = new ArrayListNode();
public ListNode solveQuestion() {
Node n = new Node(peoNum,savageNum,0,0,0,new ArrayListInteger(),0,0);
boolean dfsResult = dfs(n);
if(dfsResult) {
resultList.add(0,n);
for(Node node : resultList) {
System.out.println("左岸传教士:"+node.getLeftPeo()+"左岸野人: "+node.getLeftSavage()+" 右岸传教士: "+node.getRightPeo()+"右岸野人:"+node.getRightSavage()+"船上传教士:"+node.getOnBoatPeoNum()+"船上野人:"+node.getOnBoatSavageNum());
}
return resultList;
}
return null;
}
public CrossRiverQuestion(int peoNum, int savageNum) {
super();
this.peoNum = peoNum;
this.savageNum = savageNum;
}
private boolean dfs(Node n) {
if(n.hasVisited()) return false;
n.addCheckSum();
if(n.getLeftPeo()==0n.getLeftSavage()==0) return true;
if(n.getLeftPeo()0||n.getRightPeo()0||n.getLeftSavage()0||n.getRightSavage()0) {
return false;
}
if(n.getLeftPeo()n.getLeftSavage()n.getLeftPeo()0) return false;
if(n.getRightPeo()n.getRightSavage()n.getRightPeo()0) return false;
if(n.getCURR_STATE()==n.getStateBoatLeft()) {
Node n1 = new Node(n.getLeftPeo()-1,n.getLeftSavage()-1,n.getRightPeo()+1,n.getRightSavage()+1,n.getStateBoatRight(),n.getNodesCheckSum(),1,1);
if(dfs(n1)) {
resultList.add(0,n1);
return true;
}
Node n4 = new Node(n.getLeftPeo()-2,n.getLeftSavage(),n.getRightPeo()+2,n.getRightSavage(),n.getStateBoatRight(),n.getNodesCheckSum(),2,0);
if(dfs(n4)) {
resultList.add(0,n4);
return true;
}
Node n5 = new Node(n.getLeftPeo(),n.getLeftSavage()-2,n.getRightPeo(),n.getRightSavage()+2,n.getStateBoatRight(),n.getNodesCheckSum(),0,2);
if(dfs(n5)) {
resultList.add(0,n5);
return true;
}
}
else {
Node n6 = new Node(n.getLeftPeo(),n.getLeftSavage()+1,n.getRightPeo(),n.getRightSavage()-1,n.getStateBoatLeft(),n.getNodesCheckSum(),0,1);
if(dfs(n6)) {
resultList.add(0,n6);
return true;
}
Node n7 = new Node(n.getLeftPeo()+1,n.getLeftSavage(),n.getRightPeo()-1,n.getRightSavage(),n.getStateBoatLeft(),n.getNodesCheckSum(),1,0);
if(dfs(n7)) {
resultList.add(0,n7);
return true;
}
Node n1 = new Node(n.getLeftPeo()+1,n.getLeftSavage()+1,n.getRightPeo()-1,n.getRightSavage()-1,n.getStateBoatLeft(),n.getNodesCheckSum(),1,1);
if(dfs(n1)) {
resultList.add(0,n1);
return true;
}
Node n4 = new Node(n.getLeftPeo()+2,n.getLeftSavage(),n.getRightPeo()-2,n.getRightSavage(),n.getStateBoatLeft(),n.getNodesCheckSum(),2,0);
if(dfs(n4)) {
resultList.add(0,n4);
return true;
}
Node n5 = new Node(n.getLeftPeo(),n.getLeftSavage()+2,n.getRightPeo(),n.getRightSavage()-2,n.getStateBoatLeft(),n.getNodesCheckSum(),0,2);
if(dfs(n5)) {
resultList.add(0,n5);
return true;
}
}
return false;
}
public ListNode getResultList() {
return resultList;
}
}
Node.java
import java.util.ArrayList;
import java.util.List;
public class Node {
private ListInteger nodesCheckSum = new ArrayListInteger();
private int leftPeo;
private int rightPeo;
private int leftSavage;
private int rightSavage;
private int CURR_STATE = 0;
private int onBoatPeoNum = 0;
private int onBoatSavageNum = 0;
private final int STATE_BOAT_LEFT = 0;
private final int STATE_BOAT_RIGHT = 1;
public Node(int leftPeo, int leftSavage, int rightPeo, int rightSavage, int state, List checkSumList, int onBoatPeoNum, int onBoatSavageNum) {
this.CURR_STATE = state;
this.leftPeo = leftPeo;
this.leftSavage = leftSavage;
this.rightPeo = rightPeo;
this.rightSavage = rightSavage;
this.nodesCheckSum.addAll(checkSumList);
this.onBoatPeoNum = onBoatPeoNum;
this.onBoatSavageNum = onBoatSavageNum;
}
public int getLeftPeo() {
return leftPeo;
}
public void setLeftPeo(int leftPeo) {
this.leftPeo = leftPeo;
}
public int getRightPeo() {
return rightPeo;
}
public void setRightPeo(int rightPeo) {
this.rightPeo = rightPeo;
}
public int getLeftSavage() {
return leftSavage;
}
public void setLeftSavage(int leftSavage) {
this.leftSavage = leftSavage;
}
public int getRightSavage() {
return rightSavage;
}
public void setRightSavage(int rightSavage) {
this.rightSavage = rightSavage;
}
@Override
public String toString() {
return leftPeo+","+leftSavage+","+rightPeo+","+rightSavage+","+CURR_STATE;
}
public int getCURR_STATE() {
return CURR_STATE;
}
public void setCURR_STATE(int cURR_STATE) {
CURR_STATE = cURR_STATE;
}
public int getStateBoatLeft() {
return STATE_BOAT_LEFT;
}
public int getStateBoatRight() {
return STATE_BOAT_RIGHT;
}
public int calcCheckSum() {
return 1*getCURR_STATE()+10*getLeftPeo()+100*getLeftSavage()+1000*getRightPeo()+10000*getRightSavage();
}
public void addCheckSum() {
int checkSum = calcCheckSum();
nodesCheckSum.add(checkSum);
}
public boolean hasVisited() {
int sum = calcCheckSum();
for (Integer checkSum : nodesCheckSum) {
if(checkSum==sum) return true;
}
return false;
}
public ListInteger getNodesCheckSum() {
return nodesCheckSum;
}
public int getOnBoatPeoNum() {
return onBoatPeoNum;
}
public void setOnBoatPeoNum(int onBoatPeoNum) {
this.onBoatPeoNum = onBoatPeoNum;
}
public int getOnBoatSavageNum() {
return onBoatSavageNum;
}
public void setOnBoatSavageNum(int onBoatSavageNum) {
this.onBoatSavageNum = onBoatSavageNum;
}
}
java实现二进制穷举
闲着没事,给你写了一个一个DFS算法,可以枚举(a1, a2… ak),剩下的太简单,你自己写着玩吧。。。
import java.util.ArrayList;
public class DFS {
public static void main(String[] args) {
DFS dfs=new DFS();
dfs.dfs(1);
}
int sum=0;
int index=5;
ArrayListIntegerlist=new ArrayListInteger();
public void dfs(int i)
{
if(iindex)
{
sum++;
System.out.println("this is the "+sum);
for(Integer integer:this.list)
{
System.out.print(integer+" ");
}
System.out.println();
return;
}
for(int j=0;j=1;j++)
{
list.add(j);
this.dfs(i+1);
list.remove(list.size()-1);
}
}
}
求一个java的随机算法,高手来
好好的看一下就知道为什么list的长度与count 相同时,总是出现死循环 。
你在int[] a=new int[count]; 这一步操作时,数组a中的数据全部默认为0,而int rs=ran.nextInt(list.size()); 返回的值是0到list.size()-1的数。当list的长度与count 相同时,在产生最后一个随机数时,只能产生0,可是0在数组a中又存在了,所以就死循环了。
编程时,多注意边界问题。
判断有向图是否连通+dfs+java
方法1:
如果存在回路,则必存在一个子图,是一个环路。环路中所有顶点的度=2。
n算法:
第一步:删除所有度=1的顶点及相关的边,并将另外与这些边相关的其它顶点的度减一。
第二步:将度数变为1的顶点排入队列,并从该队列中取出一个顶点重复步骤一。
如果最后还有未删除顶点,则存在环,否则没有环。
n算法分析:
由于有m条边,n个顶点。
i)如果m=n,则根据图论知识可直接判断存在环路。(证明:如果没有环路,则该图必然是k棵树 k=1。根据树的性质,边的数目m = n-k。k=1,所以:mn)
ii)如果mn 则按照上面的算法每删除一个度为0的顶点操作一次(最多n次),或每删除一个度为1的顶点(同时删一条边)操作一次(最多m次)。这两种操作的总数不会超过m+n。由于mn,所以算法复杂度为O(n)。
关于dfsjava实现和java dfa的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。