ZJU-PAT 1065. A+B and C (64bit) (20)

本文介绍了一个程序设计问题,即如何判断三个给定整数A、B和C是否满足A+B>C的条件,特别关注了整数溢出的情况,并提供了一种使用C++实现的解决方案。

Given three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C.

Input Specification:

The first line of the input gives the positive number of test cases, T (<=10). Then T test cases follow, each consists of a single line containing three integers A, B and C, separated by single spaces.

Output Specification:

For each test case, output in one line "Case #X: true" if A+B>C, or "Case #X: false" otherwise, where X is the case number (starting from 1).

Sample Input:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
Sample Output:
Case #1: false
Case #2: true
Case #3: false

分析:

1.考虑相加溢出的情况

(1)  a > 0 && b>0 && a+b <0  , 因 a+b > c,故true

(2)  a < 0 && b<0 && a+b >0 , 因 a+b < c,故false

2. PAT上不支持 __int64数据,采用long long型数据

#pragma warning (disable:4786)
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;

int main()
{
    int T;
    cin>>T;
    for(int i=1;i<=T;i++)
    {
        long long int  a,b,c;
        cin>>a>>b>>c;

        cout<<"Case #"<<i+1<<": ";
        
        bool flag;
        long long ans =a+b;
        if(a>0 && b>0 && ans<=0)
            flag=true;
        else if(a<0 && b <0 && ans >= 0)
            flag=false;
        else
            flag = (ans > c);
        if(flag)
            cout<<"true"<<endl;
        else
            cout<<"false"<<endl;
    }
    return 0;
}



x@pi:~/WiringPi/WiringPi $ sudo apt install -y python3-rpi.gpio python3-spidev python3-can # 通过 pip 安装最新版 (可选) sudo pip3 install RPi.GPIO spidev python-can Reading package lists... Done Building dependency tree... Done Reading state information... Done python3-spidev is already the newest version (20200602~200721-1+bookworm). The following additional packages will be installed: python3-msgpack python3-packaging rpi.gpio-common Suggested packages: python-can-doc The following packages will be REMOVED: python3-rpi-lgpio The following NEW packages will be installed: python3-can python3-msgpack python3-packaging python3-rpi.gpio rpi.gpio-common 0 upgraded, 5 newly installed, 1 to remove and 159 not upgraded. Need to get 301 kB of archives. After this operation, 1,467 kB of additional disk space will be used. Get:2 http://mirrors.zju.edu.cn/raspbian/raspbian bookworm/main armhf python3-packaging all 23.0-1 [32.5 kB] Get:1 http://raspbian.raspberrypi.com/raspbian bookworm/main armhf python3-msgpack armhf 1.0.3-2+b1 [62.0 kB] Get:3 http://mirrors.zju.edu.cn/raspbian/raspbian bookworm/main armhf python3-can all 4.1.0-1 [179 kB] Get:4 http://raspbian.raspberrypi.com/raspbian bookworm/main armhf rpi.gpio-common armhf 0.7.1~a4-1+b2 [6,808 B] Get:5 http://raspbian.raspberrypi.com/raspbian bookworm/main armhf python3-rpi.gpio armhf 0.7.1~a4-1+b2 [21.2 kB] Fetched 301 kB in 4s (85.8 kB/s) (Reading database ... 137316 files and directories currently installed.) Removing python3-rpi-lgpio (0.6-0~rpt1) ... Selecting previously unselected package python3-msgpack. (Reading database ... 137301 files and directories currently installed.) Preparing to unpack .../python3-msgpack_1.0.3-2+b1_armhf.deb ... Unpacking python3-msgpack (1.0.3-2+b1) ... Selecting previously unselected package python3-packaging. Preparing to unpack .../python3-packaging_23.0-1_all.deb ... Unpacking python3-packaging (23.0-1) ... Selecting previously unselected package python3-can. Preparing to unpack .../python3-can_4.1.0-1_all.deb ... Unpacking python3-can (4.1.0-1) ... Selecting previously unselected package rpi.gpio-common:armhf. Preparing to unpack .../rpi.gpio-common_0.7.1~a4-1+b2_armhf.deb ... Unpacking rpi.gpio-common:armhf (0.7.1~a4-1+b2) ... Selecting previously unselected package python3-rpi.gpio. Preparing to unpack .../python3-rpi.gpio_0.7.1~a4-1+b2_armhf.deb ... Unpacking python3-rpi.gpio (0.7.1~a4-1+b2) ... Setting up rpi.gpio-common:armhf (0.7.1~a4-1+b2) ... Setting up python3-rpi.gpio (0.7.1~a4-1+b2) ... Setting up python3-packaging (23.0-1) ... Setting up python3-msgpack (1.0.3-2+b1) ... Setting up python3-can (4.1.0-1) ... Processing triggers for man-db (2.11.2-2) ... error: externally-managed-environment × This environment is externally managed ╰─> To install Python packages system-wide, try apt install python3-xyz, where xyz is the package you are trying to install. If you wish to install a non-Debian-packaged Python package, create a virtual environment using python3 -m venv path/to/venv. Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make sure you have python3-full installed. For more information visit http://rptl.io/venv note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages. hint: See PEP 668 for the detailed specification. x@pi:~/WiringPi/WiringPi $
06-27
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值