写一个重新排序函数
Writing a reorder function
我必须编写一个函数,将三个变量从最低到最高重新排序。函数名称是void reorder,我很好奇,如果有无论如何,我可以调用我的max和min函数在这个函数内部,以压缩我的代码,或者如果有一种方法,我写它不使用if语句。我已经开始了,我想知道在我继续之前该做什么。谢谢你
#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;
}
我将把这三个值放在一个向量中并调用排序算法。这提供了一个整洁的函数,没有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;
}
我不认为使用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);
}
可以在没有if
的情况下比较值,但我更喜欢清晰的代码,除非测量显示需要英勇的优化。也可以只使用两次交换对三个变量进行排序(参见这个问题)。
相关文章:
- 是否有类似std::lower_bound的函数,而不需要排序/分区输入
- std::sort()函数无法对向量的一部分进行排序
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 用于合并排序的合并函数
- 有没有比在库中添加一个并非由所有派生类实现的新虚拟函数更好的设计实践
- C++模板函数中的初始化 - 新的初始值设定项表达式列表被视为复合表达式
- 放置上的析构函数 - 新
- 在 C++20 中对概念约束函数进行排序的规则是什么?
- 在 c++ 中使用 lambda 函数进行排序
- OPENCV C 调整大小函数:新宽度应乘以3
- Visual Studio:热键/自动对.h中的函数进行排序以与.cpp保持一致的方式
- 使用自定义比较函数进行C 排序
- 根据对象的成员函数进行排序
- C++:构造函数:新实例中的父类参数和设置值
- C++std::使用自定义函数进行排序
- 比较函数用于排序字符串的矢量(每个字符串都是一个数字)C++
- 当另一个函数使用比较函数进行排序时,如何在类中编写比较函数
- 创建没有重复项的新排序向量
- 链表单函数归并排序中的分段故障
- 意外返回主函数和排序字符串函数