ACM Learning process 2--ACM The input and output methods used in the topic

Source: Internet
Author: User
Tags function prototype int size terminates

Because the ACM Competition topic input data and the output data generally has many groups (indefinite), and the format is various, therefore, how handles the question the input output is to the programmer most basic request.

(i) The first type of input: the input does not indicate how many input blocks, with EOF as the end sign.

Example1:

Calculate A + B.

Input:each line would contain two integers A and B. Process to end of file.

Output:for each case, output A + B at one line.

Sample Input
1 1
2 2

Sample Output
2
4

The meaning of this example is to output two digits per read two digits, and the input solution is as follows:

C Syntax:

while (scanf ("%d%d", &a, &b)!= EOF)

{
Put in the calculation and the statement can
}

C + + Syntax:

while (Cin >> a >> B)
{
Put in the calculation and the statement can
}

Note: The return value of the scanf function is the number of variables read out, such as: scanf ("%d%d", &a, &b);
If there is only one integer input, the return value is 1, and if there are two integer inputs, the return value is 2, and if none, the return value is-1. EOF is a predefined constant, equal to-1.

In some competitions, the test data is not by the background through CIN and cout input, but to read the file, here you need to understand and master C + + read and write files operation. Reading and writing to a file in C + + requires opening a file, then reading and writing using Fin and fout, and closing the file after the read and write operation of all the data has been completed. The following is C and C + + read and write file operations, or the above calculation a+b as an example, see C + + and C is how to manipulate file flow.

C + + Syntax:

#include <iostream>

#include <fstream>

using namespace Std;

void Main ()

{

Ifstream fin ("in.txt");

Ofstream fout ("OUT.txt");

int a,b;

while (FIN>>A>>B)

fout<<a+b<<endl;

Fin.close ();

Fout.close ();

}

C Syntax:

#include <stdio.h>

void Main ()

{

FILE *fin,*fout;

int a,b;

Fin=fopen ("In.txt", "R");

Fout=fopen ("OUT.txt", "w");

while (FSCANF (FIN, "%d%d", &a, &b)!= EOF)

fprintf (Fout, "%d\n", a+b);

Fclose (Fin);

Fclose (Fout);

}

(ii) Type II input: the title tells you that there are n input blocks, followed by n input blocks.

Example2:

Problem Description

Your task is to Calculate A + B.
Input:

Input contains an integer n in the The "the", and then n lines follow. Each line consists of the a pair of integers a and b, separated by a space, one pair of integers per line.
Output:

For each pair of input integers a and b you should output the sum of a and B in one line, and with one line of output for Each line in input.
Sample Input:

2

1 5

10 20

Sample Output:

6

30

In this topic it is clear that there are 2 sets of input data, and each group represents the two addends that need to be computed, so only two cycles are required. This type of input data is as follows:

C Syntax:

scanf ("%d", &n);

for (i=0; i<n; i++)
{
scanf ("%d%d", &a, &b);

printf ("%d\n", a+b);
}

C + + Syntax:

CIN >> N;
for (i=0; i<n; i++)
{
cin>>a>>b;

cout<<a+b<<endl;
}

(iii) Type III input: input does not specify how many input blocks, but with a special input as the end flag.

Example3:

Problem Description

Your task is to Calculate A + B.

Input

Input contains multiple test cases. Each test case contains a pair of integers a and B, one pair of integers per line. A test case containing 0 0 terminates the "input and this" test case are not. Processed.
Output

For each pair of input integers a and b you should output the sum of a and B in one line, and with one line of output for Each line in input.
Sample Input

1 5

10 20

0 0
Sample Output

6

30

The topic here is also to compute A and B's and to read two numbers at a time, and then output and, but as to how many sets of data need to be input is determined by the special input, in the topic when the two number of reads are zero, then the input of the data is considered to end.

C Syntax:

#include <stdio.h>

int main ()

{

int a,b;

while (scanf ("%d%d", &a, &b) && (a!=0 && b!=0))

printf ("%d\n", a+b);

}

C + + Syntax:

#include <iostream>

using namespace Std;

int main ()

{

int a,b;

while (CIN>>A>>B)

{

if (a==0 && b==0)

Break

cout<<a+b<<endl;

}

return 0;

}

For the third type of input and output, the usual solution is as follows, as to what kind of end, this specific topic, but usually the method of such input solution is as follows, only to be based on the specific end of the rules of a few changes can be.

C Syntax:

while (scanf ("%d", &n)!=eof && n!=0)

{
}

C + + Syntax:

while (CIN >> n && n!= 0)
{
}

(iv) Class fourth input: The above three kinds of combinations

Example4:

Your task is to Calculate the sum of some integers.
Input:

Input contains multiple test cases. Each test case contains an integer n, and then n integers follow in the same line. A test case starting with 0 terminates the "input" and "This" test case are not. Processed.
Output:

For each group of input integers your should output their sum in one line, and with one line of output for each line in INP Ut.
Sample Input:

4 1 2 3 4

5 1 2 3 4 5

0
Sample Output

10

15

This topic is also calculated a+b and, but the input format of the data is this, first input n, representing the following n data to add, when n is 0 o'clock, indicating the end of input. Therefore, the input format in this topic is as follows:

#include <iostream>

using namespace Std;

int main ()

{

int i,n,s,sum;

while (Cin>>n && n!=0)

{

sum=0;

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

{

cin>>a;

Sum+=a;

}

cout<<sum<<endl;

}

return 0;

}

(v) class fifth input: The input is a whole line of strings, this class input method is as follows:

C Syntax:

Char buf[20];
Gets (BUF);

C + + Syntax:

Char buf[255];
Cin.getline (buf, 255);

Note: in C canf ("%s%s", STR1,STR2), separated by one or more spaces between multiple strings;

If the gets function is used, it should be gets (STR1); Gets (STR2); Strings are separated by a carriage return. In general, the SCANF function is accepted for short characters, and the gets function is accepted for long characters. The GetChar function accepts only one character at a time and is often used C=getchar (). Reading a string in C + + usually uses the Cin.getline function to accept the user's input until the specified number is reached, or the user enters a specific character. Its function declaration form (function prototype) is as follows:

istream& getline (char line[], int size, char EndChar = ' \ n ');

Char line[]: is an array of characters in which the user input is stored in the array.

int size: Accepts a maximum of several characters and the user's input exceeding size will not be accepted.

Char EndChar: Automatically ends when the user enters the character specified by the EndChar, and the default is a carriage return.

(vi) The first type of output: an input block corresponds to an output block,output block with no blank lines between them.

Example5:

Sample Input:

1 5

10 20

Sample Output:

6

30

Here we are mainly concerned about the output format, the same is the calculation of the a+b and, the request does not output two numbers and the output of a line wrap. The output of this class is as follows:

C Syntax:

{

Calculated statement
printf ("%d\n", ans);

}

C + + Syntax:

{

Calculated statement
cout << ans << endl;
}

(vii) Type II output: an input block corresponds to an output block, with a blank line after each output block.

Example6:

Sample Input:

1 5

10 20

Sample Output:

6

(There is a blank line here)

30

(There is a blank line here)

The output in this way is very much the same as the output mode of the top class, each group of data results output followed by two lines, the first line is to move the output cursor to the next line of the output in fact, the second line is to add a blank line, so that the position of the output cursor moved to the next line in fact position.

The output of this class is as follows:

C Syntax:

{

Calculated statement
printf ("%d\n\n", ans);

}

C + + Syntax:

{

Calculated statement
cout << ans << endl << Endl;
}

(eight) The third type of output: an input block corresponds to an output block, after each output block has a blank line. Note: This is different from the second type of output, the second class four each output block followed by a blank line, here is the output block between the empty line, meaning that the last output block after the empty line. Of course, in the actual programming, unless you know exactly how many input blocks, the last empty line is really not very good control, of course, if the algorithm topic is not clearly told how many input blocks, still use this class output, generally to the last redundant empty line is not to do the format check.

Example7:
Sample Input:

3

4 1 2 3 4

5 1 2 3 4 5

3 1 2 3
Sample Output:

10

(There is a blank line here)

15

(There is a blank line here)

6 (There is no blank line here)

The output of this class is as follows:

C Syntax:

scanf ("%d", &count);

for (k=0;k<count;k++)
{
while (...)
{
printf ("%d\n", result);
}
if (k!=count-1) printf ("\ n");
}

C + + Syntax:

Similarly, the output statement can be changed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.