算法题(3):比较大小A+B>C

本文探讨了一道算法问题,涉及在[-2^31, 2^31]区间内的三个数a、b、c,如何判断(a+b)是否大于c。文章提供了c和c++两种语言的解决方案,并附有示例输入、输出及运行演示。" 111509940,10296828,Axure实现关键词高亮:搜索框实时变色效果,"['原型设计', 'Axure教程', '交互设计', '数据筛选', '动态面板']

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

算法题(3):比较大小 A+B>C

每天两道算法题

题目

对于3个属于区间 [ -231, 231 ] 的数a,b,c 。请问(a+b)>c是否成立?

举例输入:

4
1 2 3
2 3 4
2147483647 0 2147483646
0 -2147483648 -2147483647

举例输出:

Case #1: false
Case #2: true
Case #3: true
Case #4: false

运行演示:

$ ./a.out 
4
1 2 3
2 3 4
2147483647 0 2147483646
0 -2147483648 -2147483647
Case #1: false
Case #2: true
Case #3: true
Case #4: false









c

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

typedef struct abc
{
    int32_t a;
    int32_t b;
    int32_t c;
} abc_t;

int main( void )
{
    int n;

    /* 输入阶段 */
    scanf("%d", &n);
    abc_t *arr = malloc( n*sizeof(abc_t) );
    for( int i=0; i<n; ++i ) {
        scanf("%d %d %d", &arr[i].a, &arr[i].b, &arr[i].c);
    }

    /* 输出阶段 */
    for( int i=0; i<n; ++i ) {
        if( (arr[i].a+arr[i].b) > arr[i].c ) {
            printf("Case #%d: true\n" , i+1);
        }
        else {
            printf("Case #%d: false\n", i+1);
        }
    }
    
    free(arr);
    return 0;
}

c++

#include <cstdint>
#include <tuple>
#include <list>
#include <iostream>

using namespace std;

list<tuple<int64_t,int64_t,int64_t>> coll;

int main( void )
{
    int n;
    int64_t a,b,c;
    
    /* 输入阶段 */
    cin >> n;
    while( n-- > 0 ) {
        cin >> a >> b >> c;
        tuple<int64_t,int64_t,int64_t> tmp(a,b,c);
        coll.push_back(tmp);
    }
    
    /* 输出阶段 */
    list<tuple<int64_t,int64_t,int64_t>>::iterator pos;
    n = 1;
    for( pos=coll.begin(); pos!=coll.end(); ++pos ) {
        a = get<0>(*pos);
        b = get<1>(*pos);
        c = get<2>(*pos);
        
        cout << "Case #" << n++ << ": ";
        if( (a+b) > c ) {
            cout << "true" << endl;
        }
        else {
            cout << "false" << endl;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值