C++ 编写重新排序函数
我必须编写一个函数,将三个变量从最低到最高重新排序。函数名为void reorder,我很好奇是否有任何方法可以调用函数中的max和min函数来压缩代码,或者是否有一种方法可以不使用if语句来编写代码。我已经开始了,我想知道在继续之前该怎么做。 多谢各位C++ 编写重新排序函数,c++,function,C++,Function,我必须编写一个函数,将三个变量从最低到最高重新排序。函数名为void reorder,我很好奇是否有任何方法可以调用函数中的max和min函数来压缩代码,或者是否有一种方法可以不使用if语句来编写代码。我已经开始了,我想知道在继续之前该怎么做。 多谢各位 #include <cstdlib> #include <cmath> #include <iostream> #include <iomanip> using namespace std;
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
/*
*
*/
int min3int(int a, int b, int c)
// function returns the minimum of 3 integer inputs
//@
//@
{
if(a<b)
{
if(a<c)
{
return a;
}
}
else if(b<a)
{
if(b<c)
{
return b;
}
}
else if (c<a)
{
if(c<b)
{
return c;
}
}
}
int max3int(int a, int b, int c)
// function returns the maximum of 3 integer inputs
//@
//@
{
if(a>b)
{
if(a>c)
{
return a;
}
}
else if(b>a)
{
if(b>c)
{
return b;
}
}
else if (c>a)
{
if(c>b)
{
return c;
}
}
}
void reorder(int min,int med,int max);
// function reorders the 3 reference parameters so that they are in ascending order
//@
//@
int temp;
if(min<med)
{
if(med<max)
{
if(min<max)
{
min=min;
med=med;
max=max;
}
else if(med<min)
{
if (med<max)
{
if (max<)
}
}
}
}
bool isFactor(int num1,int num2);
// function checks if the first int is a factor of the second int.
int main(int argc, char** argv) {
int dividend;
int factor1, factor2, factor3;
cout << "Enter the number to be divided: ";
cin >> dividend;
cout << endl << "Enter the first possible factor: ";
cin >> factor1;
cout << endl << "Enter the second possible factor: ";
cin >> factor2;
cout << endl << "Enter the third possible factor: ";
cin >> factor3;
cout << endl << "Possibilites entered are in the range of " << min3int(factor1,factor2,factor3)
<< " to " << max3int(factor1,factor2,factor3) << "." << endl;
reorder(factor1,factor2,factor3);
cout << endl << "The following numbers were determined to be factors of " << dividend << ": ";
if(isFactor(factor1,dividend))
cout << factor1 << " ";
if(isFactor(factor2, dividend))
cout << factor2 << " ";
if(isFactor(factor3, dividend))
cout << factor3;
cout << endl << endl;
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
/*
*
*/
int min3int(int a、int b、int c)
//函数返回至少3个整数输入
//@
//@
{
如果(ab)
{
返回c;
}
}
}
无效重新排序(最小整数、中间整数、最大整数);
//函数对3个参考参数重新排序,使它们按升序排列
//@
//@
内部温度;
if(min我把这三个值放在一个向量中,并调用排序算法。这给出了一个整洁的函数,没有if语句
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void reorder(int &min, int &med, int &max)
{
vector<int> vec;
vec.push_back(min);
vec.push_back(med);
vec.push_back(max);
sort(vec.begin(), vec.end());
min = vec[0];
med = vec[1];
max = vec[2];
}
int main()
{
int min = 1, med = 1, max = 3;
cout << min << " " << med << " " << max << endl;
reorder(min, med, max);
cout << min << " " << med << " " << max << endl;
system("pause");
return 0;
}
#包括
#包括
#包括
使用名称空间std;
无效重新排序(整数和最小值、整数和中间值、整数和最大值)
{
向量向量机;
向量推回(分钟);
向量推回(med);
向量推回(最大);
排序(vec.begin(),vec.end());
min=vec[0];
med=vec[1];
max=vec[2];
}
int main()
{
最小积分=1,最小积分=1,最大积分=3;
cout我看不到使用min3int()
或max3int()
进行排序的简单方法,因为它们不会告诉您其他变量的顺序
我建议写一些简单易懂的东西,例如:
void reorder(int &a, int &b, int &c)
{
if (b < a) std::swap(b, a);
if (c < a) std::swap(c, a);
if (c < b) std::swap(c, b);
}
void重新排序(内部和a、内部和b、内部和c)
{
if(b
如果
,则可以在没有的情况下比较值,但我更喜欢清晰的代码,除非测量表明需要进行英勇的优化。也可以仅用两次交换对三个变量进行排序().您希望如何从void
函数中返回min、med、max;
?我意识到,在发布后,我不想将这些im新信息返回给cpp,因此这是一个用户错误:如果(和循环),您仍然有,它们只是隐藏在std::sort
中。这也会影响内存分配。