c++ - 在 c++ 中创建带循环的间隔并确定没有数组的最小值和最大值

标签 c++ algorithm loops max min

<分区>

我的家庭作业是:

编写一个程序,提示用户输入两个正整数:区间的顶部和底部,并创建两个函数来显示该区间中可被 2 整除和不可被 4 整除的最大和最小数 Display调用这两个函数的结果。

样本运行:

Enter the bottom of the interval: 100
Enter the top of the interval: 2200
Output: Minimum: 102
Output: Maximum: 2198

-- 我到达这里:

#include <iostream>

using namespace std;
int main()
{
    int bottom,top;
    int x;
    int pl = 0;

    cout << "Enter the bottom of the interval:";
    cin >> bottom;
    cout << "Enter the top of the interval:";
    cin >> top;

    for( x = bottom+1; x < top; x ++)
    {
        if (x % 2 == 0 & x % 4 != 0)


    }


    return 0;
}

并且不太清楚如何打印最大值和最小值。你能给我一个提示吗?

最佳答案

我明白了。谢谢你帮助我!

#include <iostream>

using namespace std;
int main()
{
int bottom,top;
int x;



cout << "Enter the bottom of the interval:";
cin >> bottom;
cout << "Enter the top of the interval:";
cin >> top;


int mini = bottom + (bottom%2);

if(mini%4 == 0)
    mini = mini + 2;

int high = top - (2 - top%2);

if(high%4 == 0)
    high = high - 2;

    cout << "Maximum:" << high << endl;
    cout << "Minimum:" << mini << endl;

return 0;
}

关于c++ - 在 c++ 中创建带循环的间隔并确定没有数组的最小值和最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39831784/

相关文章:

c++ - 惰性传播

java - 递归函数获取列表动态数量的组合

javascript - 不使用 parseInt 的二进制到十进制

c++ - 具有替代返回类型的模板特化

c++ - 我需要做什么才能使 Boost.Beast HTTP 解析器找到正文的结尾?

algorithm - 最有效的座位安排

loops - 如何获得自动播放和循环播放的youtube视频。

c++ - 从函数返回 vector 以打印内容

c++ - C++ std::container( vector )如何存储其内部结构(元素地址、按索引访问)?

Google Chrome 用于字符串搜索的算法?