也不是最大/最小代码,我如何找出此代码?
It is nor max/min code how do i figure out this code?
获取一个程序,它得到(三个整数(并显示的值既不是max也不是min?
我试图想出一种方法来获取最大和最小之间的数字。我的输入是x,y,z,它们是(三个整数(如何获得这个既不是最大值也不是最小的数字?
int x,y,z,max,min,between;
cout<<"enter 3 integer values n";
cin>>x>>y>>z,between;
if (x>y&&x>z)
x=max;
else if (x<y&&x<z)
x=min;
else if(y>z&&y>x)
y=max;
else if (y<z&&y<x)
y=min;
else if(!max==between&& !min==between)
cout<<"not max or min is "<<between<<endl;
return 0;
}
我会知道这不是正确的代码,但我知道路径在某个地方.
得到一个介于最大值和最小值之间的数字。 输入(三个整数(
这基本上是三个数字的中位数。
一个简单的解决方案可以是将数组中的元素,对它们进行排序,然后选择中间的元素,如下所示,
int elements[3] = ... // contains x,y,z
std::sort(std::begin(elements), std::end(elements)); // sorts the elements
elements[1] // pick the middle element
我假设所有数字都是不同的,否则没有既不是最小也不是最大的数字。例如,在三个数字中,3、3 和 3 是最小值和最大值。在这种情况下没有解决方案。
使用此代码,您可以比较前 2 个变量以查看它们的最大值,然后只需与另一个变量进行比较
if (x < y)
{
if (z < x)
{
return x;
}
else if(z < y) {
return z;
}
else {
return y;
}
}
else if (y < z)
{
if (z < x) {
return z;
}
else {
return y;
}
}
相关文章:
- 如何找出GDB的SIGTRAP核心转储的根本原因
- 不必要的C++代码最终会出现在我完成的程序中吗?
- 无法找出我的代码中的内存泄漏
- 请找出我的代码中的错误,它在提交得到错误答案的同时仍然适用于我的所有测试用例
- 试图找出为什么我的代码保持无限循环
- 在Hackerearth上获取运行时错误(SIGSEGV),无法找出代码中的问题
- 我无法找出我的代码出了什么问题
- 也不是最大/最小代码,我如何找出此代码?
- (C++)比较两段代码,一段有效,一段无效,无法找出区别
- 无法找出此错误(将结构数组传递给函数):线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x1)
- 找出一个正方形逻辑中可能的网格数量,但代码不接受超过六个输入,它停止并说.exe停止工作
- 此代码给出错误的输出无法找出我在哪里犯了错误
- 在Visual Studio 2012中运行我的OpenCV代码时无法找出'u202A'警告
- 如何找出gcc和g++产生不同代码的原因
- 无法找出C++代码的分段错误
- 如何找出代码的哪些部分正在创建最多的虚拟内存
- 找出c++中需要的部分代码
- CUDA:请帮我找出代码中的错误
- 在代码厨师上获取运行时错误 (SIGSEGV),无法找出代码中的问题
- 无法找出我的代码中出了什么问题