【问题标题】:Is there any simple way to add a prefix to each element of an array in C?有什么简单的方法可以为 C 中数组的每个元素添加前缀吗?
【发布时间】:2014-05-06 09:00:12
【问题描述】:

我正在解决格雷码问题。 我使用递归调用制定了我的逻辑。 一切看起来都不错,但我不知道如何为数组的每个元素添加前缀“0”或“1”。 例如)前缀 0 : { 00, 01, 11, 10 } -> { 000, 001, 011, 010 } 或 前缀 1 : { 00, 01, 11, 10 } -> { 100, 101, 111, 110 }

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

int grayCode(int list[], int bit)
{
    int listLen = strlen(list); // calculate received array length
    int revList[] = { 0 };
    int newList[] = { 0 };

    if (listLen < pow(2, bit)) // If the received array length is below 2^bit, proceed below.

    {
        for (int i = 0; i < listLen; i++) // create a reverse order array
        {
            revList[i] = list[listLen - i];
        }

        for (int i = 0; i < listLen; i++) // prefix 0 to each element of an received array, I know this doesn't work. How should I do it??
        {
            list[i] = "0" + list[i];
        }

        for (int i = 0; i < listLen; i++) // prefix 1 to each element of an reverse array, I know this doesn't work. How should I do it??
        {
            revList[i] = "1" + revList[i];
        }

        for (int i = 0; i < listLen; i++)  // put all elements of received array in the new array
        {
            newList[i] = list[i];
        }

        for (int i = 0; i < listLen; i++)  // put all elements of reverse array in the new array
        {
            newList[listLen + i] = revList[i];
        }

        for (int i = 0; i < listLen * 2; i++)  // print all elements in a new array
        {
            printf("%d bit Gray Code is { ", bit);
            printf("%d, ", newList[i]); 
            printf("}\n");
        }

        grayCode(newList, bit); // Recursive call
    }

    else if (listLen == pow(2, bit)) // If the received array length is same as 2^bit, return.
    {
        return 0;
    }
}

int main(void)
{
    int initList[2] = { 0, 1 };
    int bit;

    printf("Which bit of gray-code do you want? : ");
    scanf_s("%d", &bit);

    while (bit < 1)
    {
        printf("Input an integer bigger than 1 : ");
        scanf_s("%d", &bit);
    }

    if (bit == 1)
    {
        printf("1 bit Gray Code is { 0, 1 }\n");
    }

    else if (bit > 1)
    {
        grayCode(initList, bit);
    }

    return 0;
}

【问题讨论】:

  • 您是否尝试在非字符串(list 是指向int 的指针)上使用字符串函数(strlen)?这通常不是一个好主意,因为strlen 将在第一个零 byte 处停止(int 通常是四个字节)。
  • 您似乎没有为您的列表分配空间
  • 你的代码中还有不少undefined behavior的其他问题和案例。
  • 请通过编辑您的帖子来修正缩进。
  • 这些是字符串还是整数?在整数前面加 0 是没有意义的。如果您想以 0 开头显示整数,可以使用 printf 格式来完成。

标签: c prefix gray-code


【解决方案1】:

不,您不能将 0 作为前缀附加到整数。
0 开头的整数被假定为相应十进制整数的八进制表示,

(037)8 == (31)10

如果你真的想保存前缀为0s的数字,你必须将数字存储为字符串。

您可以使用临时字符串来存储中间体。

算法将是:

char str[10] = "01";      // To be converted to 001
char temp[10];

strcpy(temp, "0");     // temp = "0", str = "01"
strcat(temp, str);     // temp = "001", str = "01"
strcpy(str, temp);     // temp = "001", str = "001"

【讨论】:

  • 哇,我想不出使用 'char' 类型!非常感谢!!
猜你喜欢
  • 2020-03-03
  • 2017-06-17
  • 2018-08-12
  • 2020-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-06
  • 2021-10-05
相关资源
最近更新 更多