输出三个整数之间两个数的最大和

多次判断? 三目?

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了,还没想通。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值