小编lin*_*usz的帖子

malloced数组VS. 可变长度数组

有两种方法可以为数组分配内存,其大小在开始时是未知的.最常见的方式是使用malloc这样的

int * array;
... // when we know the size
array = malloc(size*sizeof(int));
Run Code Online (Sandbox Code Playgroud)

但是在我们知道尺寸后,在C99中定义数组也是有效的.

... // when we know the size
int array[size];
Run Code Online (Sandbox Code Playgroud)

他们完全一样吗?

c malloc variable-length-array

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

完全k-partite图中的最大权重k-clique

我的问题

是否有一种有效的算法可以在一个完整的k -partite图中找到最大权重(或最小权重)k - clique(当且仅当顶点根据维基百科属于不同的部分集时,顶点是相邻的图)?

有关条款的更多详细信息

Max-weight Clique:图中的每条边都有一个重量.集团的权重是集团中所有边缘的权重之和.目标是找到一个具有最大重量的集团.

请注意,clique的大小是k,它是完整k-partite图中最大可能的clique大小.

我试过了什么

我在项目期间遇到了这个问题.由于我不是CS人,我不确定复杂性等.

我搜索了几篇相关的论文,但没有一篇论文涉及同样的问题.我还编写了一个贪心算法+模拟退火处理它(结果似乎不好).我也尝试过类似动态编程的东西(但它看起来效率不高).所以我想知道是否可以有效地计算出精确的最优值.提前致谢.

编辑由于我的输入可能非常大(例如每个集团中的顶点数量是2 ^ k),我希望找到一个非常快速的算法(例如,k的及时多项式)来计算出最优结果.如果不可能,我们可以证明复杂性的下限吗?

algorithm complexity-theory np-hard clique-problem

9
推荐指数
2
解决办法
1429
查看次数

如果没有明确的"返回",函数将返回什么

当我忘记编写return函数的子句时,我遇到了这个问题,但是没有警告或错误gcc.我修复了它,但开始想知道为什么函数会在没有a的情况下返回无意义的东西return.以下是我尝试的一些示例:

#include "stdio.h"
#include "stdlib.h"

int func1 () {
        int i;
        i = 2;
}

int func2 (int a) {
        int i = a+3;
}

int func3 () {
        int i;
        for (i = 0; i <= 1; i++);
}

int main(void) {
        int a = 0;
        int b = 0;
        int c = 0;
        a = func1();
        printf("a = %d \n", a);
        b = func2(a);
        printf("b = %d \n", b);
        c …
Run Code Online (Sandbox Code Playgroud)

c return function undefined-behavior

3
推荐指数
2
解决办法
1979
查看次数

为什么我在初始化结构时会遇到段错误?

搜索了一个小时左右.我想我最好在这里发布这个问题.

我简化了代码.段错误在功能中initMyStruct.

#include "stdlib.h"

typedef struct {
        int * arr1;
        int * arr2;
} myStruct;

void allocMyStruct (myStruct * a, int num) {
        a = malloc(sizeof(myStruct));
        a->arr1 = malloc(10*sizeof(int));
        a->arr2 = malloc(10*num*sizeof(int));
}
void initMyStruct (myStruct * a, int num) {
        int i;
        for (i = 0; i < 10; i++)     a->arr1[i]  =  0;
        for (i = 0; i < 10*num; i++) a->arr2[i]  = -1;
}
void freeMyStruct (myStruct * a, int num) {
        int i;
        for (i …
Run Code Online (Sandbox Code Playgroud)

c malloc struct segmentation-fault

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

std :: cout行为不一致

我似乎std::cout在打印多个内容时不能始终如一地工作,如以下两个示例所示.我认为它可能与缓冲区刷新有关,但即使我std::flush在测试示例中添加了一些数据也没有任何区别.

#include <cstdlib>                                                                                                   
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

void test(const std::string& f1);

int main(void) {
    std::string a = "a";
    std::cout << a <<  a << a << std::endl; 
    // Then I got aaa on the screen, which is as expected. 

    test("inputfile");
    // The input file contains one character: "a" 
    // after running the test I got only one "a" on the screen
    // even though the string is repeated three times in the …
Run Code Online (Sandbox Code Playgroud)

c++ cout

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