#include <iostream>
using namespace std;
int main()
{
int num1,num2,num3,num4;
int x;
int y;
cin>>num1>>num2>>num3>>num4;
if (num1 > num2)
{
x=num1;
}
else
{x = num2;
}
if(num3>num4)
{y = num3;
}
else
{
y= num4;
}
if (x>y)
{cout<<"the largest number is:"<<x;
}
else
{
cout<<"the largest number is :"<<y;
}
return 0;
}
这是我的代码,用于打印4个数字中的最大数字。 问题是我被要求优化或压缩解决方案。 我试过,但无法找到另一种方式来写这样program.Can任何一个可以帮助我优化的解决方案,使之更好.. 忽略语法错误..打印4个数字中的最大数字优化
要压缩源代码,把所有4个值中的阵列。然后在for循环中检查当前冠军对抗当前元素。保持较高者。 4轮后退出并输出赢家。实际上你可以做任何其他变量。如果(num [i]> num [0])num [0] = num [i]。然后在循环cout << num [0]; – WDS
提示:使用'std :: vector'来存储数字并查找循环中最大的数字。 –