求解四个数中较大的两个数
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
int main1(int a, int b) {
return a > b ? a : b;
}
int main()
{
int a,b,c,d;
int max,max2,max3,max4,max5;
int finalmax;
printf("请输入四个数:");
scanf("%d%d%d%d", &a,&b,&c,&d);
max = main1(a,b);
max2 = main1(c, d);
max3 = main1(max, max2);
if (max3 == a) {
max4 = main1(b, c);
max5 = main1(c, d);
finalmax = main1(max4, max5);
}
else if (max3 == b) {
max4 = main1(a, c);
max5 = main1(c, d);
finalmax = main1(max4, max5);
}
else if (max3 == c) {
max4 = main1(a, b);
max5 = main1(b, d);
finalmax = main1(max4, max5);
}
else {
max4 = main1(a, b);
max5 = main1(b, c);
finalmax = main1(max4, max5);
}
printf("最大值为:%d,第二大值为:%d", max3,finalmax);
return 0;
}
根据提示输入四个数字,输入时要用空格隔开。