【问题标题】:Prints out the number of multiples of 11 that are greater than or equal to N1 and less than or equal to N2打印出大于或等于 N1 且小于或等于 N2 的 11 的倍数
【发布时间】:2020-10-28 00:02:22
【问题描述】:

编写一个执行以下操作的程序:

  • 要求用户输入一个整数 N1。
  • 要求用户输入一个整数 N2。
  • 打印出大于或等于 N1 且小于或等于 N2 的 11 的倍数。

我已经这样做了,但是程序要求打印出可以与 11 相乘但必须大于 N1 且小于 N2 的数字,这就是我遇到问题的地方

#include <stdio.h>

int main () {
    int N1;
    printf("Enter an integer N1: ");
    scanf("%d", &N1);

    int N2;
    printf("Enter an integer N2: ");
    scanf("%d", &N2);

    int i = N1;
    while (i <= N2)
    {
        if (i % 11 == 0)
        {
            printf("%d\n", i);
        }
        i = i + 1;
    }
    printf("Exiting...");
    return 0;
}

【问题讨论】:

  • 这里真的不需要循环。想想你会如何用笔和纸来表达。
  • 我太累了,无法写出正确的答案,但基本上:在顶部声明一个变量j,将其初始化为零,每次递增i % 11 == 0(在您现有的@987654324 @block),并在最后打印出来。

标签: c


【解决方案1】:

您可以使用额外的计数器变量来跟踪这一点。一些额外的改进:

  1. 当使用scanf 时,最好总是在输入格式前加上一个空格,这样缓冲区中的任何前导空格都会被消耗,但不会格式化到输出变量中,并且还要检查它的返回值是否成功(它返回一个int 表示从字符串中成功读取的格式说明符的数量)。

  2. 您不需要一次迭代一个。您可以将 N1 递增到 11 的下一个倍数,然后继续向其添加 11,直到达到 N2 的值。

     #include <stdio.h>
    
     int main() 
     {
         int N1;
         printf("Enter an integer N1: ");
         if (scanf(" %d", &N1) != 1)
         {
             // Handle scanf failure
         }
    
         int N2;
         printf("Enter an integer N2: ");
         if (scanf(" %d", &N2) != 1)
         {
             // Handle scanf failure
         }
    
         while (N1 % 11 != 0)
         {
             N1++;
         }
    
         int count = 0;
    
         while (N1 < N2)
         {
             printf("%d\n", N1);
    
             N1 += 11;
             count++;
         }
    
         printf("Saw %d multiples of 11 in this range.\n", count);
    
         printf("Exiting...");
         return 0;
     }
    

【讨论】:

  • 是的,我完全同意你在那里所做的,但问题是确定可以与小于 N2 且大于 N1 的 11 相乘的数字有多少!
  • @Watusshi 我的错误;重新阅读您的帖子并更新我的答案。 :)
  • 真的很感激,老板!我正在学习成为一名软件工程师,这是我第一次学习 C,所以即使是一个简单的问题对我来说也是一个难题。还是谢谢!
【解决方案2】:

对于这个答案,我首先复制了您的代码,添加了来自 Govind Parmar 答案的 scanf 改进,并添加了一个计数器变量。

现在我想以 Eugene Sh. 的评论为基础,完全摆脱循环。这意味着我的答案不会打印出每个 11 的倍数,但无论如何您都不需要这样做。

首先,我们需要检查N2 &gt;= N1 是否为真。当我们使用循环时,如果这个条件为假,则循环不会执行,但是当我们摆脱循环时,我们需要手动检查这个条件。

对于问题的主要部分,可以这样想:你有一个数字范围,从N1 开始,到N2 结束。您想确定该范围内有多少个 11 的倍数。为此,请在该范围内找到 11 的最小倍数,在该范围内找到 11 的最大倍数,并找出它们之间有多少个 11。

要找到大于或等于N1 的 11 的最小倍数,首先找到小于或等于 11 (N1 / 11) 的最大倍数,然后在必要时添加 11。

要找到小于或等于N2 的 11 的最大倍数,只需执行 N2 / 11

将这些倍数分别称为smallest_multiplelargest_multiple。现在,要查找总共有多少个倍数,请执行 (largest_multiple - smallest_multiple) / 11 + 1

把它们放在一起:

#include <stdio.h>

int main () {
    int N1;
    printf("Enter an integer N1: ");
    if (scanf(" %d", &N1) != 1) {
        // Handle scanf failure
    }

    int N2;
    printf("Enter an integer N2: ");
    if (scanf(" %d", &N2) != 1) {
        // Handle scanf failure
    }

    int count;

    if (N2 >= N1) {
        int smallest_multiple;
        int largest_multiple;

        smallest_multiple = N1 / 11;
        if (smallest_multiple < N1) {
            smallest_multiple += 11;
        }

        largest_multiple = N2 / 11;

        if (largest_multiple >= smallest_multiple) {
            count = (largest_multiple - smallest_multiple) / 11 + 1;
        } else {
            count = 0;
        }
    } else {
        count = 0;
    }

    printf("Saw %d multiples of 11 in this range.\n", count);

    printf("Exiting...");
    return 0;
}

这个答案可能存在一个问题:如果N1 &lt; 0 可能无法正常工作。不幸的是,大多数 C 算术运算符指南在除法方面似乎都忽略了负数。到目前为止我发现的最好的是https://en.cppreference.com/w/cpp/language/operator_arithmetic

商在实现定义的方向上四舍五入。 (直到 C++11)

商被截断为零(小数部分被丢弃)。 (C++11 起)

当我知道更多时,我会更新这个答案。


编辑:我发现我原来的答案有问题。如果范围内没有 11 的倍数,则 smallest_multiple 将大于 N2(范围外),largest_multiple 将小于 N1(范围外)和 largest_multiple - smallest_multiple == -11。我已经编辑了我的答案以对此进行测试。

【讨论】:

    猜你喜欢
    • 2011-01-25
    • 2013-09-02
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多