小编dra*_*sht的帖子

以o(n)复杂度向左或向右旋转一组位置

我想编写一个程序,根据用户的输入(正 - >,负< - )将数组向右或向左移动一定数量的位置.该程序必须具有O(n)复杂性.我是用这种方式写的,但它不能正常工作.在示例中,输出应为"2,3,4,5,6,1",但在我的版本中为"6,2,3,4,5,1".

#include <stdio.h>
#include <string.h>
#include <math.h>

void rotate(int *array, int N, int D);

int main(){
    int i;
    int array[] = {1, 2, 3, 4, 5, 6};

    rotate(array, sizeof(array)/sizeof(int), 5);

    for(i=0; i<sizeof(array)/sizeof(int); i++)
        printf("%d\t", array[i]);

    return 0;
}

void rotate(int *array, int N, int D){
    int i, j, *tmp, d;

    tmp = malloc(abs(D) * sizeof(int));

    if(D<0){
        d = abs(D);
        for(i=d; i<N; i++){
            tmp[i%d] = array[i];
            array[i] = array[i-d];
            array[i-d] = tmp[i%d];
        }
    }
    if(D>0){
        for(i=(N-D-1); i>=0; …
Run Code Online (Sandbox Code Playgroud)

c rotation shift

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

写入时副本的线程安全性

我试图了解开发线程安全应用程序的正确方法.

在目前的项目中,我有以下课程:

class Test
{
public:
    void setVal(unsigned int val)
    {
        mtx.lock();
        testValue = val;
        mtx.unlock();
    }

    unsigned int getVal()
    {
        unsigned int copy = testValue;
        return copy;
    }
private:
    boost::mutex mtx;
    unsigned int testValue;
}
Run Code Online (Sandbox Code Playgroud)

而我的问题是:在多线程环境中上面的方法是Test :: getVal()threadsafe,还是在复制前必须锁定?我读过一些关于COW的文章,现在我不确定.

谢谢!

c++ multithreading copy-on-write

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

在同一进程中处理线程的id

以下代码用于打印2线程linux的进程id(ubuntu 14.04)

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

void* thread_function (void* arg)
{
    fprintf (stderr, "child thread pid is %d\n", (int) getpid ());
    /* Spin forever. */
    while (1);
    return NULL;
}

int main ()
{
    pthread_t thread; 
    fprintf (stderr, "main thread pid is %d\n", (int) getpid ());
    pthread_create (&thread, NULL, &thread_function, NULL);
    /* Spin forever. */
    while (1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是

main thread pid is 3614
child thread pid is 3614
Run Code Online (Sandbox Code Playgroud)

但是不应该是因为GNU/Linux的进程id不同,线程是作为进程实现的?

c linux pthreads

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

为什么对象"被破坏"了两次?

在下面的代码中,析构函数a被调用两次,并且第一次调用似乎被忽略:

struct A1
{
    int A;
    A1(int a=0) : A(a) { std::cout << "ctor: " << A << "\n"; std::cout.flush(); }
    ~A1() { std::cout << "dtor: " << A << "\n"; std::cout.flush(); }
};


int main()
{
    A1 a(1), *pa=new A1(2), *pb=new A1(3);

    a.~A1();
    pa->~A1();
    delete pb;
    std::cout << "'destructed' a.A = " << a.A << "\n"; std::cout.flush();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

ctor: 1
ctor: 2
ctor: 3
dtor: 1
dtor: 2
dtor: 3
'destructed' a.A = 1 …
Run Code Online (Sandbox Code Playgroud)

c++ destructor

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

使用fscanf()跳过字符串

输入文件是这样的,它在第一行有一个字符串后跟一个整数,从第二行开始,它有一个字符串后跟2个整数.我的下面的代码运行良好,但有没有办法跳过字符串?我只是用一些字符数组char sink [30]扫描它.实际上我不需要这个值如何使用fscanf()来跳过这个字符串并只读取整数.

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

int main()
{
    int v,i=0,f=1;
    static int *p,*q;
    FILE *fp;
    char sink[30];
    fp = fopen("some.txt","r");

    while(!feof(fp))
    {   
        if(f)
        {   
            fscanf(fp,"%s %d",sink,&v);
            p = (int *)malloc(sizeof(int)*v);
            q = (int *)malloc(sizeof(int)*v);
            f=0;
        }   
        else
        {   
            fscanf(fp,"%s %d %d",sink,&p[i],&q[i]);
            i++;
        }   
    }   

    fclose(fp);

    printf("The input vertices are\n");
    for(i=0;i<v;i++)
        printf("%d %d\n",p[i],q[i]);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c

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

当增加1个Java时,线程没有完成

下面的代码工作正常,但我希望值到达数组的末尾,因为它从1开始然后到数组的末尾.

Booth[] boot = new Booth[numberOfBooths];


for (int j = 1; j < boot.length; j++) {
    boot[j] = new Booth(j, buff);
    boot[j].start();
}
for (int j =1 ; j < boot.length; j++) {
    try {
        boot[j].join();
    } catch (InterruptedException ex) {
        System.out.println(ex);
    }
}
Run Code Online (Sandbox Code Playgroud)

我改变了代码,所以循环从0开始..这样:

for (int j = 0; j < boot.length; j++) {
    boot[j] = new Booth(j, buff);
    boot[j].start();
}
for (int j =0 ; j < boot.length; j++) {
    try {
        boot[j].join();
    } catch (InterruptedException ex) { …
Run Code Online (Sandbox Code Playgroud)

java multithreading

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

使用递归查找数组的最小元素

我编写了以下程序,以使用递归从数组中查找最小值元素.然而,该程序继续向我显示答案为1000

#include<stdio.h>
#define MAX 100

int getminElement(int []);
int size;

int main(){

    int min;
    int i;
    int a[10]={12,6,-24,78,43,3,22,45,40};

    min=getminElement(a);

    printf("Smallest element of an array is: %d",min);

    return 0;
}

int getminElement(int a[]){

    static int i=0,min =1000;

    if(i < size){
        if(min > a[i])
            min=a[i];
        i++;
        getminElement(a);
    }

    return min;
}
Run Code Online (Sandbox Code Playgroud)

c arrays recursion min

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

这是一个未定义的行为吗?

所以我只是想问一下,添加注释行时这是一个未定义的行为.虽然没有编译错误,但它们都给出了相同的答案.我想知道有什么不同.地址是否被a的地址覆盖.另外如果有人这样做(即为b分配内存),那将memcpy()是一个很好的解决方案.这可能是一个微不足道的例子,但我想了解其中的区别.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int *a;
    int *b;
    a=malloc(sizeof(int));
    //b=malloc(sizeof(int));
    int c=6;
    a=&c;
    b=a;
    printf("%d\n",*b);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c malloc pointers undefined-behavior

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

C,从文件读入数组,按编号读取

void readfromFile() {

    FILE *myFile;
    myFile = fopen("matrikel.txt", "r");

    //read file into array
    int numberArray[12];
    int i;

    if (myFile == NULL) {
        printf("Error Reading File\n");
        exit (0);
    }

    for (i = 0; i < 12; i++) {
        fscanf(myFile, "%d,", &numberArray[i] );
    }

    for (i = 0; i < 1; i++) {
        printf("Number is: %d\n\n", numberArray[i]);
    }

    fclose(myFile);
}
Run Code Online (Sandbox Code Playgroud)

"matrikel.txt"包含

808098822790 
Run Code Online (Sandbox Code Playgroud)

这个数字似乎太长了int numberArray[12],在运行代码时会输出一个随机数.当从它所使用的数字的末尾剪切一些单个整数时,最大长度似乎是9.

我不太确定但不应该fscanf在第一个for循环中将一个数字的数字打印到每个单元格中numberArray[]

c arrays scanf

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

在C中反转一个字符串

#include <stdio.h>
void reverse(int len, char s[], char b[]);

int main() {
    char s[5] = "hello";
    char b[5];
    reverse(5, s, b);
    return 0;
}

void reverse(int len, char s[], char b[]) {
    int i;
    for (i = 0; i < len; i++) {
        b[(len - 1) - i] = s[i];
    }
    printf("%s : %s\n", s, b);
    printf("%s", b);
}
Run Code Online (Sandbox Code Playgroud)

这是我上面的代码C.当我运行它时,它将字符串切换s[]b[]b[]也包括s[].有人可以请彻底解释我做错了什么吗?对此,我真的非常感激.

c arrays string reverse pointers

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