用C写一个程序,输出大于或等于n且所有数字都是偶数的最小自然数

Write a program in C that outputs the smallest natural number greater or equal to n whose all digits are even

用C写一个程序,取一个自然数n,输出大于或等于n且所有数字都是偶数的最小自然数。例如,如果n = 16,则应输出20,如果n = 41,则应输出42。 我尝试过的:

#include <stdio.h>

int main(void) {
  int n, i, condition = 0;

  scanf("%d", &n);
  for (i = n; i >= n; i++) {
    if (i % 2 == 0) {
      i = i / 10;
      while (i > 0) {
        if (i % 2 == 0) {
          i = i / 10;
          condition = 1;
        } else {
          break;
        }
      }
    } else {
      condition = 0;
    }
    if (condition == 1) {
      printf("%d", i);
      break;
    }
  }
  return 0;
}

为什么这行不通?

您在将 i 设为 0 后打印它,即将它除以 10 直到第一个数字出现。因此,它会在每种情况下打印 0。

相反,您应该尝试将 i 的值分配给另一个变量,这样您就不必更改 i。此外,在第一次迭代变为 0 后,由于 for 循环中的要求 i>=n,它不会进行其他迭代。

#include <stdio.h>

int main(void) {
int n;
scanf("%d", &n);
for (int i = n; i >= n; i++){
    int num = i;
    int cond = 0;
    while(num>0){
        int dig = num%10;
        if(dig%2 != 0){
        cond = 1;
        break;
        }
    num/=10;
    }
    if (cond ==0){
        printf("%d",i);
        break;
    }
 }
 return 0;
}

添加到@Sanjay-sopho 答案中,您可以尝试两种方法:

  • 迭代,蛮力。
  • 递归。

这是迭代版本的样子:

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

int check_even(int n);

int
main(void) {
    int i, n;

    printf("Enter n: ");
    if (scanf("%d", &n) != 1 || n < 1) {
        printf("Invalid entry, must be a natural number!\n");
        exit(EXIT_FAILURE);
    }

    for (i = n; i >= n; i++) {
        if (check_even(i)) {
            printf("%d\n", i);
            break;
        }
    }

    return 0;
}

int
check_even(int n) {
    int i;

    for (i = n; i > 0; i /= 10) {
        if (i % 2 != 0) {
            return 0;
        }
    }
    return 1;
}

或者你可以试试这个递归函数:

int
check_even_rec(int n) {

    if (n == 0) {
        return 1;
    } else if (n % 2 != 0) {
        return 0;
    }

    return check_even_rec(n /= 10); 
}

到目前为止发布的解决方案搜索所有大于或等于 n 的数字,直到找到一个全偶数的数字。这是非常低效的。

更好的方法是从右到左扫描一次数字,然后调整它们直接得到想要的结果:

// assume n >= 0                                                                
int next (int n)
{
    int r = 0; // result                                                        
    int c = 1; // coefficient (power of ten) of the current digit               

    // Scan digits from right to left.                                          
    while (n) {
        // current digit                                                        
        int d = n % 10;

        if (d % 2 == 0) {
            r += c * d;
        }
        else {
            // Make sure we handle the case d == 9 correctly.                   
            n++;
            d = n % 10;

            r += c * d;

            // Since we are incrementing the current digit (or a more           
            // significant one), we must set every less significant             
            // digits to zero.                                                   
            r -= r % c;
        }

        c *= 10;
        n /= 10;
    }

    return r;
}