
正文
贪心算法代码实现Java 贪心算法 java
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java贪吃蛇技术选型怎么写的?
Java贪吃蛇技术选型一般需要考虑以下几点:
开发平台:需要选择适用于Java语言的开发平台贪心算法代码实现Java,如Eclipse、IntelliJ IDEA等。
编程语言:需要选择Java语言来开发贪吃蛇游戏。
框架和库:可以使用Java中的Swing框架和AWT库来开发图形界面贪心算法代码实现Java,并使用Java多线程编程技术来实现游戏的实时动态效果。
算法和数据结构:可以使用队列或链表等数据结构来存储贪吃蛇的身体,并使用类似贪心算法的思想来决定贪吃蛇的下一步移动方向。
编码风格和规范:需要遵循Java的编码风格和规范,确保代码的可读性和可维护性。
希望以上内容能够帮助您了解Java贪吃蛇技术选型。如果您有其贪心算法代码实现Java他问题,欢迎随时告诉我,我会尽力为您解答。
相关问答
Q1: 找零钱问题 [贪心算法](java实现)
public getMin{
public int MinNumber=0;
public int findMax(int[] a){
for(int i=0;ia.length;i++){
if(a[i]==0) return a[--i];
}
return a[a.length-1];
}
public boolean Compare(int a,int b){
public boolean flag=true;
if(ab) flag=flase;
return flag;
}
public int getMinNumber(int[] M,int Money){
int[] findM=new int[M.length];
int index=0;
for(int i=0;iM.length;i++){
boolean f = this.Compare(M[i],money)
if(f) findM[index++]=M[i];
}
int max = this.findMax(findM);
MinNumber++;
if((Money-max)!=0) {
getMinNumber(M,Money-max)
}
return MinNumber;
}
public int[] Start(){
System.out.println("请输入查询组数");
int group=System.in.read();
int[] M={1,2,5,10,20,50,100};
int[] Result = new Int[group];
int index=0;
while (group-- 0){
System.out.println("请输入金额");
int money=System.in.read();
Result[index++] = getMinNumber(M,money);
MinNumber=0;
}
}
public void print(int[] MinNumber){
for(int i=0;iMinNumber.length.i++){
System.out.println(MinNumber[i]+" ");
}
}
}
public static void main(String[] args){
new getMin().print(new getMin().Start());
}
没测试啊,有问题请勿喷,呵呵
Q2: JAVA-数组转化问题
public class test6 {
int a[] = { 1, 2, 3, 4, 5, 6, 7 };
int b[] = { 2, 1, 4, 3, 6, 5, 7 };
public static void main(String args[]) {
test6 test = new test6();
test.go();
}
public void go() {
if (smart(a, b))
System.out.println("n=" + n(a, b));
}
public boolean smart(int[] a, int[] b) {
int a_[] = array(a);
int b_[] = array(b);
if (a.length == b.length) {
for (int c = 0; c a.length; c++) {
if (a_[c] != b_[c]) {
System.out.println("cannot transform");
return false;
}
}
for (int c = 0; c a.length; c++)
System.out.print(a[c]+" ");
System.out.println();
for (int c = 0; c b.length; c++)
System.out.print(b[c]+" ");
System.out.println();
return true;
} else {
System.out.println("cannot transform");
return false;
}
}
public int[] array(int[] a) {
int[] z = new int[a.length];
for (int c = 0; c a.length; c++)
z[c] = a[c];
for (int c = 0; c z.length - 1; c++) {
for (int d = c + 1; d z.length; d++) {
if (z[c] z[d]) {
int x = z[c];
z[c] = z[d];
z[d] = x;
}
}
}
return z;
}
public int n(int[] a, int[] b) {
int c = 0;
for (int d = 0; d a.length; d++)
if (a[d] != b[d])
c += 5;
return (c + 8) / 10;
}
}
Q3: java 最短路径算法 如何实现有向 任意两点的最短路径
Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。
Dijkstra一般的表述通常有两种方式,一种用永久和临时标号方式,一种是用OPEN, CLOSE表方式
用OPEN,CLOSE表的方式,其采用的是贪心法的算法策略,大概过程如下:
1.声明两个集合,open和close,open用于存储未遍历的节点,close用来存储已遍历的节点
2.初始阶段,将初始节点放入close,其他所有节点放入open
3.以初始节点为中心向外一层层遍历,获取离指定节点最近的子节点放入close并从新计算路径,直至close包含所有子节点
代码实例如下:
Node对象用于封装节点信息,包括名字和子节点
[java] view plain copy
public class Node {
private String name;
private MapNode,Integer child=new HashMapNode,Integer();
public Node(String name){
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MapNode, Integer getChild() {
return child;
}
public void setChild(MapNode, Integer child) {
this.child = child;
}
}
MapBuilder用于初始化数据源,返回图的起始节点
[java] view plain copy
public class MapBuilder {
public Node build(SetNode open, SetNode close){
Node nodeA=new Node("A");
Node nodeB=new Node("B");
Node nodeC=new Node("C");
Node nodeD=new Node("D");
Node nodeE=new Node("E");
Node nodeF=new Node("F");
Node nodeG=new Node("G");
Node nodeH=new Node("H");
nodeA.getChild().put(nodeB, 1);
nodeA.getChild().put(nodeC, 1);
nodeA.getChild().put(nodeD, 4);
nodeA.getChild().put(nodeG, 5);
nodeA.getChild().put(nodeF, 2);
nodeB.getChild().put(nodeA, 1);
nodeB.getChild().put(nodeF, 2);
nodeB.getChild().put(nodeH, 4);
nodeC.getChild().put(nodeA, 1);
nodeC.getChild().put(nodeG, 3);
nodeD.getChild().put(nodeA, 4);
nodeD.getChild().put(nodeE, 1);
nodeE.getChild().put(nodeD, 1);
nodeE.getChild().put(nodeF, 1);
nodeF.getChild().put(nodeE, 1);
nodeF.getChild().put(nodeB, 2);
nodeF.getChild().put(nodeA, 2);
nodeG.getChild().put(nodeC, 3);
nodeG.getChild().put(nodeA, 5);
nodeG.getChild().put(nodeH, 1);
nodeH.getChild().put(nodeB, 4);
nodeH.getChild().put(nodeG, 1);
open.add(nodeB);
open.add(nodeC);
open.add(nodeD);
open.add(nodeE);
open.add(nodeF);
open.add(nodeG);
open.add(nodeH);
close.add(nodeA);
return nodeA;
}
}
图的结构如下图所示:
Dijkstra对象用于计算起始节点到所有其他节点的最短路径
[java] view plain copy
public class Dijkstra {
SetNode open=new HashSetNode();
SetNode close=new HashSetNode();
MapString,Integer path=new HashMapString,Integer();//封装路径距离
MapString,String pathInfo=new HashMapString,String();//封装路径信息
public Node init(){
//初始路径,因没有A-E这条路径,所以path(E)设置为Integer.MAX_VALUE
path.put("B", 1);
pathInfo.put("B", "A-B");
path.put("C", 1);
pathInfo.put("C", "A-C");
path.put("D", 4);
pathInfo.put("D", "A-D");
path.put("E", Integer.MAX_VALUE);
pathInfo.put("E", "A");
path.put("F", 2);
pathInfo.put("F", "A-F");
path.put("G", 5);
pathInfo.put("G", "A-G");
path.put("H", Integer.MAX_VALUE);
pathInfo.put("H", "A");
//将初始节点放入close,其他节点放入open
Node start=new MapBuilder().build(open,close);
return start;
}
public void computePath(Node start){
Node nearest=getShortestPath(start);//取距离start节点最近的子节点,放入close
if(nearest==null){
return;
}
close.add(nearest);
open.remove(nearest);
MapNode,Integer childs=nearest.getChild();
for(Node child:childs.keySet()){
if(open.contains(child)){//如果子节点在open中
Integer newCompute=path.get(nearest.getName())+childs.get(child);
if(path.get(child.getName())newCompute){//之前设置的距离大于新计算出来的距离
path.put(child.getName(), newCompute);
pathInfo.put(child.getName(), pathInfo.get(nearest.getName())+"-"+child.getName());
}
}
}
computePath(start);//重复执行自己,确保所有子节点被遍历
computePath(nearest);//向外一层层递归,直至所有顶点被遍历
}
public void printPathInfo(){
SetMap.EntryString, String pathInfos=pathInfo.entrySet();
for(Map.EntryString, String pathInfo:pathInfos){
System.out.println(pathInfo.getKey()+":"+pathInfo.getValue());
}
}
/**
* 获取与node最近的子节点
*/
private Node getShortestPath(Node node){
Node res=null;
int minDis=Integer.MAX_VALUE;
MapNode,Integer childs=node.getChild();
for(Node child:childs.keySet()){
if(open.contains(child)){
int distance=childs.get(child);
if(distanceminDis){
minDis=distance;
res=child;
}
}
}
return res;
}
}
Main用于测试Dijkstra对象
[java] view plain copy
public class Main {
public static void main(String[] args) {
Dijkstra test=new Dijkstra();
Node start=test.init();
test.computePath(start);
test.printPathInfo();
}
}
Q4: java代码,多机调度问题,怎么解释
多机调度问题的Java实现(贪心算法)
具体问题描述以及C/C++实现参见网址
[java] view plain copy print?
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* 多机调度问题--贪心算法
* @author Lican
*
*/
public class JobMachine {
public static class JobNode implements Comparable{
int id;//作业的标号
int time;//作业时间
public JobNode(int id,int time){
this.id=id;
this.time=time;
}
@Override
public int compareTo(Object x) {//按时间从大到小排列
int times=((JobNode)x).time;
if(timetimes) return -1;
if(time==times) return 0;
return 1;
}
}
public static class MachineNode implements Comparable{
int id;//机器的标号
int avail;//机器空闲的时间(即机器做完某一项工作的时间)
public MachineNode(int id,int avail){
this.id=id;
this.avail=avail;
}
@Override
public int compareTo(Object o) {//升序排序贪心算法代码实现Java,LinkedList的first为最小的
int xs=((MachineNode)o).avail;
if(availxs) return -1;
if(avail==xs) return 0;
return 1;
}
}
public static int greedy(int[] a ,int m){
int n=a.length-1;//a的下标从1开始贪心算法代码实现Java,所以n(作业的数目)=a.length-1
int sum=0;
if(n=m){
for(int i=0;in;i++)
sum+=a[i+1];
System.out.println("为每个作业分别分配一台机器");
return sum;
}
ListJobNode d=new ArrayListJobNode();//d保存所有的作业
for(int i=0;in;i++){//将所有的作业存入List中,每一项包含标号和时间
JobNode jb=new JobNode(i+1,a[i+1]);
d.add(jb);
}
Collections.sort(d);//对作业的List进行排序
LinkedListMachineNode h=new LinkedListMachineNode();//h保存所有的机器
for(int i=1;i=m;i++){//将所有的机器存入LinkedList中
MachineNode x=new MachineNode(i,0);//初始时,每台机器的空闲时间(完成上一个作业的时间)都为0
h.add(x);
}
int test=h.size();
for(int i=0;in;i++){
Collections.sort(h);
MachineNode x=h.peek();
System.out.println("将机器"+x.id+"从"+x.avail+"到"+(x.avail+d.get(i).time)+"的时间段分配给作业"+d.get(i).id);
x.avail+=d.get(i).time;
sum=x.avail;
}
return sum;
}
public static void main(String[] args) {
int[] a={0,2,14,4,16,6,5,3};
int m=3;
int sum=greedy(a,m);
System.out.println("总时间为:"+sum);
}
}
/**
运行结果:
将机器1从0到16的时间段分配给作业4
将机器2从0到14的时间段分配给作业2
将机器3从0到6的时间段分配给作业5
将机器3从6到11的时间段分配给作业6
将机器3从11到15的时间段分配给作业3
将机器2从14到17的时间段分配给作业7
将机器3从15到17的时间段分配给作业1
总时间为:17
*/
贪心算法代码实现Java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于贪心算法 java、贪心算法代码实现Java的信息别忘了在本站进行查找喔。






