微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有没有办法让两个不同大小的数组成对?

如何解决有没有办法让两个不同大小的数组成对?

所以我有 2 个不同大小的数组,我想将它们中的元素配对成一个新的数组。

示例:

a = {1 2 3 4 5 6 7 8 9}

b = {a b c d}

c = {1a 1b 1c 1d 2a 2b 2c 2d...9a 9b 9c 9d}(我想获得的数组)

到目前为止我已经得到了这个,它打印如下:1a 1ab 1abc...

我已经设法用 printf("%s%s",&a[i],&b[j]) 以我想要的方式打印它们,但我仍然无法理解如何将这些对保存在单独的数组中

    const char *a[] = {'1','2','3','4','5','6','7','8','9'};
    const char *b[] = {'a','b','c','d'};
    char *f;

    for(int i = 0; i < 9; i++){
        for(int j = 0; j < 4; j ++){
            f = (strcat(&a[i],&b[j]));
            printf("%s ",f);
        }
    }

我正在尝试学习 C,但在这部分遇到了障碍。感谢您的理解,如果我的英语不好,请见谅

解决方法

我不完全确定您需要什么...

如果你想要这样的输出: 1a 1b 1c 1d 2 2b 2c 2d 3 3b 3c 3d 4 4b 4c 4d 5 5b 5c 5d 6 6b 6c 6d 7 7b 7c 7d 8 8b 8c 8d 9 9b 9c 9d

最简单的解决方案是将 f 视为一个包含两个字符的数组。 因为 strcat (char *strcat(char *dest,const char *src);) 在第一个参数字符串之后连接第二个参数字符串,您不能简单地使用第一个字符作为目标,但您需要第三个变量,在您的情况下为 f

打印后,您必须重新初始化(使用 strcpy '因为它是一个字符串) 以便为下一对有空间。

    const char *a[] = {'1','2','3','4','5','6','7','8','9'};
    const char *b[] = {'a','b','c','d'};
    char f[2] = "";

    int i,j;

    for(i = 0; i < 9; i++)
    {
        for(j = 0; j < 4; j++)
        {
            strcat(f,&a[i]);
            strcat(f,&b[j]);
            printf("%s ",f);
            strcpy(f,"");
        }
    }

我希望这是你需要的!

EDIT -> 要保存您需要大小为 (Asize x Bsize) 的第四个数组的值

,

这很好,但仍然有一个错误。我会立即修复它:

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

#define NODE_LEN 2 + 1

void solution() {
    
    const char a[] = {'1','9'};
    const char b[] = {'a','d'};
    char *result_array[8] = { 0 };
    char f[NODE_LEN] = { 0 };
    int i,index_j;

    for (i = 0,index_j = 0; i < 9; i++) {
        
        sprintf(f,"%c%c",a[i],b[index_j]);
        printf("%s ",f);
        
        char* new_value = calloc(NODE_LEN,sizeof(char));
        strcpy(new_value,f);
        result_array[i] = new_value;
        
        if (++index_j >= 4) index_j = 0;
    }
    
    for (i = 0; i < 9; i++) {
        printf("-%s\n",result_array[i]);
    }
    
    for (i = 0; i < 9; i++) {
        free(result_array[i]);
    }
}

int main(void) {
    solution();
    
    return 0;
}
,

字符数组

如果数组ab只包含字符,你甚至不需要函数strcat。我提供了示例代码,其中数组 f 包含九个字符串,每个字符串有两个字符加上 null character '\0'

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

int main (void)
{
    /* arrays `a` and `b` only contain characters */
    char a[] = {'1','9'};
    char b[] = {'a','d'};

    /* array `f` contains 9 * 4 strings with 3 characters each */
    char f[9 * 4][3];

    for (int i = 0; i < 9; ++i) {
        for (int j = 0; j < 4; ++j) {
            int k = i * 4 + j;
            f[k][0] = a[i];
            f[k][1] = b[j];
            /* null character for string termination */
            f[k][2] = '\0';
        }
    }

    /* print content of the array `f` */
    for (int k = 0; k < 9 * 4; ++k)
            printf("%s ",f[k]);
    printf("\n");

    return EXIT_SUCCESS;
}

编译后,第一个程序在执行时输出如下:

1a 1b 1c 1d 2a 2b 2c 2d 3a 3b 3c 3d 4a 4b 4c 4d 5a 5b 5c 5d 6a 6b 6c 6d 7a 7b 7c 7d 8a 8b 8c 8d 9a 9b 9c 9d

字符串数组

如果数组 ab 应该包含一个以上字符的字符串,则需要使用 strcpy 中的 strcat<string.h>。为 f 中的字符串选择合适的最大大小。

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

int main (void)
{
    /* arrays `a` and `b` contain strings with multiple character */
    char *a[] = {"one","two","three","four","five","six","seven","eight","nine"};
    char *b[] = {"_a","_b","_c","_d"};

    /* array `f` contains 9 * 4 strings with a maximum of 15 characters*/
    char f[9 * 4][15];

    for (int i = 0; i < 9; ++i) {
        for (int j = 0; j < 4; ++j) {
            int k = i * 4 + j;
            strcpy(f[k],a[i]);
            /* `strcat` automatically null-terminates */
            strcat(f[k],b[j]);
        }
    }

    /* print content of the array `f` */
    for (int k = 0; k < 9 * 4; ++k)
            printf("%s ",f[k]);
    printf("\n");

    return EXIT_SUCCESS;
}

编译后,第二个程序执行时输出如下:

one_a one_b one_c one_d two_a two_b two_c two_d three_a three_b three_c three_d four_a four_b four_c four_d five_a five_b five_c five_d six_a six_b six_c six_d seven_a seven_b seven_c seven_d eight_a eight_b eight_c eight_d nine_a nine_b nine_c nine_d
,

这通常称为置换函数。我们需要空间来存储我们的结果,这将是数组 A 的长度乘以数组 B 的长度。

我们的单个结果将是 char [3] 类型,一个空格表示数字,一个空格表示字母,一个空格表示正确创建 字符串所需的 NUL 终止字节 ('\0') 在 C 中。

const char a[] = {'1','9'};
const char b[] = {'a','d'};

char zipped[9*4][3];

for (int i = 0,k = 0; i < 9; i++) 
    for (int j = 0; j < 4; j++,k++) {
        zipped[k][0] = a[i];
        zipped[k][1] = b[j];
        zipped[k][2] = '\0';
    }

for (int i = 0; i < 9 * 4; i++)
    printf("%s ",zipped[i]);

// 1a 1b 1c 1d 2a 2b 2c 2d 3a 3b 3c 3d 4a 4b 4c 4d 5a 5b 5c 5d 6a 6b 6c 6d 7a 7b 7c 7d 8a 8b 8c 8d 9a 9b 9c 9d
,

要将所有内容存储在单独的数组中,您需要通过 malloc(或类似方法)分配内存或指定固定大小的数组。

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

int main(int argc,char** argv) {
    const char a[] = {'1','d'};
    char **f;
    
    // Getting the size of each array
    const int a_size = sizeof(a) / sizeof(a[0]);
    const int b_size = sizeof(b) / sizeof(b[0]);

    // Allocating the memory needed for the array of strings
    f = (char**) malloc(a_size * b_size * sizeof(char*));

    for(int i = 0; i < a_size; i++) {
        for(int j = 0; j < b_size; j++) {

            // Allocating memory for each combination pair. 1a,1b...
            f[i * a_size + j] = (char*) malloc(3 * sizeof(char)); // Size of 3 is needed to include the null terminator '\0'
            sprintf(f[i * a_size + j],b[j]);
            printf("%s ",f[i * a_size + j]);
        }
    }

    // Make sure to free all the allocated memory.
    for (int i = 0; i < a_size * b_size; i++) {
        free(f[i]);
    }
    free(f);
    return 0;
}

使用 gcc 编译将输出

1a 1b 1c 1d 2a 2b 2c 2d 3a 3b 3c 3d 4a 4b 4c 4d 5a 5b 5c 5d 6a 6b 6c 6d 7a 7b 7c 7d 8a 8b 9a 9d 8c 9d 8c

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。