PAT B 1011 A + B and C (C language)

1011 A + B and C (15 minutes)

Given interval [-231, 231] three integers A, B and C in, please determines whether A + B is greater than C.

Input format:
Enter line 1 gives a positive integer T (≤10), are the number of test cases. T is then given set of test cases, each per line, the order is given A, B and C. Between integer separated by a space.

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

Sample input:

4

1 2 3

2 3 4

2147483647 0 2147483646

0 -2147483648 -2147483647

Sample output:

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

for loop to achieve:

#include <stdio.h>
int main()
{
    int n;
    long a,b,c;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {    
        scanf("%ld %ld %ld",&a,&b,&c);
        if(a+b>c)
            printf("Case #%d: true\n",i);
        else
            printf("Case #%d: false\n",i);
    }
    return 0;
}

This is achieved using a while loop:

#include <stdio.h>
int main()
{
    int n,i=1;
    long a,b,c;
    scanf("%d",&n);
    while(n--)
    {    
        scanf("%ld %ld %ld",&a,&b,&c);
        if(a+b>c)
            printf("Case #%d: true\n",i++);
        else
            printf("Case #%d: false\n",i++);
    }
    return 0;
}
Published 15 original articles · won praise 0 · Views 289

Guess you like

Origin blog.csdn.net/weixin_44562957/article/details/104055860