"ACM" "pro.1000" A + B problem ACM tour begins

Source: Internet
Author: User

A + B Problem

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 446348 Accepted Submission (s): 141167

link:http://acm.hdu.edu.cn/showproblem.php?pid=1000


#include <stdio.h>Main () { int A, b; while (scanf ("%d%d", &a,&b)! =EOF) printf ("%d\n ", A +b) ; return 0 ;}

The first time to do ACM, encountered problems. After searching the internet, it was solved.

Here is a blog post about what ' EOF ' is in C:

Original link: http://www.kuqin.com/language/20111112/314745.html

What is the C-language EOF?

One of the problems I encountered when I was learning C was EOF.

It is an abbreviation of end of file that represents the end of the stream. The "text stream" here can be either a file or a standard input (stdin).

For example, the following code indicates that, if it is not the end of the file, the contents of the file are copied to the screen.

int C;

  while ((c = fgetc (FP))! = EOF) {

Putchar (c);

}

Naturally, I thought, at the end of each file, there is a special character called EOF, which reads to this character and the operating system thinks the file is over.

However, later I found that EOF is not a special character, but a constant defined in the header file stdio.h, which is generally equal to-1.

#define EOF (-1)

So, I was confused.

If EOF is a special character, it can be assumed that each text file has an EOF (that is, 1) at the end, because the ASCII code corresponding to the text is positive and cannot have negative values. But what about binary files? What do you do with the 1 inside the file?

This question let me think for a long time, later checked the data to know, in the Linux system, EOF is not a character at all, but when the system read to the end of the file, the return of a signal value (that is, 1). As for how the system knows the end of the file, it says by comparing the length of the file.

Therefore, the processing file can be written as follows:

int C;

while ((c = fgetc (FP))! = EOF) {

Do something

}

There is a problem with this writing. FGETC () returns EOF not only when the end of the file is encountered, but also when an error occurs. Therefore, the C language also provides the feof () function, which is used to ensure that the end of the file is indeed. The Code feof () version above is written as follows:

int C;

while (!feof (FP)) {

c = fgetc (FP);

Do something;

}

However, there are also problems with this writing. FGETC () After reading the last character of the file, the C feof () function still returns 0, indicating that the end of the file is not reached; only if FGETC () reads a character backwards (that is, over the last character), feof () returns a non-0 value indicating the end of the file.

So, in this way, if a file contains n characters, then the internal operation of the while loop runs n+1 times. So the safest way to do this is as follows:

int c = fgetc (FP);

while (c! = EOF) {

Do something;

c = fgetc (FP);

}

if (feof (FP)) {

printf ("\ n End of file reached.");

} else {

printf ("\ n Something went wrong.");

}

In addition to representing the end of a file, EOF can also represent the end of a standard input.

int C;

while ((c = GetChar ())! = EOF) {

Putchar (c);

}

However, the standard input is not the same as the file, the length of the input cannot be known beforehand, and a character must be entered manually to indicate that EOF is reached.

Linux, at the beginning of a new line, press ctrl-d, which represents EOF (if you press ctrl-d in the middle of a row, the output "standard input" buffer, so you must press two times ctrl-d); In Windows, Ctrl-z represents EOF. (Incidentally, by pressing Ctrl-z in Linux, the process is interrupted, suspended in the background, the FG command can be re-cut back to the foreground, and the process is terminated by pressing CTRL-C.) )

So, what if you really want to enter ctrl-d? At this point you must press CTRL-V, then you can enter the ctrl-d, the system will not think this is the EOF signal. Ctrl-v said that by "literal meaning" to interpret the next input, if you want to press "literal meaning" input ctrl-v, input two consecutive times on the line.

Finish

"ACM" "pro.1000" A + B problem ACM tour begins

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.