【问题标题】:C++ compare values with unlimited inputC++ 将值与无限输入进行比较
【发布时间】:2016-01-16 08:30:51
【问题描述】:

这是一个任务。我被要求编写一个程序来比较无限数量的输入值。输入以 0 结尾。我必须显示计数的最小数字的数量。以下是我当前的代码:

#include <iostream>
#include <limits>
using namespace std;
int main()
{
    int num,minct=0,temp=0;
    int min = std::numeric_limits<int>::max();
    cout<<"Enter numbers:";
    do{
        cin>>num;
        if (num<=min &&num!=0){
            min=num;
            minct++;
        }

    }
    while(num!=0);

    cout<<"The smallest number is " << min<<" ."<<endl;
    cout<<"The occurrence count of the smallest number is "<<minct<<" ."<<endl;
    return 0;
}

在此代码中,当最小数字不在输入的第一个时,计数的数字总是比实际值大 1。我怎样才能使它准确?

【问题讨论】:

  • 简单地在纸上处理你的算法就会发现问题。
  • ..或一个微不足道的调试工作。
  • 我在问我算法的问题。在那之前我已经工作了两天。 16 LOC 两天。对。
  • 我的意思是在我来这个网站询问我的算法错误之前

标签: c++ numbers int compare min


【解决方案1】:
if(num<=min && num!=0){
    if(num<min) {
        min=num;
        minct=1;
    }
    else if(num==min) {
        minct++;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    相关资源
    最近更新 更多