Xiaobai vs. PAT: Grade B 1011

1. Topic information

Given three integers A, B and C in the interval [-2 31 , 2 31 ], please determine whether A+B is greater than C.

Input format:

Enter the first line to give a positive integer T (<=10), which is the number of test cases. Then T groups of test cases are given, each group occupies one line, and A, B and C are given in order. Integers are separated by spaces.

Output format:

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

2. Basic ideas

I have thought of two ideas: the first is to store the three numbers to be compared in three long variables, and each time three numbers are input, the results are immediately fed back, and a single for loop is enough; The investigated values ​​are uniformly stored in a long-type array, and then the for loop is compared to the size. At this time, two for loops are required.

Third, the specific implementation code and the problems encountered

(1) Starting from the second way of thinking

Put the code first:

#include<stdio.h>

intmain ()

{

    long A[30];

    int n,signal,i; /*n is used to store the number of samples, the first time signal is used to eat the carriage return, the second time is used to read the input sequentially (or the space is eaten), i is used to execute the loop (one time or twice) */

    n = getchar () - 48 /* get a single character, ASCII code value when %d */

    signal = getchar();

    for(i=0;i<3*n;i+=3)

    {

        scanf("%ld %ld %ld", &A[i],&A[i+1],&A[i+2]);

      /*  signal = getchar();*/

        printf("A[%d]=%ld,n=%d\n", i,A[i],n);

      /*  i++;*/

    }

    for(i = 3; i/3<=n;i+=3)

    {

        if(A[i-3] + A[i-2] > A[i-1])

            printf("Case #%d: true\n", i/3);

        else

            printf("Case #%d: false\n", i/3);

    }

}

View submissions

评测结果

时间 结果 得分 题目 语言 用时(ms) 内存(kB) 用户
4月24日 15:34 答案正确 15 1011 C (gcc 4.7.2) 1 264 chauncyyoung

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 1 256 9/9
1 答案正确 1 264 3/3
2 答案正确 1 260 3/3


代码中包含了测试不同方法,用备注的形式注明了;不同参数在不同阶段的调用目的也用备注形式写明了。

遇到的问题:

1、getchar()取值。getchar获取的是单个字符,所以除0-9的数字只能单个数字获取;且获取的数字是以字符存储,所以用于循环比大小或者数值运算时,

千万千万要记得这一点!!!

避免方法和代码中相同,即读取0-9的数字后-48,即为ASCII码对应的数值;

2、scanf()读取数值的问题。scanf是以空格、换行后停止读入,所以既可以每次读入一个,也可以一次读入多个,开心就好。也不需要用getchar吃掉空格和换行符,因为scanf已经选择性无视它们了;

 
 

(一)接下来是第一种思路

先放上代码:

#include<stdio.h>

int main()

{

    long A,B,C;

    int n,signal,i; 

    n = getchar() - 48/*得到的是单个字符,%d时是ASCII码值*/

    signal = getchar();

    for(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);

    }

}

评测结果

时间 结果 得分 题目 语言 用时(ms) 内存(kB) 用户
4月24日 15:36 答案正确 15 1011 C (gcc 4.7.2) 1 384 chauncyyoung

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 1 264 9/9
1 答案正确 1 384 3/3
2 答案正确 1 384 3/3


运行时间几乎相同但是内存占用更大一丢丢,至于为什么...暂时不清楚,等日后了解了再更新吧~

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326659387&siteId=291194637