PAT1011 A+B 和 C (15 分)

本文提供了一种算法,用于判断三个整数是否能构成一个三角形的边长。使用C++和Python实现,通过比较两短边之和是否大于最长边来确定。提供了两种C++实现方式和一种Python实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

在这里插入图片描述

C++解法

解法1

#include<cstdio>
#include<cstring>
using namespace std;
int main() {
	int T, tcase = 1;
	scanf("%d", &T);
	while (T--) {
		long long a, b, c;
		scanf("%lld%lld%lld", &a, &b, &c);
		if (a + b > c) {
			printf("Case #%d: true\n", tcase++);
		}
		else {
			printf("Case #%d: false\n", tcase++);
		}
	}
	return 0;
}

解法2

#include <iostream>
using namespace std;
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        long long int a, b, c;
        scanf("%lld%lld%lld", &a, &b, &c);
        printf("Case #%d: %s\n", i + 1, a + b > c ? "true" : "false");
    }
    return 0;
}

在这里插入图片描述

python解法

N = int(input())

for i in range(N):
    a, b, c = tuple(map(int,input().split()))

    if a+b > c:
        print("Case #{}: true".format(int(i+1)))
    else:
        print("Case #{}: false".format(int(i+1)))

在这里插入图片描述
python牛逼
不过做算法最好用C++
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值