【问题标题】:find the smallest of a set of integers c++找到一组整数中的最小值c ++
【发布时间】:2022-08-10 01:19:59
【问题描述】:
#include <stdio.h>
int main () {
    int n, smNum = 1;
    printf(\"Enter a number: \");
    scanf(\"%d\", &n);
    while (n != 0 || smNum != 0){
        printf(\"Enter a number: \");
        scanf(\"%d\", &smNum);
            if (smNum< n)   smNum = n;
    }
    printf(\"The smallest number is: %d\", n);
}

这个程序必须确定最小的提示数字并打印出来。当用户输入数字“0”时程序必须停止。那是发生错误的时候。该程序一直要求进入。

  • if 条件何时为真?
  • 似乎是简单调试会话的理想人选。您是否尝试调试代码?
  • 现在是学习使用调试器的好时机。当您拥有可以让您准确查看程序在做什么的工具时,为什么还要依赖其他人为您调试?
  • 这段代码看起来像 C,而不是 C++。您实际使用的是哪种语言?
  • 提示:min 风格的函数可以很容易地以int smallest = INT_MAX; 开头来编写

标签: c++ while-loop


【解决方案1】:

在我看来,这个问题有点混乱。我将尝试了解您想要做什么,也许会解决您的一些疑问。

首先,在 C++ 中使用 stdio.h 函数看起来有点奇怪。即使它可以使用并且肯定会工作,我还是建议使用 C,或者使用 C++ 函数进行 IO。

我猜你想实现类似:“创建一个要求用户输入一些整数的 C/C++ 程序,当用户输入 0 时,程序停止并打印用户输入的最小整数(不包括 0) "

我将维护你程序的一般结构,这样你可能会发现它更容易阅读:

#include <stdio.h>
int main() {
    int n;    // It will hold the user's input
    int min;  // It will hold the smallest number entered so far
    printf("Enter a number: ");
    scanf("%d", &n);
    min = n;          // This is the first number, so we save in the min variable so that we can compare it to the other values later
    while (n != 0) {  // As we said, we stop when n is equal to 0
        printf("Enter a number: ");
        scanf("%d", &n);
        if (n < min && n != 0) min = n;  // If n is smaller than min, it has to become the new min.
        // But this is not true when it is equal to 0, we just have to go out of the loop in that case
    }
    printf("The smallest number is: %d", min);
}

【讨论】:

  • 非常非常感谢你!
  • 别客气!如果我解决了您的疑问,请考虑将其标记为已接受。祝你有美好的一天。
  • stdio.h 保留在 C++ 标准中,但已弃用。应该改用cstdio。
【解决方案2】:

首先,代码看起来更像 C 而不是 C++。您应该在 C++ 中使用 &lt;iostream&gt; 类,而不是 &lt;stdio.h&gt; 函数。无论哪种方式,您都应该在对用户进行操作之前验证用户是否实际输入了有效数字。

话虽如此...

int n, smNum = 1;
printf("Enter a number: ");
scanf("%d", &n);
while (n != 0 || smNum != 0){

无论用户输入什么作为他们的第一个输入,循环将始终至少输入 1 次,因为表达式 smNum != 0 在第一次迭代中将始终为真,因为 smNum 被初始化为 1 而不是 @ 987654327@。

printf("Enter a number: ");
scanf("%d", &smNum);
if (smNum< n)   smNum = n;

在循环内部,无论用户在后续输入中输入什么,n 将始终是第一个输入的值,因此表达式 smNum &lt; n 将仅适用于低于第一个输入的新输入,但在这种情况smNum 将被第一个输入的值覆盖。

如果用户输入0 作为第一个输入,则循环将继续,直到用户再次输入0。 n 将始终为 0,smNum 将始终为 0 或正数,因为负输入被丢弃。

如果用户没有为第一个输入输入0,则循环将永远继续,因为n 永远不会是0,所以表达式n != 0 将始终为真。

printf("The smallest number is: %d", n);

你应该打印出smNum,而不是n。

话虽如此,请尝试更多类似的东西:

#include <stdio.h>

int askForNumber(const char *msg)
{
    int n, ret;
    printf("%s: ", msg);
    do {
        ret = scanf("%d", &n);
        if (ret == 1) break;
        if (ret == 0){
            while ((ret = getchar()) != EOF && ret != '\n');
        }
        if (ret == EOF) exit(0);
        printf("Invalid input, try again: ");
    }
    while (true);
    return n;
}

int main () {
    int n = askForNumber("Enter a number");
    if (n != 0){
        int smNum = n;
        while ((n = askForNumber("Enter another number")) != 0) {
            if (n < smNum) smNum = n;
        }
        printf("The smallest number is: %d", smNum);
    }
    return 0;
}

或者,使用&lt;iostream&gt;:

#include <iostream>
#include <limits>
#include <cstdlib>
using namespace std;

int askForNumber(const char *msg)
{
    int n;
    cout << msg << ": ";
    while (!(cin >> n)) {
        if (cin.eof()) exit(0);
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid input, try again: ";
    }
    return n;
}

int main () {
    int n = askForNumber("Enter a number");
    if (n != 0){
        int smNum = n;
        while ((n = askForNumber("Enter another number")) != 0) {
            if (n < smNum) smNum = n;
        }
        cout << "The smallest number is: " << smNum;
    }
    return 0;
}

【讨论】:

  • 太感谢了!
【解决方案3】:
#include <stdio.h>

int main() {
    int a,b;
    scanf("%d %d",&a,&b);
    a>b? prienter code herentf("%d is smaller",b):printf("%d is smaller",a);
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2014-11-04
    相关资源
    最近更新 更多