多次判断? 三目?
1.题目:
There are three cards on the desk, each with a positive integer written on it. The integers on the cards are
A, B, and C.
You have chosen two cards and picked them up.
Find the maximum possible sum of the integers written on the picked cards.
Constraints1≤A,B,C≤100.All values in input are integers.
2.思考:
emmmm就是三个数中,输出两个较大值的和(摸鱼题)。
想法1: 将三个数排序,再输出两个较大的值的和。
用数组+sort函数一步到位,或者用多重判断if判断
想法2: 直接用三个数的两两和进行比较,输出最大的就可。感觉会复杂一点,但是也是一种方法嘛。(但是上交的时候居然wa了)
3.代码实现:
想法1:
//数组+sort
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
int a[4];//够用就行
scanf("%d%d%d",&a[0],&a[1],&a[2]);
sort(a,a+3);//默认升序,所以输出是最后两个的和
printf("%d\n",a[1]+a[2]);
return 0;
}
//多重if判断
#include<stdio.h>
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a>b){
if(b>c)//a>b>c
printf("%d\n",a+b);
else //a>b,c>b
printf("%d\n",a+c);
}
else{//b>a
if(c>a)//b>a,c>a
printf("%d\n",b+c);
else//b>a>c
printf("%d\n",a+b);
}
return 0;
}
想法2:
//比较三个数的两两和大小,输出最大的
#include<stdio.h>
int main(){
int a,b,c,sum;
scanf("%d%d%d",&a,&b,&c);
sum=a+b>(a+c>b+c?a+c:b+c) ?a+b: (a+c>b+c?a+c:b+c);
printf("%d\n",sum);
return 0;
}
交acm的时候wa了,还没想通。