[ACM] [Pro.1000] A + B Problem ACM journey started, pro.1000acm
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 = 1, 1000
#include <stdio.h>main(){ int a,b; while(scanf("%d%d",&a,&b)!=EOF) printf("%d\n",a+b); return 0;}
I encountered a problem when I started ACM for the first time. After searching on the internet, it can be solved.
Here, I will reference a blog post to introduce what 'eof 'is in C language:
Link: http://www.kuqin.com/language/20111112/314745.html
What is C language EOF?
When I learned the C language, I encountered an issue with EOF.
It is the abbreviation of end of file, indicating the end of "stream. Here, the "Text Stream" can be a file or a standard input (stdin ).
For example, the following code indicates that, if it is not the end of the file, the content of the file will be copied to the screen.
int c;
while ((c = fgetc(fp)) != EOF) {
putchar (c);
}
Naturally, I thought that at the end of each file, there was a special character called EOF. After reading this character, the operating system thought the file was over.
However, I later 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 am confused.
If EOF is a special character, it can be assumed that each text file ends with an EOF (that is,-1), because the ASCII code corresponding to the text is positive, A negative value is not allowed. But what about binary files? How can I handle the-1 contained in the file?
I thought about this question for a long time, and I found out the information later,In Linux, EOF is not a single character, but a signal value (-1) returned when the system reads the end of the file ).As for how the system knows the end of the file, the document compares 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 () not only returns EOF at the end of a file, but also returns EOF when an error occurs. Therefore, the C language provides the feof () function to ensure that the end of the file is indeed reached. The above code feof () is written as follows:
int c;
while (!feof(fp)) {
c = fgetc(fp);
do something;
}
However, there is also a problem with this writing. After fgetc () reads the last character of the file, the feof () function of C language returns 0, indicating that it does not reach the end of the file; only when fgetc () if you read another character (that is, beyond the last character), feof () returns a non-zero value, indicating that the end of the file is reached.
Therefore, according to the above statement, if a file contains n characters, the internal operation of the while loop will run n + 1 times. Therefore, the safest syntax 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 the end of the file, EOF can also represent the end of the standard input.
int c;
while ((c = getchar()) != EOF) {
putchar(c);
}
However, the standard input is different from the file, and the length of the input cannot be known in advance. You must manually enter a character to indicate that the input reaches the EOF.
In Linux, press Ctrl-D at the beginning of a new line to indicate EOF (if Ctrl-D is pressed in the middle of a line, it indicates that the cache area of "standard input" is output, therefore, press Ctrl-D twice. In Windows, Ctrl-Z indicates EOF. (By The Way, in Linux, pressing Ctrl-Z indicates that the process is interrupted and suspended in the background. You can use the fg command to switch back to the foreground; press Ctrl-C to terminate the process .)
So what if I really want to enter Ctrl-D? In this case, you must press Ctrl-V first, and then you can enter Ctrl-D, the system will not think this is an EOF signal. Ctrl-V indicates to interpret the next input by "literal meaning". If you want to press "literal meaning", enter Ctrl-V and enter it twice in a row.
(End)