如何将此C ++代码转换为Java? [英] How do I convert this C++ code to java?

查看:71
本文介绍了如何将此C ++代码转换为Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< iostream>

#include< queue>

#include< vector>

using namespace std;



//确定每个节点开始的级别的函数

//来自x使用BFS

void printLevels( vector< int> graph [],int V,int x)

{

//存储每个节点级别的数组

int level [V];

bool标记为[V];



//创建一个队列

queue< int> ; que;



//入队元素x

que.push(x);



//将源节点的级别初始化为0

级别[x] = 0;



//将其标记为已访问

标记为[x] = true;



//直到队列为空

while(!que .empty()){



//获取队列的第一个元素

x = que.front();



//出列元素

que.pop();



//遍历邻居节点x

for(int i = 0; i< graph [x] .size(); i ++){

// b是节点x的邻居

int b = graph [x] [i];



//如果b未标记

if (!标记为[b]){



//将队列排队等待

que.push(b);



// b的等级是x + 1的等级

等级[b] =等级[x] + 1;



//商标b

标记为[b] = true;

}

}

}



//显示所有节点及其级别

cout<< 节点

<<

<< 等级<< endl;

for(int i = 0; i< V; i ++)

cout<< << i<< - ><< level [i]<<结束;

}



// Dirver Code

int main()

{

//树的邻接图

int V = 8;

vector< int> graph [V];



graph [0] .push_back(1);

graph [0] .push_back(2);

graph [1] .push_back(3);

graph [1] .push_back(4);

graph [1] .push_back( 5);

graph [2] .push_back(5);

graph [2] .push_back(6);

graph [6 ] .push_back(7);



//调用级别函数,源为0

printLevels(graph,V,0);



返回0;

}



我尝试了什么:



包bfsjava;





公共课BFSJAVA {





public static void printLevels( vector< int> [] graph ,int V, int x)

{

//每个节点存储级别的数组

int [] level = new int [V];

boolean [] marked = new boolean [V];



//创建一个队列

queue< ; INT> que = new queue< int>();



// enqueue element x

que.push(x);



//将源节点的级别初始化为0

级别[x] = 0;



//将其标记为已访问

标记为[x] = true;



//直到队列为空

while(!que.empty())

{



//获取队列的第一个元素

x = que.front();



//出列元素

que.pop();



//遍历节点x的邻居

for(int i = 0; i< graph [x] .size(); i ++ )

{

// b是节点x的邻居

int b = graph [x] [i];



//如果b没有标记

如果(!标记[b])

{



//在队列中排队b

que.push(b);



//等级b是x + 1 <的水平br />
等级[b] =等级[x] + 1;



//标记b

标记为[b ] = true;

}

}

}



//显示所有节点及其级别

System.out.print(节点);

System.out.print();

System.out.print(Level);

System.out.print(\ n);

for(int i = 0;我< V; i ++)

{

System.out.print();

System.out.print(i);

System.out.print( - >);

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

System.out。 print(\ n);

}

}



// Dirver Code

public static int Main()

{

//树的邻接图

int V = 8;

vector< int> graph [V];



graph [0] .push_back(1);

graph [0] .push_back( 2);

graph [1] .push_back(3);

graph [1] .push_back(4);

graph [1 ] .push_back(5);

graph [2] .push_back(5);

graph [2] .push_back(6);

graph [6] .push_back(7);



//调用级别函数,源为0

printLevels(图形,V, 0);



返回0;

}



}

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

// function to determine level of each node starting
// from x using BFS
void printLevels(vector<int> graph[], int V, int x)
{
// array to store level of each node
int level[V];
bool marked[V];

// create a queue
queue<int> que;

// enqueue element x
que.push(x);

// initialize level of source node to 0
level[x] = 0;

// marked it as visited
marked[x] = true;

// do until queue is empty
while (!que.empty()) {

// get the first element of queue
x = que.front();

// dequeue element
que.pop();

// traverse neighbors of node x
for (int i = 0; i < graph[x].size(); i++) {
// b is neighbor of node x
int b = graph[x][i];

// if b is not marked already
if (!marked[b]) {

// enqueue b in queue
que.push(b);

// level of b is level of x + 1
level[b] = level[x] + 1;

// mark b
marked[b] = true;
}
}
}

// display all nodes and their levels
cout << "Nodes"
<< " "
<< "Level" << endl;
for (int i = 0; i < V; i++)
cout << " " << i << " --> " << level[i] << endl;
}

// Dirver Code
int main()
{
// adjacency graph for tree
int V = 8;
vector<int> graph[V];

graph[0].push_back(1);
graph[0].push_back(2);
graph[1].push_back(3);
graph[1].push_back(4);
graph[1].push_back(5);
graph[2].push_back(5);
graph[2].push_back(6);
graph[6].push_back(7);

// call levels function with source as 0
printLevels(graph, V, 0);

return 0;
}

What I have tried:

package bfsjava;


public class BFSJAVA {


public static void printLevels(vector <int>[] graph, int V, int x)
{
// array to store level of each node
int[] level = new int[V];
boolean[] marked = new boolean[V];

// create a queue
queue<int> que = new queue<int>();

// enqueue element x
que.push(x);

// initialize level of source node to 0
level[x] = 0;

// marked it as visited
marked[x] = true;

// do until queue is empty
while (!que.empty())
{

// get the first element of queue
x = que.front();

// dequeue element
que.pop();

// traverse neighbors of node x
for (int i = 0; i < graph[x].size(); i++)
{
// b is neighbor of node x
int b = graph[x][i];

// if b is not marked already
if (!marked[b])
{

// enqueue b in queue
que.push(b);

// level of b is level of x + 1
level[b] = level[x] + 1;

// mark b
marked[b] = true;
}
}
}

// display all nodes and their levels
System.out.print("Nodes");
System.out.print(" ");
System.out.print("Level");
System.out.print("\n");
for (int i = 0; i < V; i++)
{
System.out.print(" ");
System.out.print(i);
System.out.print(" --> ");
System.out.print(level[i]);
System.out.print("\n");
}
}

// Dirver Code
public static int Main()
{
// adjacency graph for tree
int V = 8;
vector<int> graph[V];

graph[0].push_back(1);
graph[0].push_back(2);
graph[1].push_back(3);
graph[1].push_back(4);
graph[1].push_back(5);
graph[2].push_back(5);
graph[2].push_back(6);
graph[6].push_back(7);

// call levels function with source as 0
printLevels(graph, V, 0);

return 0;
}

}

推荐答案

你没有。在这些不同语言之间转换代码永远不会在目标语言中产生良好的代码:它会导致代码有效 - 有时 - 但难以维护甚至理解。你必须非常了解源语言和目标语言。



相反,使用代码的工作方式作为指南并使用编写新的应用程序目标语言结构和方法。

而不是找到可能适合你的家庭作业问题的代码,并试图让它工作,这样你就可以把它交给你,自己解决问题:你会得到更好的代码,这意味着更好的标记 - 你将在这个过程中学到一些东西!
You don't. Converting code between such dissimilar languages never results in good code in the target language: it results in code that works - sometimes - but is difficult to maintain or even understand. And you have to have a very good understanding of both the source and target language.

Instead, use the way the code works as a guide and write a new application using the target language structures and methods.
Instead of finding code that might fit your homework question and trying to get it to work so you can hand it in, solve the problem yourself: you will get better code, which means a better mark - and you will learn something in the process!


一个小小的研究很长的路要走:Vector(Java Platform SE 8) [ ^ ]。
A little research goes a long way: Vector (Java Platform SE 8 )[^].


这篇关于如何将此C ++代码转换为Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆