简体   繁体   English

C - 将 9 转为 0

[英]C - turn 9 to 0

It's my first question here and I'm a beginner, the code is written in C (ANSI C).这是我在这里的第一个问题,我是初学者,代码是用 C (ANSI C) 编写的。

The code should return the digits as (n+1) for each digit in a recursive function.(123 -> 234; 801->912; 239->340)代码应该为递归函数中的每个数字返回数字为 (n+1)。(123 -> 234; 801->912; 239->340)

The problem is when the digit 9 appears and the code adds it 1 the result is 10 and it's required to become 0. Is there a way to handle it without checking specially for the digit 9?问题是当数字 9 出现并且代码将其加 1 时,结果是 10 并且它需要变为 0。有没有办法处理它而无需专门检查数字 9?

Thanks!谢谢!

int swap(int num)
{
    int a,b;
    if (num/10==0)
        return num+1;
    else
    {
        a = num % 10;
        a++;
        b = swap(num / 10);
        return (b * 10) + a;
    }
}

In order to get the next digit without checking you just have to wrap around using MOD operator %.为了在不检查的情况下获得下一个数字,您只需使用 MOD 运算符 % 环绕。 So a = (num % 10 + 1) % 10 or even simpler a = (num + 1) % 10 as pointed out by Michael所以a = (num % 10 + 1) % 10或者更简单的a = (num + 1) % 10正如迈克尔所指出的

It seems you mean the following看来你的意思是以下

#include <stdio.h>

unsigned int increase_digits( unsigned int n )
{
    const unsigned int Base = 10;

    return ( n + 1 ) % Base + ( n / Base == 0  ? 0 : Base * increase_digits( n / Base ) ); 
}

int main(void) 
{
    unsigned int n = 0;

    printf( "%u: %u\n", n, increase_digits( n ) );

    n = 9;

    printf( "%u: %u\n", n, increase_digits( n ) );

    n = 13;

    printf( "%u: %u\n", n, increase_digits( n ) );

    n = 801;

    printf( "%u: %u\n", n, increase_digits( n ) );

    n = 239;

    printf( "%u: %u\n", n, increase_digits( n ) );

    return 0;
}

The program output is程序输出是

0: 1
9: 0
13: 24
801: 912
239: 340

Recursively, the simplest approach would be:递归地,最简单的方法是:

unsigned swap (unsigned n) {
    if (n < 10) return ++n % 10;
    return 10 * swap(n/10) + swap(n%10);
}

What this function says is:这个函数说的是:

  • If n is less than 10, the result is 1 larger than its current value, modulo 10.如果n小于 10,则结果比其当前值大 1,取模 10。
    Thus, 9 would become (10 mod 10), which would be 0.因此,9 将变为 (10 mod 10),即 0。
  • Otherwise, recursively apply the algorithm against the last digit and the rest of the digits.否则,对最后一位数字和其余数字递归应用该算法。
    The trick here is that the rest of the digits is obtained by dividing the original number by 10, and multiplying the received result by 10.这里的技巧是,其余的数字是通过将原始数字除以 10,并将接收到的结果乘以 10 得到的。

A version that prints 00 for an input of 99输入99打印00的版本

int swap(int num) {
     return num/10 ?
        swap(num/10)*10 + ((num % 10 + 1)%10) :
        (num % 10 + 1) % 10;
}

The swap caller function determines the length of the integer parameter, then displays any necessary leading zeroes.交换调用函数确定整数参数的长度,然后显示任何必要的前导零。

void caller(int num) {
     char s[20];             // Assume an int as at most 19 chars...
     sprintf (s, "%d", num); // Could also use log or a loop...

     printf("result: %0*d\n", (int)strlen(s), swap(num));    
}

Use it as将其用作

caller(mynumber);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM