完善下列代码#include <stdio.h> const int N = 10; //判断无向图是否欧拉图 //返回值为奇度数结点数 int euler1(int** a, int n) { //begin************* //end**************** } int main( ) { int** a1;//无向图 int n, i, j; scanf("%d", &n);//读入结点数 if (n > N) { printf("error\n"); return 0; } a1 = new int* [n]; for (i = 0; i < n; i++) { a1[i] = new int[n]; for (j = 0; j < n; j++) { a1[i][j] = 0; } } while (1) { scanf("%d%d", &i, &j);//读入无向边 //begin***** //end*********** } int odd = euler1(a1, n); if (odd == 0) { printf("a is euler\n"); } else if (odd == 2) printf("a is semi-euler\n"); else printf("a is not euler\n"); },
时间: 2023-07-23 16:25:34 浏览: 48
代码如下:
```c++
#include <stdio.h>
const int N = 10;
//判断无向图是否欧拉图
//返回值为奇度数结点数
int euler1(int** a, int n) {
int odd_count = 0;
for (int i = 0; i < n; i++) {
int degree = 0;
for (int j = 0; j < n; j++) {
degree += a[i][j];
}
if (degree % 2 == 1) {
odd_count++;
}
}
return odd_count;
}
int main() {
int** a1; //无向图
int n, i, j;
scanf("%d", &n); //读入结点数
if (n > N) {
printf("error\n");
return 0;
}
a1 = new int* [n];
for (i = 0; i < n; i++) {
a1[i] = new int[n];
for (j = 0; j < n; j++) {
a1[i][j] = 0;
}
}
while (1) {
scanf("%d%d", &i, &j); //读入无向边
if (i == -1 && j == -1) {
break;
}
a1[i][j] = 1;
a1[j][i] = 1;
}
int odd = euler1(a1, n);
if (odd == 0) {
printf("a is euler\n");
} else if (odd == 2) {
printf("a is semi-euler\n");
} else {
printf("a is not euler\n");
}
return 0;
}
```
程序实现了判断无向图是否为欧拉图的功能。其中,`euler1`函数传入邻接矩阵和结点数,返回奇度数结点数。在主函数中,首先读入结点数,并初始化邻接矩阵;然后读入各个无向边,并在邻接矩阵中标记相应位置;最后调用`euler1`函数判断无向图是否为欧拉图,根据返回值输出相应结果。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)