C++ 在c+中创建带循环的间隔并确定不带数组的最小值和最大值+;
我的作业是: 编写一个程序,提示用户输入两个正整数:区间的顶部和底部,并创建两个函数以显示该区间中可被2整除和不可被4整除的最大和最小数字显示调用这两个函数的结果 样本运行:C++ 在c+中创建带循环的间隔并确定不带数组的最小值和最大值+;,c++,algorithm,loops,max,min,C++,Algorithm,Loops,Max,Min,我的作业是: 编写一个程序,提示用户输入两个正整数:区间的顶部和底部,并创建两个函数以显示该区间中可被2整除和不可被4整除的最大和最小数字显示调用这两个函数的结果 样本运行: Enter the bottom of the interval: 100 Enter the top of the interval: 2200 Output: Minimum: 102 Output: Maximum: 2198 -- 我来到这里: #include <iostream> using n
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;
}
#包括
使用名称空间std;
int main()
{
int底部,顶部;
int x;
int-pl=0;
cout>底部;
cout>top;
对于(x=底部+1;x<顶部;x++)
{
如果(x%2==0&x%4!=0)
}
返回0;
}
也不知道如何打印最大值和最小值。你能给我一个提示吗?这个代码片段达到了要求:
min = bottom + (bottom%2)
if (min%4 == 0)
min = min + 2
max = top - (2 - top%2)
if (max%4 == 0)
max = max - 2
通过首先计算可被2
整除的最小数=
底部
,我们找到min
。如果bottom
可被2
整除,则min
=bottom
,否则我们将余数
,即bottom%2
添加到min
接下来,检查min
是否可被4
整除。如果不是,那么我们不需要更改min
,否则我们将2
添加到min
,因为在这种情况下,min
将不会被4
整除,而它仍然可以被2
整除。这min
将是满足问题约束条件的最小可能数
同样适用于查找
max
此代码片段满足以下要求:
min = bottom + (bottom%2)
if (min%4 == 0)
min = min + 2
max = top - (2 - top%2)
if (max%4 == 0)
max = max - 2
通过首先计算可被2
整除的最小数=
底部
,我们找到min
。如果bottom
可被2
整除,则min
=bottom
,否则我们将余数
,即bottom%2
添加到min
接下来,检查min
是否可被4
整除。如果不是,那么我们不需要更改min
,否则我们将2
添加到min
,因为在这种情况下,min
将不会被4
整除,而它仍然可以被2
整除。这min
将是满足问题约束条件的最小可能数
同样适用于查找
max
我得到了它。谢谢你帮助我
#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;
}
#包括
使用名称空间std;
int main()
{
int底部,顶部;
int x;
cout>底部;
cout>top;
int mini=底部+(底部%2);
如果(最小值%4==0)
mini=mini+2;
int high=顶部-(2-顶部%2);
如果(高%4==0)
高=高-2;
我明白了。谢谢你帮助我
#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;
}
#包括
使用名称空间std;
int main()
{
int底部,顶部;
int x;
cout>底部;
cout>top;
int mini=底部+(底部%2);
如果(最小值%4==0)
mini=mini+2;
int high=顶部-(2-顶部%2);
如果(高%4==0)
高=高-2;
无法编写两个函数。第一个函数从范围的底部找到最小值,返回该值,然后打印。第二个函数从范围的顶部开始,向下运行,直到找到最大值,返回该值,然后打印该值。这里有一个提示:您显示的循环从范围中的最小值开始,并按如下方式运行向上。因此,它找到的第一个数字将是符合标准的最低数字。祝贺你!你现在解决了一半的问题。现在,仔细想想:最高数字的等效方法是什么?应该是非常明显的。完成了这项工作后,你剩下的唯一任务是检查范围太小,即最小值=15,最大值=16.如果你认为代码>最大=最大(最大% 4)给你下一个最大的数字(从最大值开始),你甚至不需要循环。这可以被4整除。我把它留给你,让你得到完整的表达式;)但是一旦我写了cout@Illusive:我已经对我的答案添加了一些解释。如果你对我查找min
和max
的方法有任何疑问,请检查一下。写两个函数。第一个函数从ran的底部开始查找minge,返回该值,然后打印。第二个函数从范围的顶部开始,向下运行,直到找到最大值,返回该值,然后打印该值。这里有一个提示:您显示的循环从范围中的最小值开始,然后向上运行。因此,它找到的第一个值将是满足cr的最小值iteria。祝贺你!你现在解决了一半的问题。现在,仔细想想:最高数字的等效方法是什么?这应该是非常明显的。完成了这项工作后,你唯一剩下的任务就是检查范围太小的病理例子,即最小值=15,最大值=16。如果你是c,你甚至不需要循环考虑到最大值=最大值-(最大值%4)
为您提供了下一个最大值(从最大值开始)这可以被4整除。我把它留给你,让你得到完整的表达式;)但是一旦我写了cout@Illusive:我已经对我的答案添加了一些解释。如果你对我找到min
和max
”的方法有任何疑问,请检查一下。@TobySpeight:添加了一些解释。@TobySpeight:添加了一些解释。