小编chq*_*lie的帖子

编写一个函数,对于给定的非负整数 n,以数字形式返回 9 出现的次数

我无法解决这个问题,几天来我一直在努力解决这个问题。这是问题的全文:

编写一个函数,对于给定的非负 int ,以数字形式n返回 出现的次数,但 a紧邻其左侧的另一个会计数为双倍,因此结果为 4。9999912349

该问题有两部分:a) 和 b)。

a) 部分要求使用递归来解决该问题,并且

b) 部分需要迭代。

我主要在递归问题上遇到麻烦。这是我的 a) 部分的代码:

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

/* Write a function that for a given a non-negative int n, computes the count of the 
occurrences of 9 as a digit, except
that an 9 with another 9 immediately to its left counts double, so 9914329 yields 4. */

int recursiveNines(int mynumber) {
    int counter = 0;
    if (mynumber = 0) …
Run Code Online (Sandbox Code Playgroud)

c iteration recursion function

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

在Windows和Linux中使用fscanf时,行尾不同的字符

当我读到fscanf:

 nscan = fscanf(input_file, "%30[^,], %d, %lf, %d%c",
                array[i].epwnymo, &array[i].ypolipo,
                &array[i].epitokio, &array[i].meromhnia, &termch);
Run Code Online (Sandbox Code Playgroud)

termch应该是\n,但在Linux中,我得到了\r哪些使我的程序给出了错误的结果.我已经阅读过某个窗口\n,\r\n但是,为什么我会\r进入Linux?

c linux windows newline

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

free()内存在单链表中使用'for'循环

这是我的完整计划:

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

#define kArraySize 50
#define kFirstElement 0

struct Name {
    char name[kArraySize + 1];
    struct Name *nextName;
} *gFirstNameNode, *gLastNameNode;

char GetName(struct Name *currentName);
void AddToList(struct Name *currentName);
void PrintList(struct Name *gFirst);
void FreeTheMemory(struct Name *gFirst);

int main(void) {
    struct Name *currentName;
    char character;
    int counter;
    gFirstNameNode = NULL;
    gLastNameNode = NULL;

    do {
        currentName = malloc(sizeof(struct Name));
        if (currentName == NULL) {
            printf("Out of Memory!\n");
            exit(0);
        } else {
            for (counter = 0; counter <= …
Run Code Online (Sandbox Code Playgroud)

c singly-linked-list

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

无法在C中声明struct里面的struct指针数组

我想在struct中有一个数组,它将存储相同数据类型(即struct map)的指针.我查看了Stackoverflow,发现了这个:

struct map {
    int city;
    struct map **link = (struct map *)malloc(204800 * sizeof(struct map *));
}
Run Code Online (Sandbox Code Playgroud)

但是我收到了这个错误: -

error: expected ':', ',', ';', '}' or '__attribute__' before '=' token    
    struct map **link = (struct map *)malloc(204800*sizeof(struct map *));
Run Code Online (Sandbox Code Playgroud)

c arrays struct pointers

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

在C中获得不寻常的循环输出

我得到一个不寻常的输出下面的循环.当它在我的编译器中运行时,输出为233445.为什么我在下面的循环中获得此输出?7这将是这个循环唯一合理的输出,为什么7printf语句在循环中时我不能得到?

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

int main() {
    int i;
    int j;

    for (i = 1; i < 4; i++)
        for (j = 1; j < 3; j++)
            printf("%d", i + j); 
}
Run Code Online (Sandbox Code Playgroud)

c

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

pow功能出错

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

void main() {
    int decimal, count, binary, digit;
    printf("Enter the number : ");
    scanf("%d", &decimal);
    count = 0; binary = 0;
    while (decimal > 0) {
        digit = decimal % 2;
        binary = binary + digit * pow(10, count);
        decimal = decimal / 2;
        ++count;
    }
    printf("Binary form : %d", binary);
}
Run Code Online (Sandbox Code Playgroud)

我使用上面的代码将Decimal转换为二进制.但问题是输出.

Input           : 12
Expected Output : 1100
Actual Output   : 1099
Run Code Online (Sandbox Code Playgroud)

[img] https://i.imgur.com/1mZlMQN.png [/ img ]

其他输入也存在此问题.只有8给出正确的输出.

有人可以解释为什么会这样吗?当我将它移植到那里时,这个错误也出现在C++中.

PS:pow在检查数字是否是阿姆斯特朗以及是否为回文时,也会弹出此错误.

c

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

为什么我的 %[^\n] 不能正常工作?

int main() {
    char a[100];

    printf("\nEnter 1st Sentence : ");
    scanf("%[^\n]", a);
    printf("\nSentence 1 : %s", a);

    printf("\nEnter 2nd Sentence : ");
    scanf("%[^\n]", a);
    printf("\nSentence 2 : %s", a);

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

我的输出:

Enter 1st Sentence : Testing 1st Sentence
Sentence 1 : Testing 1st Sentence
Enter 2nd Sentence : 
Sentence 2 : Testing 1st Sentence
Run Code Online (Sandbox Code Playgroud)

我基本上是在检查%[^\n]。当我输入第一个句子并按“Enter”键时,它会打印“Enter 2nd Sentence :”,然后打印“Sentence 2 :”,然后打印第一个句子。

c

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

嵌套for循环的说明

嗨,有人可以请你解释下面的循环如何打印45.我想我不太明白这些嵌套循环是如何工作的:

#include <stdio.h>

int main() {
    int x, y, n;
    n = 0;
    for (x = 0; x < 10; x++)
        for (y = 0; y < x; y++)
            n++;

    printf("%d\n", n);
}
Run Code Online (Sandbox Code Playgroud)

c for-loop

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

传递给函数时,C类型从char*转换为int

我正在尝试将文件名传递给线程函数,但它的类型在函数内转换为int

struct Data {
    char file_name;
}
void *Requestor(void *args) {
    struct Data *data = (struct Data*)args;
    printf("%s\n", data->file_name); //says expected char* but argument is type of int
}

int file_count = 5;
struct Data files[file_count];

for (int i = 0; i < file_count; i++) {
    printf("%s\n", argv[5 + i]); //this prints the file_name correctly;
    files[i].file_name = argv[5 + i]; // I get: warning: assignment makes integer from pointer without a cast [-Wint-conversion when compiling
    int thread = pthread_create(&(requesterThreads[i]), NULL, …
Run Code Online (Sandbox Code Playgroud)

c

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

对堆栈中值的地址感到困惑

下面是一些工作 - 16 位 x86 程序集


        global    _start

        section   .text
_start: mov ah, 0x0e

        mov bp, 0x8000
        mov sp, bp

        push "A"
        push "B"
        push "C"

        pop bx
        mov al, bl
        int 0x10

        pop bx
        mov al, bl
        int 0x10

        mov al, [0x7ffe]
        int 0x10
        
        jmp $
        
        times 510 -( $ - $$ ) db 0
        dw 0 xaa55
Run Code Online (Sandbox Code Playgroud)

我将 3 个值压入堆栈,然后打印前 2 个值。我试图通过间接寻址打印第三个值,因此 mov al, [0x7ffe] - 但问题是,因为堆栈的基数为 0x8000 并且只能弹出 16 位值,要打印的最后一个字符的地址不是,-“a”是 0x8000 - 0x1 = 0x7FFF 而不是 0x8000 …

x86 assembly x86-16

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