把C语言转成Java#include "stdio.h" #define maxver 10 #define maxright 100 int G[maxver][maxver],record=0,touched[maxver][maxver]; int circle=0; int FindCircle(int,int,int,int); int main() { int path[maxver][2],used[maxver][maxver]; int i,j,k,t,min=maxright,exsit=0; int v1,v2,num,temp,status=0; restart: printf("Please enter the number of vertex(s) in the graph:\n"); scanf("%d",&num); if(num>maxver||num<0) { printf("Error!Please reinput!\n"); goto restart; } for(j=0; j<num; j++) for(k=0; k<num; k++) { if(j==k) { G[j][k]=maxright; used[j][k]=1; touched[j][k]=0; } else if(j<k) { re: printf("Please input the right between vertex %d and vertex %d,if no edge exists please input -1:\n",j+1,k+1); scanf("%d",&temp); if(temp>=maxright||temp<-1) { printf("Invalid input!\n"); goto re; } if(temp==-1) temp=maxright; G[j][k]=G[k][j]=temp; used[j][k]=used[k][j]=0; touched[j][k]=touched[k][j]=0; } } for(j=0; j<num; j++) { path[j][0]=0; path[j][1]=0; } for(j=0; j<num; j++) { status=0; for(k=0; k<num; k++) if(G[j][k]<maxright) { status=1; break; } if(status==0) break; } for(i=0; i<num-1&&status; i++) { for(j=0; j<num; j++) for(k=0; k<num; k++) if(G[j][k]<min&&!used[j][k]) { v1=j; v2=k; min=G[j][k]; } if(!used[v1][v2]) { used[v1][v2]=1; used[v2][v1]=1; touched[v1][v2]=1; touched[v2][v1]=1; path[i][0]=v1; path[i][1]=v2; for(t=0; t<record; t++) FindCircle(path[t][0],path[t][0],num,path[t][0]); if(circle) { /*if a circle exsits,roll back*/ circle=0; i--; exsit=0; touched[v1][v2]=0; touched[v2][v1]=0; min=maxright; } else { record++; min=maxright; } } } if(!status) printf("We cannot deal with it because the graph is not connected!\n"); else { for(i=0; i<num-1; i++) printf("Path %d:vertex %d to vertex %d\n",i+1,path[i][0]+1,path[i][1]+1); } return 1; } int FindCircle(int start,int begin,int times,int pre) { /* to judge whether a circle is produced*/ int i; for(i=0; i<times; i++) if(touched[begin][i]==1) { if(i==start&&pre!=start) { circle=1; return 1; break; } else if(pre!=i) FindCircle(start,i,times,begin); else continue; } return 1; }
理解问题把C语言转成Java#include "stdio.h" #define maxver 10 #define maxright 100 int G[maxver][maxver],record=0,touched[maxver][maxver]; int circle=0; int FindCircle(int,int,int,int); int main() { int path[maxver][2],used[maxver][maxver]; int i,j,k,t,min=maxright,exsit=0; int v1,v2,num,temp,status=0; restart: printf("Please enter the number of vertex(s) in the graph:\n"); scanf("%d",&num); if(num>maxver||num<0) { printf("Error!Please reinput!\n"); goto restart; } for(j=0; j<num; j++) for(k=0; k<num; k++) { if(j==k) { G[j][k]=maxright; used[j][k]=1; touched[j][k]=0; } else if(j<k) { re: printf("Please input the right between vertex %d and vertex %d,if no edge exists please input -1:\n",j+1,k+1); scanf("%d",&temp); if(temp>=maxright||temp<-1) { printf("Invalid input!\n"); goto re; } if(temp==-1) temp=maxright; G[j][k]=G[k][j]=temp; used[j][k]=used[k][j]=0; touched[j][k]=touched[k][j]=0; } } for(j=0; j<num; j++) { path[j][0]=0; path[j][1]=0; } for(j=0; j<num; j++) { status=0; for(k=0; k<num; k++) if(G[j][k]<maxright) { status=1; break; } if(status==0) break; } for(i=0; i<num-1&&status; i++) { for(j=0; j<num; j++) for(k=0; k<num; k++) if(G[j][k]<min&&!used[j][k]) { v1=j; v2=k; min=G[j][k]; } if(!used[v1][v2]) { used[v1][v2]=1; used[v2][v1]=1; touched[v1][v2]=1; touched[v2][v1]=1; path[i][0]=v1; path[i][1]=v2; for(t=0; t<record; t++) FindCircle(path[t][0],path[t][0],num,path[t][0]); if(circle) { /*if a circle exsits,roll back*/ circle=0; i--; exsit=0; touched[v1][v2]=0; touched[v2][v1]=0; min=maxright; } else { record++; min=maxright; } } } if(!status) printf("We cannot deal with it because the graph is not connected!\n"); else { for(i=0; i<num-1; i++) printf("Path %d:vertex %d to vertex %d\n",i+1,path[i][0]+1,path[i][1]+1); } return 1; } int FindCircle(int start,int begin,int times,int pre) { /* to judge whether a circle is produced*/ int i; for(i=0; i<times; i++) if(touched[begin][i]==1) { if(i==start&&pre!=start) { circle=1; return 1; break; } else if(pre!=i) FindCircle(start,i,times,begin); else continue; } return 1; }
已完成理解「把C语言转成Java#include "stdio.h" #define maxver 10 #define maxright 100 int G[maxver][maxver],record=0,touched[maxver][maxver]; int circle=0; int FindCircle(int,int,int,int); int main() { int path[maxver][2],used[maxver][maxver]; int i,j,k,t,min=maxright,exsit=0; int v1,v2,num,temp,status=0; restart: printf("Please enter the number of vertex(s) in the graph:\n"); scanf("%d",&num); if(num>maxver||num<0) { printf("Error!Please reinput!\n"); goto restart; } for(j=0; j<num; j++) for(k=0; k<num; k++) { if(j==k) { G[j][k]=maxright; used[j][k]=1; touched[j][k]=0; } else if(j<k) { re: printf("Please input the right between vertex %d and vertex %d,if no edge exists please input -1:\n",j+1,k+1); scanf("%d",&temp); if(temp>=maxright||temp<-1) { printf("Invalid input!\n"); goto re; } if(temp==-1) temp=maxright; G[j][k]=G[k][j]=temp; used[j][k]=used[k][j]=0; touched[j][k]=touched[k][j]=0; } } for(j=0; j<num; j++) { path[j][0]=0; path[j][1]=0; } for(j=0; j<num; j++) { status=0; for(k=0; k<num; k++) if(G[j][k]<maxright) { status=1; break; } if(status==0) break; } for(i=0; i<num-1&&status; i++) { for(j=0; j<num; j++) for(k=0; k<num; k++) if(G[j][k]<min&&!used[j][k]) { v1=j; v2=k; min=G[j][k]; } if(!used[v1][v2]) { used[v1][v2]=1; used[v2][v1]=1; touched[v1][v2]=1; touched[v2][v1]=1; path[i][0]=v1; path[i][1]=v2; for(t=0; t<record; t++) FindCircle(path[t][0],path[t][0],num,path[t][0]); if(circle) { /*if a circle exsits,roll back*/ circle=0; i--; exsit=0; touched[v1][v2]=0; touched[v2][v1]=0; min=maxright; } else { record++; min=maxright; } } } if(!status) printf("We cannot deal with it because the graph is not connected!\n"); else { for(i=0; i<num-1; i++) printf("Path %d:vertex %d to vertex %d\n",i+1,path[i][0]+1,path[i][1]+1); } return 1; } int FindCircle(int start,int begin,int times,int pre) { /* to judge whether a circle is produced*/ int i; for(i=0; i<times; i++) if(touched[begin][i]==1) { if(i==start&&pre!=start) { circle=1; return 1; break; } else if(pre!=i) FindCircle(start,i,times,begin); else continue; } return 1; }」
展开阅读网页
要将C语言代码转换为Java,需要考虑几个关键点:数据类型、语法结构、数组和循环的使用。下面是将提供的C语言代码转换为Java的示例:
import java.util.Scanner;
public class GraphMST {
private static final int MAX_VER = 10;
private static final int MAX_RIGHT = 100;
private int[][] G;
private int record;
private int[][] touched;
private int circle;
private int[][] used;
public GraphMST() {
G = new int[MAX_VER][MAX_VER];
touched = new int[MAX_VER][MAX_VER];
used = new int[MAX_VER][MAX_VER];
record = 0;
circle = 0;
}
public int findCircle(int start, int begin, int times, int pre) {
/* to judge whether a circle is produced */
for (int i = 0; i < times; i++) {
if (touched[begin][i] == 1) {
if (i == start && pre != start) {
circle = 1;
return 1;
} else if (pre != i) {
findCircle(start, i, times, begin);
}
}
}
return 0;
}
public void main() {
Scanner scanner = new Scanner(System.in);
int path[][] = new int[MAX_VER][2];
int num, temp, min = MAX_RIGHT, status = 0;
int v1, v2;
restart:
System.out.print("Please enter the number of vertices in the graph:\n");
num = scanner.nextInt();
if (num > MAX_VER || num < 0) {
System.out.print("Error! Please reinput!\n");
goto restart;
}
for (int j = 0; j < num; j++) {
for (int k = 0; k < num; k++) {
if (j == k) {
G[j][k] = MAX_RIGHT;
used[j][k] = 1;
touched[j][k] = 0;
} else if (j < k) {
System.out.printf("Please input the weight between vertex %d and vertex %d, if no edge exists please input -1:\n", j + 1, k + 1);
temp = scanner.nextInt();
if (temp >= MAX_RIGHT || temp < -1) {
System.out.print("Invalid input!\n");
continue;
}
if (temp == -1) temp = MAX_RIGHT;
G[j][k] = G[k][j] = temp;
used[j][k] = used[k][j] = 0;
touched[j][k] = touched[k][j] = 0;
}
}
}
for (int j = 0; j < num; j++) {
path[j][0] = 0;
path[j][1] = 0;
}
for (int j = 0; j < num; j++) {
status = 0;
for (int k = 0; k < num; k++) {
if (G[j][k] < MAX_RIGHT) {
status = 1;
break;
}
}
if (status == 0) break;
}
for (int i = 0; i < num - 1 && status != 0; i++) {
for (int j = 0; j < num; j++) {
for (int k = 0; k < num; k++) {
if (G[j][k] < min && used[j][k] == 0) {
v1 = j;
v2 = k;
min = G[j][k];
}
}
}
if (used[v1][v2] == 0) {
used[v1][v2] = 1;
used[v2][v1] = 1;
touched[v1][v2] = 1;
touched[v2][v1] = 1;
path[i][0] = v1;
path[i][1] = v2;
findCircle(v1, v1, num, -1);
if (circle != 0) {
/* if a circle exists, roll back */
circle = 0;
i--;
touched[v1][v2] = 0;
touched[v2][v1] = 0;
min = MAX_RIGHT;
} else {
record++;
min = MAX_RIGHT;
}
}
}
if (status == 0) {
System.out.println("We cannot deal with it because the graph is not