PAT 1001 (2)

PAT 1001 (2)

Title description:

给定区间[-231, 231]内的3个整数A、B和C,请判断A+B是否大于C。

Enter a description:

输入第1行给出正整数T(<=10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。

Output description:

对每组测试用例,在一行中输出“Case #X: true”如果A+B>C,否则输出“Case #X: false”,其中X是测试用例的编号(从1开始)。

Input example:

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

Example output:

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

Code:

#include <iostream>
#include<cstring>
#include<algorithm>

#define what_is(x) cerr <<#x<<"is"<<x<<endl;

using namespace std;
using II =long long;
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    for(int cases=1;cases<=T;cases++)
    {
        II a,b,c;
        cin>>a>>b>>c;
        if(a>c-b)
        {
            printf("Case #%d: true\n",cases);
        }
        else
        {
            printf("Case #%d: false\n",cases);
        }
    }

    return 0;

}

on ios::sync_with_stdio(false); and cin.tie(0); cout.tie(0);

一、sync_with_stdio

This function is a "compatibility stdio" switch. C++In order to be compatible with C, to ensure that the program will not be confused when using std::printfand std::cout, the output streams are tied together.

cin, coutThe reason why the efficiency is low is because the output must be stored in the buffer first, and then output, resulting in a decrease in efficiency. This statement can just cancel the iostreaminput and output cache, which can save a lot of time, making the efficiency almost the scanfsame printfas that of the output.

Two. tie

tieIt is a function that binds the two stream. If the parameter is empty, it returns the current output stream pointer.
By default , cinit is coutbound to call flush every time the << operator is executed, which will increase the IO burden. You can unbind with tie(0)(0 means NULL) to further speed up execution efficiency.cincout

Guess you like

Origin blog.csdn.net/Duba_zhou/article/details/126395246