C ++ notes (6) - OJ on a single point and multi-point test test

Single point test

PAT is the use of single-point test (LeetCode should also be a single point test). Single-point test system determines the output of each data is correct, then the correct value is obtained by this test and the test. The topic score is equal to the total score and by data.

On writing code requires only a single point test can be performed again in accordance with normal program logic.

Multi-point testing

Multi-point test requires a one-time program to run all of the data, and require that all outputs are quite right to AC, as long as a set of data output error then this problem can only be 0. Most of OJ are this way. Only in this way can the rigors of written code is strict. Multi-point testing program needs to be able to run all the data, so the program must ensure that there is a way to execute code repeatedly core part, so need to use cycle.

There are usually title 3 input format, the following is the corresponding input program:

while...EOF型

scanfFunction returns successfully read, the number of parameters when read failure when scanfthe function returns -1, and C using EOF (End Of File) is represented -1.

while(scanf("%d", &n) != EOF) {
    // 这里填运行代码
}

Another gets(str):

while(gets(str) != NULL) {
    // 这里填核心代码
}

while...break型

This is required to stop in the title input when the input data meets certain conditions, for example when the input aand bboth end input is 0:

#include <stdio.h>

int main() {
    int a, b;
    while(scanf("%d%d", &a, &b) != EOF) {
        if(a==0 && b==0) break;
        printf("%d\n", a+b);    // 这里可以换成别的
    }
    return 0;
}

Another condition is to launch while in the determination:

#include <stdio.h>

int main() {
    int a, b;
    while(scanf("%d%d", &a, &b), a || b) {
        printf("%d\n", a+b);
    }
    return 0;
}

So that when aand bas long as there is not a 0 it will loop forever.

while(T--)型

This is set every time a given number of test data, so it is necessary to store a variable number of T program to be executed, the last program execution cycle T times, each time to solve a set of data:

#include <stdio.h>

int main() {
    int T, a, b;
    scanf("%d", &T):
    while(T--) {
        scanf("%d%d", &a, &b);
        printf("%d\n", a+b);
    }
    return 0;
}

In addition, multi-site testing should pay attention to reset it before each cycle variables and arrays, arrays usually reset memsetor fill.

reference

"Algorithm notes," Hu, who with

Guess you like

Origin www.cnblogs.com/yejianying/p/cpp_notes_6.html