小编Err*_*404的帖子

Prim在图上的算法,每个边上的权重仅为1和2,使用两个列表

给定加权的,连通的,简单的无向图G,每个边上的权重仅为1和2

我想以这种方式实现Prim的算法:

权重是1或2,所以我可以简单地将边缘存储在2个单独的列表中,一个用于权重为1的边缘,第二个用于权重为2的边缘.

要找到权重最小的边,我只需从第一个列表中取一个,除非它是空的,在这种情况下,我从第二个列表中取一个边.

访问和删除列表中的元素是O(1),因此Prim的算法将在O(V + E)中运行.

package il.ac.oranim.alg2016;

import edu.princeton.cs.algs4.*; 

public class MST12 {    
    private int weight; // weight of the tree
    private Edge[] mstEdges; // use this to store the edges of your Minimum Spanning Tree

    public MST12(EdgeWeightedGraph G, int s)  throws IndexOutOfBoundsException, DisconnectedGraphException, WrongWeightException {
        // check that the starting vertex is in the range 0,1,...,G.V()
        if (s < 0 || s >= G.V()) {
            throw new IndexOutOfBoundsException();
        }
        // check that the input graph …
Run Code Online (Sandbox Code Playgroud)

java eclipse algorithm graph minimum-spanning-tree

5
推荐指数
1
解决办法
1247
查看次数

带十进制输入的递归函数打印从零到n的所有数字以二进制形式输出

我的作业是编写一个递归函数,打印从零到n输入整数的所有二进制数.禁止使用循环,静态变量,数组,全局变量.

例如,如果输入为7,则输出应为:

000
001
010
011
100
101
110
111
Run Code Online (Sandbox Code Playgroud)

我的输出:

1
10
11
100
101
110
111
Run Code Online (Sandbox Code Playgroud)

如何用这些零修复输出?

这是我的代码:

#include <stdio.h>


void convert(int num)//converts decimal number to binary
{
    if(num>0)
    {
    convert(num/2);
    printf("%d", num%2);
    }
}

void print_binary_number(int num)
{
    if(num<0)
       return;
    print_binary_number(num-1);
     printf("\n");
    convert(num);

}

int main()
{

    int num;
    printf("Please enter an integer:");
    scanf("%d", &num);
    print_binary_number(num);
    return 0;

}
Run Code Online (Sandbox Code Playgroud)

c recursion

1
推荐指数
1
解决办法
233
查看次数

即使有一个while(true)循环,也要停止GUI

我无法停止我的应用程序,因为我有一个while循环,所以gui不让我点击停止按钮,它看起来像:

private void btnStart_Click(object sender, EventArgs e)
{   while(true)
    { 
     //some code here
    }
}

//some methods here

private void btnStop_Click(object sender, EventArgs e)
{
 Application.Exit();
}
Run Code Online (Sandbox Code Playgroud)

c#

1
推荐指数
1
解决办法
115
查看次数

标签 统计

algorithm ×1

c ×1

c# ×1

eclipse ×1

graph ×1

java ×1

minimum-spanning-tree ×1

recursion ×1