C程序将数字反转

发布于 2024-08-23 14:56:30 字数 176 浏览 4 评论 0原文

我正在寻找用于反转数字的 C 程序,如下所示:

如果我输入:

123456

那么结果将是:

654321

请帮助我。

I am looking for the C program for reverse the digits like below:

If i enter:

123456

Then the result would be:

654321

Please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

雨后彩虹 2024-08-30 14:56:30

这是这个复杂问题的简单解决方案:

#include <stdio.h>

int main()
{
    int ch;
    ch = getchar();
    if (ch != '\n') {
        main();
        printf("%c", ch);
    }
}


A new version that outputs the newline:

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

static void newline(void)
{
    printf("\n");
}

int main()
{
    int ch;
    ch = getchar();
    if (ch != '\n') {
        main();
        printf("%c", ch);
    } else {
        atexit(newline);
    }
}

Here is a simple solution to this complex problem:

#include <stdio.h>

int main()
{
    int ch;
    ch = getchar();
    if (ch != '\n') {
        main();
        printf("%c", ch);
    }
}


A new version that outputs the newline:

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

static void newline(void)
{
    printf("\n");
}

int main()
{
    int ch;
    ch = getchar();
    if (ch != '\n') {
        main();
        printf("%c", ch);
    } else {
        atexit(newline);
    }
}
梦醒时光 2024-08-30 14:56:30

如果您的教授正在对您的表现进行评分,请尝试 ggf31416 在另一个问题中对此问题的解决方案

int FastReverse(int num) {
    int res = 0;
    int q = (int)((214748365L * num) >> 31);
    int rm = num - 10 * q;
    num = q;
    if (rm == 0) return -1;
    res = res * 10 + rm;
    while (num > 0) {
       q = (int)((214748365L * num) >> 31);
       rm = num - 10 * q;
       num = q;
       res = res * 10 + rm;
    }
    return res;
}

优化®

绝对必须在纳秒内完成。

If your professor is grading you on performance, then try ggf31416's solution to this problem in another question:

int FastReverse(int num) {
    int res = 0;
    int q = (int)((214748365L * num) >> 31);
    int rm = num - 10 * q;
    num = q;
    if (rm == 0) return -1;
    res = res * 10 + rm;
    while (num > 0) {
       q = (int)((214748365L * num) >> 31);
       rm = num - 10 * q;
       num = q;
       res = res * 10 + rm;
    }
    return res;
}

Optimization®

When it absolutely, positively has to be done this nanosecond.

天暗了我发光 2024-08-30 14:56:30

使用%10获取最后一位数字。输出它。将数字除以 10 即可得到除最后一位数字之外的所有数字。使用 % 10 获取最后一位数字。以此类推,直到数字变为0。

Use % 10 to get the last digit. Output it. Divide the number by 10 to get all but the last digit. Use % 10 to get the last digit of that. And so on, until the number becomes 0.

肤浅与狂妄 2024-08-30 14:56:30

这是另一种可能性。它是非递归的,并且代码可能会少一些。代码注释中有一个例子来解释逻辑。

/*
    Example: input = 12345

    The following table shows the value of input and x
    at the end of iteration i (not in code) of while-loop.
    ----------------------
    i   |   input  |    x
    ----------------------
    0       12345       0
    1        1234       5
    2         123      54
    3          12     543
    4           1    5432
    5           0   54321
    ----------------------
*/
uint32_t
reverseIntegerDigits( uint32_t input )
{
    uint32_t x = 0;

    while( input )
    {
        x = 10 * x + ( input % 10 );
        input = input / 10;
    }

    return x;
}

Here is another possibility. It is non-recursive, and perhaps a little less code. There is an example in the code comments to explain the logic.

/*
    Example: input = 12345

    The following table shows the value of input and x
    at the end of iteration i (not in code) of while-loop.
    ----------------------
    i   |   input  |    x
    ----------------------
    0       12345       0
    1        1234       5
    2         123      54
    3          12     543
    4           1    5432
    5           0   54321
    ----------------------
*/
uint32_t
reverseIntegerDigits( uint32_t input )
{
    uint32_t x = 0;

    while( input )
    {
        x = 10 * x + ( input % 10 );
        input = input / 10;
    }

    return x;
}
划一舟意中人 2024-08-30 14:56:30

使用 scanf("%s", A) 读取字符数组 A 中的数字,或者更好的是使用 fgets 并通过输出从 strlen(A) 开始的每个字符来反转 char 数组 - 10

Read the number in a char array A with scanf("%s", A) or, better, with fgets and output the char array reversed by outputting each character starting from strlen(A) - 1 to 0.

南汐寒笙箫 2024-08-30 14:56:30

strrev 函数反转字符串。如果性能不是问题,那么例如您可以先执行 itoa,然后执行 strrev,然后执行 atoi。
但即使没有strrev,任务也很简单。

strrev function reverses the string. If performance is not a problem then for instance you can do itoa, then strrev, then atoi.
But the task is very simple even without strrev.

未蓝澄海的烟 2024-08-30 14:56:30

看起来像一个非常古老的线程。但我在这里引用了这些解决方案,并且在测试了该网站上可用作解决方案的不同程序后,必须制定自己的解决方案。我意识到,如果数字以零结尾,则将数字视为 int 不会产生必要的数字反转。所以我决定将数字视为字符串,然后将数字反转。这样,我从字符串的角度得到了数字的精确反转,而不是在数字开头丢弃零的数学数字。

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

main()
{

    char num[20];
    int x=0,slen;

    printf("Enter the number you want to reverse :");
    scanf("%s",num);
    slen=strlen(num);


    char *pointertoarray = &num[slen];

    for (x=slen;x>=0; x--){ printf("%c",*pointertoarray); pointertoarray--; }
    printf("\n");

}

Looks like a very old thread. But I referred the solutions here and had to cook up my own solution after testing the different programs available as solution on this website. I realized that treating the number as int will not yield necessary reversing of digit if the number ends with zero(s). So I decided to treat the number as a string and then reverse the digits. This way, I get exact reverse of the digit from a string standpoint and not a mathematical one that discards zeros at the beginning of a number.

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

main()
{

    char num[20];
    int x=0,slen;

    printf("Enter the number you want to reverse :");
    scanf("%s",num);
    slen=strlen(num);


    char *pointertoarray = &num[slen];

    for (x=slen;x>=0; x--){ printf("%c",*pointertoarray); pointertoarray--; }
    printf("\n");

}
心碎的声音 2024-08-30 14:56:30
  1. 计算 number 中的位数并将其存储在 count 变量中
  2. 提取变量中的各个数字
  3. 使用幂函数。

    while(计数>=1)
    {
      var = num % 10;
      sum = sum + var * pow(10, 计数 - 1);
      数=数/10;
      数数 - ;
    }
    
  1. count the number of digits in number and store in count variable
  2. extract individual digits in variable
  3. use the power function.

    while(count>=1)
    {
      var = num % 10;
      sum = sum + var * pow(10, count - 1);
      num =num / 10;
      count--;
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文