网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java代码找出所有环 java代码找出所有环形图

大神们帮帮我~ Java编程:数组中有20个数,把数组看成首尾相连的环,找出哪四个相邻数的和为最大

给你代码:

凌云ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

import java.util.*;

public class Test

{

public static void main(String... args)

{

Random r=new Random();

int[] a=new int[20];

for(int i=0;ia.length;i++)

{

a[i]=r.nextInt(100);

}

System.out.println("数组:"+Arrays.toString(a));

System.out.print("索引:\t");

int index=getIndex(a);

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

{

int ind=index+i;

ind=ind=a.length?ind-a.length:ind;

System.out.print(ind+"\t");

}

System.out.println();

System.out.println("和:"+getSum(a,index));

}

static int getIndex(int[] arr) throws IllegalArgumentException

{

if(arr.length4) throw new IllegalArgumentException();

int maxSum=0;

int index=0;

for(int i=0;iarr.length;i++)

{

int sum=getSum(arr,i);

if(summaxSum){ maxSum=sum; index=i;}

}

return index;

}

static int getSum(int[] arr,int index) throws IllegalArgumentException

{

if(arr.length4) throw new IllegalArgumentException();

int sum=0;

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

{

int ind=index+i;

ind=ind=arr.length?ind-arr.length:ind;

sum+=arr[ind];

}

return sum;

}

}

c++或java实现,判断有向图中是否存在环路,并输出所有环路

有向图是个什么东西? 一张图片么?

package test;

import java.util.*;

public class GectorGraph {

private Point root;

private ListListString circlePath;

public GectorGraph(String pointName) {

root=new Point(pointName);

}

public GectorGraph(Point point) {

root=point;

}

public boolean hasCirclePath(){

findCirclePath();

return circlePath.size()0;

}

public void findCirclePath(){

ListPoint CirclePoints=findCirclePoint();

if(circlePath==null){circlePath=new ArrayListListString();}

for(Point tempPoint:CirclePoints){

ListString pointPath=new ArrayListString();

findPointPath(tempPoint,root,pointPath);

pointPath.add(root.pointName);

circlePath.add(pointPath);

}

}

public boolean findPointPath(Point target,Point currentPoint,ListString pointPath){

if(currentPoint.equals(target)){return true;}

if(!currentPoint.hasNext()){return false;}

ListPoint pointList= currentPoint.getNextPointList();

for(Point tempPoint:pointList){

if(tempPoint.equals(root)){continue;}

if(findPointPath(target,tempPoint,pointPath)){

pointPath.add(tempPoint.pointName);

return true;

}

}

return false;

}

private ListPoint findCirclePoint(){

if(!root.hasNext()){return null;}

ListPoint circlePoints=new ArrayListPoint();

findCirclePoint(root,root,circlePoints);

return circlePoints;

}

private void findCirclePoint(Point root,Point currentPoint,ListPoint circlePoints){

if(!currentPoint.hasNext()){return;}

ListPoint pointList= currentPoint.getNextPointList();

for(Point tempPoint:pointList){

if(tempPoint.equals(root)){circlePoints.add(currentPoint);}

else{findCirclePoint(root,tempPoint,circlePoints);}

}

}

public void showPath(){

if(circlePath==null){System.out.println("Error");}

for(ListString tempList:circlePath){

StringBuffer pathString=new StringBuffer();

int tempListIndex=tempList.size()-1;

for(;tempListIndex-1;tempListIndex--){

pathString.append(tempList.get(tempListIndex));

if(tempListIndex!=0){pathString.append("-");}

}

System.out.println(pathString.toString());

}

}

public static void main(String[] args) {

Point root=new Point("root");

ListPoint p3=new ArrayListPoint();

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

p3.add(new Point("3/1/"+i));

}

ListPoint p4=new ArrayListPoint();

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

p4.add(new Point("3/2/"+i));

}

ListPoint p2=new ArrayListPoint();

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

p2.add(new Point("2/"+i));

}

p3.add(0,root);

p3.get(2).addNextPoint(root);

p4.get(0).addNextPoint(root);

p2.get(0).addNextPointList(p3);

p2.get(1).addNextPointList(p4);

root.addNextPointList(p2);

GectorGraph gg=new GectorGraph(root);

if(gg.hasCirclePath()){

gg.showPath();

}

}

}

class Point{

public String pointName;

private ListPoint nextPointList;

public Point(String pointName) {

this.pointName=pointName;

}

public void addNextPoint(Point p){

if(nextPointList==null){nextPointList=new ArrayListPoint();}

nextPointList.add(p);

}

public void addNextPointList(ListPoint pList){

if(nextPointList==null){nextPointList=new ArrayListPoint();}

nextPointList.addAll(pList);

}

public boolean hasNext(){

return nextPointList!=null!nextPointList.isEmpty();

}

public ListPoint getNextPointList() {

return nextPointList;

}

public void setNextPointList(ListPoint nextPointList) {

this.nextPointList = nextPointList;

}

}

呵呵,java写的,这个有点搅,求高手给个思路。

4啊4->'>求一段java程序,求图是否存在环。该图是有向图。要求该方法输入边的序对集合,比如3->4啊4->

一个顶点a在一个环上,那么存在以它为终点的边, 假设这些边的起点集合为PreA, 考察点a能否到达点PreA中的点,如果到达就找到了一个环,否则点a不在环上。

遍历图中的顶点进行上述操作即可。

Java判断单链表是否有环的两种实现方法

方法一:首先从头节点开始,依次遍历单链表的每一个节点。每遍历到一个新节点,就从头节点重新遍历新节点之前的所有节点,用新节点ID和此节点之前所有节点ID依次作比较。如果发现新节点之前的所有节点当中存在相同节点ID,则说明该节点被遍历过两次,链表有环;如果之前的所有节点当中不存在相同的节点,就继续遍历下一个新节点,继续重复刚才的操作。

例如这样的链表:A-B-C-D-B-C-D, 当遍历到节点D的时候,我们需要比较的是之前的节点A、B、C,不存在相同节点。这时候要遍历的下一个新节点是B,B之前的节点A、B、C、D中恰好也存在B,因此B出现了两次,判断出链表有环。

假设从链表头节点到入环点的距离是D,链表的环长是S。D+S是链表总节点数。那么算法的时间复杂度是0+1+2+3+….+(D+S-1) = (D+S-1)*(D+S)/2 , 可以简单地理解成 O(N*N)。而此算法没有创建额外存储空间,空间复杂度可以简单地理解成为O(1)。

方法二:首先创建一个以节点ID为键的HashSet集合,用来存储曾经遍历过的节点。然后同样是从头节点开始,依次遍历单链表的每一个节点。每遍历到一个新节点,就用新节点和HashSet集合当中存储的节点作比较,如果发现HashSet当中存在相同节点ID,则说明链表有环,如果HashSet当中不存在相同的节点ID,就把这个新节点ID存入HashSet,之后进入下一节点,继续重复刚才的操作。

这个方法在流程上和方法一类似,本质的区别是使用了HashSet作为额外的缓存。

假设从链表头节点到入环点的距离是D,链表的环长是S。而每一次HashSet查找元素的时间复杂度是O(1), 所以总体的时间复杂度是1*(D+S)=D+S,可以简单理解为O(N)。而算法的空间复杂度还是D+S-1,可以简单地理解成O(N)。

方法三:首先创建两个指针1和2(在Java里就是两个对象引用),同时指向这个链表的头节点。然后开始一个大循环,在循环体中,让指针1每次向下移动一个节点,让指针2每次向下移动两个节点,然后比较两个指针指向的节点是否相同。如果相同,则判断出链表有环,如果不同,则继续下一次循环。

例如链表A-B-C-D-B-C-D,两个指针最初都指向节点A,进入第一轮循环,指针1移动到了节点B,指针2移动到了C。第二轮循环,指针1移动到了节点C,指针2移动到了节点B。第三轮循环,指针1移动到了节点D,指针2移动到了节点D,此时两指针指向同一节点,判断出链表有环。

此方法也可以用一个更生动的例子来形容:在一个环形跑道上,两个运动员在同一地点起跑,一个运动员速度快,一个运动员速度慢。当两人跑了一段时间,速度快的运动员必然会从速度慢的运动员身后再次追上并超过,原因很简单,因为跑道是环形的。


文章名称:java代码找出所有环 java代码找出所有环形图
网页路径:http://bjjierui.cn/article/hppeoc.html

其他资讯