PAT B1011 A+B和C

來源:互聯網
上載者:User

題目地址:https://www.patest.cn/contests/pat-b-practise/1011

題目描述:

給定區間[$-2^31, 231]內的3個整數A、B和C,請判斷A+B是否大於C。

輸入格式:

輸入第1行給出正整數T(<=10),是測試案例的個數。隨後給出T組測試案例,每組佔一行,順序給出A、B和C。整數間以空格分隔。

輸出格式:

對每組測試案例,在一行中輸出“Case #X: true”如果A+B>C,否則輸出“Case #X: false”,其中X是測試案例的編號(從1開始)。

輸入範例:

41 2 32 3 42147483647 0 21474836460 -2147483648 -2147483647

輸出範例:

Case #1: falseCase #2: trueCase #3: trueCase #4: false

解題思路:

輸入 T,用以表示下面輸入的數組組數,同時令 tcase 表示當前是第幾組資料,初值為 1 。 對每組資料,判斷 A+B 是否大於 C 若 A+B > C,則輸出Case#%d : true 否則,輸出 Case#%d : false

注意:題目給出的範圍是[-263,263],首先需要知道 int 型的資料範圍是 [-263,-263-1],在最大值這裡就會超過 int 型的範圍。另外,兩個 int 型變數相加,最後是可能超過 int 型的,因此在本題中,必須使用 long long 作為 ABC 的變數類型,輸入、輸出格式必須是 %lld,否則就會返回“答案錯誤”。

C++完整代碼如下:

#include<cstdio>int main(){    int T, tcase = 1;    scanf("%d", &T);    //輸入資料群組數    while(--T){         //迴圈 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; } 

python完整代碼如下:

from __future__ import print_functionif __name__ == "__main__" :    cnt = input()    for i in range(cnt) :        num = raw_input().split(' ')        print("Case #", end='')        print(i+1, end=": ")        if int(num[0]) + int(num[1]) > int(num[2]) :            print("true")        else :            print("false")

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.