程序可以在不使用数组的情况下找到5个数字中最大和最小的
Program to find largest and smallest among 5 numbers without using array
昨天我参加了一次面试,要求我创建一个程序,在不使用数组的情况下从5个数字中找到最大和最小的数字。
我知道如何使用数组创建程序。
int largestNumber;
int smallestNumber;
int numbers[n];
largestNumber=numbers[0];
smallestNumber=numbers[0];
for (i=0 ; i<n; i++)
{
if (numbers[i] > largestNumber)
{
largest = numbers[i];
}
if (numbers[i] < smallestNumber)
{
smallestNumber= numbers[i];
}
}
但是如何在不使用数组的情况下创建它。有什么帮助吗??
#include <algorithm>
#include <iostream>
template <typename T>
inline const T&
max_of(const T& a, const T& b) {
return std::max(a, b);
}
template <typename T, typename ...Args>
inline const T&
max_of(const T& a, const T& b, const Args& ...args) {
return max_of(std::max(a, b), args...);
}
int main() {
std::cout << max_of(1, 2, 3, 4, 5) << std::endl;
// Or just use the std library:
std::cout << std::max({1, 2, 3, 4, 5}) << std::endl;
return 0;
}
适用于从标准输入中提取的任意数量的数字:
#include <algorithm>
#include <iterator>
#include <iostream>
int main()
{
std::istream_iterator<int> it_begin(std::cin), it_end;
auto p = std::minmax_element(it_begin, it_end);
if (p.first != it_end)
std::cout << "min: " << *p.first << " max: " << *p.second;
}
免责声明:
从技术上讲,这不是C++标准所要求的。minmax_element
所需的最小迭代器类别是ForwardIterator
,而流迭代器不是。一旦一个输入迭代器被取消引用或递增,它的副本就不再保证可以取消引用或与其他迭代器相比较。它在我的机器上工作TM.:)
您可以这样做:
int min_num = INT_MAX; // 2^31-1
int max_num = INT_MIN; // -2^31
int input;
while (!std::cin.eof()) {
std::cin >> input;
min_num = min(input, min_num);
max_num = max(input, max_num);
}
cout << "min: " << min_num;
cout << "max: " << max_num;
它从标准输入读取数字,直到eof(它不在乎你有多少-5或1000000)。
这不是一个有效的答案,但它仍然适用于
int a,b,c,d,e,largest;
if ((a>b) and (a>c) and (a>d) and (a>e))
{
largest=a;
}
else if ((b>a) and (b>c) and (b>d) and (b>e))
{
largest=b;
}
else if ((c>a) and (c>a) and (c>d) and (c>e))
{
largest=c;
}
else if ((d>a) and (d>c) and (d>a) and (d>e))
{
largest=d;
}
else
{
largest=e;
}
您可以使用类似的逻辑来fid最小值
让max最多容纳5个数字。将第一个数字分配给max。取第二个数字并将其与max进行比较,如果第二个数值大于max,则将其分配给max,否则不执行任何操作。接下来取第三个数字并将其与max进行比较,如果第三个数值大于max,则将其分配给max,否则不执行任何操作。对第4个和第5个数字执行相同操作。最后,最大值将保持最大值5。
例如5个连续数字
int largestNumber;
int smallestNumber;
int number;
std::cin>>number;
largestNumber = number;
smallestNumber = number;
for (i=0 ; i<5; i++)
{
std::cin>>number;
if (number > largestNumber)
{
largest = number;
}
if (numbers < smallestNumber)
{
smallestNumber= number;
}
}
您可以使用列表(或向量),它不是数组:
#include<list>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
list<int> l;
l.push_back(3);
l.push_back(9);
l.push_back(30);
l.push_back(0);
l.push_back(5);
list<int>::iterator it_max = max_element(l.begin(), l.end());
list<int>::iterator it_min = min_element(l.begin(), l.end());
cout << "Max: " << *it_max << endl;
cout << "Min: " << *it_min << endl;
}
>
和<
是传递性质,所以如果a > b
和b > c
,那么a > c
。所以你可以
int a=10, b=6, c=4, d=21, e=4;
int maxNum = a;
int maxNum = max(b, maxNum);
int maxNum = max(c, maxNum);
int maxNum = max(d, maxNum);
int maxNum = max(e, maxNum);
使用排序网络!
#include <iostream>
#include <utility>
int main()
{
int a, b, c, d, e;
std::cin >> a >> b >> c >> d >> e;
if (a < b) std::swap(a, b);
if (d < e) std::swap(d, e);
if (c < e) std::swap(c, e);
if (c < d) std::swap(c, d);
if (b < e) std::swap(b, e);
if (a < d) std::swap(a, d);
if (a < c) std::swap(a, c);
if (b < d) std::swap(b, d);
if (b < c) std::swap(b, c);
std::cout << "largest = " << a << 'n';
std::cout << "smallest = " << e << 'n';
}
如果你喜欢保持简单,那么这里是我的解决方案。
它适用于从标准输入中提取的任意数量的整数。它也适用于负整数。完成后输入end。
#include <iostream>
int main()
{
int max,min,input;
std::cout<<"Enter the number: ";
std::cin>>input;
min=max=input;
while(std::cin>>input){
if(input>max) max=input;
if(input<min) min=input;
std::cout<<"Enter the number: ";
}
std::cout<<"nMax: "<<max<<"nMin: "<<min;
}
void main()
{
int a,b,c,d,e,max;
max=a;
if(b/max)
max=b;
if(c/max)
max=c;
if(d/max)
max=d;
if(e/max)
max=e;
cout<<"Maximum is"<<max;
}
以下是我在不使用数组的情况下所做的操作。这是一种返回最高5分的方法。
double findHighest(double score1, double score2, double score3, double score4, double score5)
{
double highest = score1;
if (score2 > score1 && score2 > score3 && score2 > score4 && score2 > score5)
highest = score2;
if(score3 > score1 && score3 > score2 && score3 > score4 && score3 > score5)
highest = score3;
if(score4 > score1 && score4 > score2 && score4 > score3 && score4 > score5)
highest = score4;
if (score5 > score1 && score5 > score2 && score5 > score3 && score5 > score4)
highest = score5;
return highest;
}
数组的效率会高得多,但我不得不在不使用数组的情况下做作业。
int findMin(int t1, int t2, int t3, int t4, int t5)
{
int min1, min2, min3;
min1 = std::min(t1, t2);
min2 = std::min(t3, t4);
min3 = std::min(min1, min2);
return std::min(min3, t5);
}
int findMax(int t1, int t2, int t3, int t4, int t5)
{
int max1, max2, max3;
max1 = std::max(t1, t2);
max2 = std::max(t3, t4);
max3 = std::max(max1, max2);
return std::max(max3, t5);
}
这些函数非常混乱,但很容易理解,因此很容易记住,并且它只使用简单的min和max方法,这些方法最适合2个值。
int findMin(int t1, int t2, int t3, int t4, int t5)
{
int min;
min = t1;
if (t2 < min)
min = t2;
if (t3 < min)
min = t3;
if (t4 < min)
min = t4;
if (t5 < min)
min = t5;
return min;
}
int findMax(int t1, int t2, int t3, int t4, int t5)
{
int max;
max = t1;
if (t2 > max)
max = t2;
if (t3 > max)
max = t3;
if (t4 > max)
max = t4;
if (t5 > max)
max = t5;
return max;
}
下面是我的实现:简单而简短的
#include <iostream>
#include <cstdio>
using namespace std;
int max_of_five(int a, int b, int c, int d,int e){
int large= max(max(a, b), max(c,d));
return max(large,e);
}
int min_of_five(int a,int b,int c, int d,int e){
int small=min(min(a,b),min(c,d));
return min(small,e);
}
int main() {
int a, b, c, d,e;
scanf("%d %d %d %d %d", &a, &b, &c, &d,&e);
int ans = max_of_five(a, b, c, d, e);
int ans1=min_of_five(a,b,c,d,e);
printf("Max:t%dn", ans);
printf("Min:t%d", ans1);
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int largestNumber;
int smallestNumber;
int temp1;
cin >> temp1;
largestNumber = temp1;
smallestNumber = temp1;
for (int i = 0; i < 5; i++)
{
int temp;
cin >> temp;
for (int i = 0; i < 3; i++)
{
if (temp > largestNumber)
{
largestNumber = temp;
}
if (temp < smallestNumber)
{
smallestNumber = temp;
}
}
}
cout << "smallest number is " << smallestNumber<<endl;
cout << "lagest number is " << largestNumber<<endl;
system("pause");
return 0;
}
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- C++通过数字比较两个数字
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 如果我不知道每个列表中有多少个数字,我如何将给定数量的数字列表作为输入?
- 获取 2 个数字之间的差异百分比
- 在 txt 文件中显示前两个数字的程序
- 形成一个斐波那契三角形,使得每个数字是上面左对角线或右对角线上两个数字的总和
- 在C++中编程,将 3 个数字发送到一个函数,然后计算这 3 个数字的平均函数
- 两个数字的对称配对功能
- 将最多 50 位数字的 2 个数字相乘
- 查找中间两个数字的正则表达式的匹配项
- 使用C++具有两个数字的最短路径算法.(C++)
- 使用三个数字比较器进行排序
- 在 CPP 中交换 2 个数字
- 将两个数字添加为链表
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 在 C++ 中查找 2 个数字的 GCD
- 代码以查找数组中的最大数字,但它仅将第一个数字显示为最大数字. 有人请告诉我为什么