小编use*_*109的帖子

pthread互斥锁无法正常工作

我目前正在从麻省理工学院开放课件中学习C语言,在C语言中称为实践编程.在讨论多线程中的竞争条件时,讲义包含一个具有竞争条件的程序示例以及如何使用互斥锁解决它.代码在Linux系统上按预期工作,但在OS X上没有.

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

pthread_mutex_t mutex; // Added to fix race condition
unsigned int cnt = 0;

void *count(void *arg) {
    int i;
    for (i = 0; i < 100000000; i++) {
        pthread_mutex_lock(&mutex); // Added to fix race condition
        cnt++;
        pthread_mutex_unlock(&mutex); // Added to fix race condition
    }
    return NULL;
}

int main() {
    pthread_t tids[4];
    int i;
    for (i = 0; i < 4; i++)
        pthread_create(&tids[i], NULL, count, NULL);
    for (i = 0; i < …
Run Code Online (Sandbox Code Playgroud)

c multithreading mutex pthreads

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

如何检查所有可能的数组矩形的总和

我们假设我们有一个二维数组A(n X n).A的所有元素都是O或1.我们还有一个给定的整数K.我们的任务是找到A中所有可能的"矩形"的数量,其中包含总和为K的元素.

To give an example , if A = 
0 0 1 0 
1 0 0 1
1 1 1 1
1 0 0 1   and k=3 ,

0 0 1 0
1 0 0 1  holds the property ,

1 1 1 holds the property ,

  1 1 1 holds the property ,

0 0 
1 0 
1 1 holds the property ,

1 1
1 0  holds the property ,

    1 1
    0 1  holds the …
Run Code Online (Sandbox Code Playgroud)

arrays algorithm

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

如何计算有向图中的所有可到达节点?

有一个有向图(可能包含循环),每个节点都有一个值,我们怎么能得到每个节点的可达值之和.例如,在下图中:

有向图

节点1的可达和为:2 + 3 + 4 + 5 + 6 + 7 = 27

节点2的可达和为:4 + 5 + 6 + 7 = 22

.....

我的解决方案:为了获得所有节点的总和,我认为时间复杂度为O(n + m),n是节点数,m代表边数.应该使用DFS,对于每个节点,我们应该递归地使用一个方法来找到它的子节点,并在完成它的计算时保存子节点的总和,以便将来我们不需要再次计算它.需要为每个节点创建一个集合,以避免由循环引起的无限计算.

它有用吗?我认为它不够优雅,特别是必须创建许多套装.有没有更好的解决方案?谢谢.

algorithm graph directed-graph

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

Initialization of variables from within the definition by referencing the type or a variable of that type

Is such heavy use of backreferencing a declaration from the initialization code covered by at least one of the standards (C99-current) or is it a gcc extension? All the member initialization terms have in common that they either reference the type or a member/variable of that type from within the definition of the type.

#include <stddef.h>
#include <stdlib.h>

struct node{
    int size;
    int offset;
    int *ptr;
    struct node *this;
} s = {sizeof(s), offsetof(struct node, offset), &s.offset, &s};

int main(void){ …
Run Code Online (Sandbox Code Playgroud)

c initialization declaration definition

4
推荐指数
2
解决办法
56
查看次数

scanf 忽略,无限循环

int flag = 0;
int price = 0;
while (flag==0)
{
    printf("\nEnter Product price: ");
    scanf("%d",&price);
    if (price==0) 
        printf("input not valid\n"); 
    else 
        flag=1;
}
Run Code Online (Sandbox Code Playgroud)

当我输入一个有效数字时,循环按预期结束。但是如果我输入的不是数字,比如hello,那么代码就会进入无限循环。它只是继续打印Enter Product price:input not valid。但它不会等我输入一个新号码。这是为什么?

c scanf infinite-loop

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

如何用垃圾收集语言(例如Java/Scala)替换/实现RAII?

在Java和C#等语言中,存在"析构函数"的概念.RAII如何与这个概念正交,以及如何在这些语言中实现?

c# java programming-languages d

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

C printf 浮点数格式

我尝试了以下代码

int main()
{
    float a = 1.0, b = 25.16;
    printf("%2.1f\n", a);
    printf("%2.1f\n", b);
}
Run Code Online (Sandbox Code Playgroud)

我期望结果为:

 1.0
25.2
Run Code Online (Sandbox Code Playgroud)

相反,它显示:

1.0
25.2
Run Code Online (Sandbox Code Playgroud)

为什么不排队呢?

c

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

为什么在第二个循环中不进行for循环打印?

我做了一个for循环,将名称存储在用户选择其大小的数组中,但是当for循环运行时,它会跳过第二条printf语句。

int NumToDelete;
printf("How much employees do you want to remove?\n");
scanf(" %d", &NumToDelete);
char Name[NumToDelete][25];
for(int i = 0; i < NumToDelete; i++)
{
    fgetc(stdin);      //To stop the program from doing
    printf("Name: ");  //something like this: Name:Name:
    fgets(Name[i], 25, stdin);
}
Run Code Online (Sandbox Code Playgroud)

提示和用户输入应如下所示(如果NumToDelete为3):

Name: Ahmed
Name: John
Name: Bob
Run Code Online (Sandbox Code Playgroud)

但是,相反,在输入名称“ Ahmed”之后,必须输入第二个名称“ John”,然后代码再次显示“名称:”提示。因此,控制台中的文本最终看起来像这样:

Name: Ahmed
John
Name: Bob
Run Code Online (Sandbox Code Playgroud)

名称是用户输入。先感谢您。

c arrays string stdin for-loop

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

没有排序的前 K 个子集和

给定一个大小为 N 的数组,(0<K<=N)按元素总和的递增顺序打印大小为 K 的所有子集

Array:
  [6,8,3,9], N=4, K=3
Sorted Subsets:
  [3, 6, 8] (sum=17)
  [3, 6, 9] (sum=18)
  [3, 8, 9] (sum=20)
  [6, 8, 9] (sum=23)
Run Code Online (Sandbox Code Playgroud)

我不需要整个排序列表,而是需要前 T 个条目(T 很小)。列出所有子集(nCk)并对它们进行排序对于大 N 来说非常昂贵。有没有办法在不实际枚举所有子集的情况下获得前 T 个子集?我正在考虑选择最小的 K 个元素,这是最小的子集,然后找到一种方法通过替换一个或多个元素来获得下一个最小的子集,但是替换的选择又太多了。

arrays algorithm subset

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

公差标准 Brent 方法

布伦特方法的停止条件是

if abs(m) <= tol or fb == 0.0 then    // root found (interval is small enough) 
    found := true;
Run Code Online (Sandbox Code Playgroud)

但是,如果abs(m)达到低于所述容差但 的值f(b)不接近零怎么办?这种情况会被认为是收敛失败还是收敛成功?我可以看到abs(m) < tolerance,即|b-a| < tolerance,但函数的值不等于零或任何接近的值。不是布伦特的方法的全部意义在于找到一个函数的根,使得f(b) == 0.0或低于某个容差?

|b-a| < tolerance即使函数的值不接近于零,即低于给定的容差,当实现收敛时是否总是这样?

algorithm math root numerical-methods modelica

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