The number of rectangles is in a 3*2 rectangle. You can find 6 1*1 rectangles, 4 2*1 rectangles, and 3 1*2 rectangles, two 2*2 rectangles, two 3*1 Rectangles and one 3*2 rectangle, 18 rectangles in total. Give A, B, and calculate the number of rectangular inputs that can be found: This question contains multiple sets of input data (<10000). You must input two integers A until the EOF, B (1 <= A, B <= 1000) Output: Number of rectangles found in the output. Example: input: 1 2 3 2 output: 3 18 original code-1: Copy Code # include <stdio. h> int rectangle (int x, int y) {int I, j, sum, temp; for (I = 1, sum = 0, temp = 0; I <= x; I ++) {for (j = 1; j <= y; j ++) {temp = (x-I + 1) * (y-j + 1 ); sum + = temp ;}} return sum;} int main () {long int A [1000], B [1000], I; int rectangle (int x, int y ); // printf ("input A & B and end with 0 \ n"); for (I = 0; I <1000; I ++) {scanf ("% d ", & A [I]); if (A [I] = 0) {break;} scanf ("% d", & B [I]);} For (I = 0; A [I]! = 0; I ++) {printf ("% d \ n", rectangle (A [I], B [I]);} return 0;} copy code evaluation: after writing it, the child admitted frankly that dewdrop did not know how to end with EOF .. I had to use 0... which one can give you some advice .. I told him that there is no need to use the array while (scanf ("% d", & A, & B )! = EOF) {// calculate the number of output rectangles} So he quickly gave the new Code: original code-2: Copy Code # include <stdio. h> int rectangle (int x, int y) {int I, j, sum, temp; for (I = 1, sum = 0, temp = 0; I <= x; I ++) {for (j = 1; j <= y; j ++) {temp = (x-I + 1) * (y-j + 1 ); sum + = temp ;}} return sum;} int main () {long int A, B; int rectangle (int x, int y ); printf ("input A & B and end with EOF \ n"); while (scanf ("% d", & A, & B )! = EOF) {printf ("% d \ n", rectangle (A, B);} return 0;} copy the code and comment: this time is much better. However, he has a new confusion: After I modify it to this, for example, I enter 2 3 to jump out of 18 first, and enter eof to jump to 18 until it stops... in short, it is the result of the last hop .. it seems that he does not know what the EOF is. He thought he typed the eof three characters on the keyboard. I told him: EOF is not a three-character but a symbolic constant. If you use the WIN system to enter Control-Z at the beginning of the line, try to use EOF in stdio. A macro defined in h is usually like this # define EOF (-1), but the C language does not say that EOF must be-1. This EOF does not exist on the keyboard, but it encounters special characters in the input stream. The return value of the scanf () function can be EOF. So what does this sentence mean? Generally, the return value of scanf () is a non-negative integer. For example, int I; scanf ("% d", & I); if you type 123 on the keyboard (1, 2, 3 match % d ), the value of scanf ("% d", & I) is 1, because a value is assigned to one variable; if you type abc on the keyboard (a does not match % d), the value of scanf ("% d", & I) is 0, because scanf cannot regard "a" as a decimal INTEGER (% d), nor can it be converted, nor can it assign a value to variable I, that is, scanf does not assign a value to any variable in this case, therefore, the return value is 0. If scanf () encounters a special character in the input stream (which character is related to the Environment), the return value is EOF. In addition, I told him to declare the function type int rectangle (int x, int y). It is silly to write it in the main () function (the function type should be declared) the value of the temp value that is written outside the function is zero (because it can be directly) sum + = (x-I + 1) * (y-j + 1, he made another modification: original code-3 copy Code # include <stdio. h> int rectangle (int x, int y) {int I, j, sum, temp; for (I = 1, sum = 0; I <= x; I ++) {for (j = 1; j <= y; j ++) {sum + = (x-I + 1) * (y-j + 1 );;}} return sum;} int rectangle (int x, int y); int main () {long int A, B; printf ("input A & B and end with EOF \ n "); While (scanf ("% d", & A, & B )! = EOF) {printf ("% d \ n", rectangle (A, B);} return 0;} copy the code and evaluate it: there are fewer problems, but there are still some. First, the location of the rectangle () function definition is incorrect. It is better to write it after the definition of main. Second, there is a problem with the data type, which is a serious problem. Long int A, B; there is absolutely no need to define A and B as the long type. The int type is sufficient. Because A and B are not defined as the long type, scanf ("% d", & A, & B) and rectangle (A, B) in the code) both calls are incorrect. Although no error results have been produced (it is estimated that the long and int types are the same in that system), it is actually just a coincidence that the blind cat has a dead mouse. The other two flaws are that the temp variable in the rectangle () function definition has been forgotten to be deleted, and the statement to be deleted in the loop body is not clean, and ";" also forgets to be deleted. Then sum + = (x-I + 1) * (y-j + 1). I don't understand this algorithm, and I don't know if it is correct. Refactoring: in fact, the original code has been changed almost, with only a few minor errors and flaws left. Here I will only talk about my algorithm: My algorithm is to list all the combinations of two vertices (P1, P2), as long as P1 can be the upper left corner of a rectangle, p2 can be the lower right corner of a rectangle (P1_X <P2_X, P1_Y <P2_Y. Copy code 1/* 2 Number of rectangles 3 in a 3*2 rectangle, you can find 6 1*1 rectangles, 4 2*1 rectangle 3 1*2 rectangle, 4 2 2*2 rectangle, 2 3*1 rectangle and 1 3*2 rectangle, A total of 18 rectangles. 5. Give A and B, and calculate the number of rectangles that can be found. 6 7 Input: 8 this question contains multiple groups of input data (<10000). You must process the EOF until 9 and input two integers A, B (1 <= A, B <= 1000) 10 11 output: 12 output the number of rectangles found. 13 14 Example: 15 16 input: 17 1 2 18 3 2 19 20 output: 21 3 22 1823 24 Author: Xue fe25 Source: http://www.cnblogs.com/pmer/ "Common Errors and flaws in C Language beginner code" series blog 26 27 */28 29 # include <stdio. h> 30 31 int count (int, int); 32 33 int main (void) 34 {35 int A, B; 36 37 while (printf ("enter two integers, B (1 <= A, B <= 1000) "), 38 scanf (" % d ", & A, & B )! = EOF 39) 40 {41 printf ("% d \ n", count (A, B); 42} 43 44 return 0; 45} 46 47 int count (int A, int B) 48 {49 int x1, y1; // The coordinates of the first vertex are 50 int x2, y2; // coordinates of the second vertex: 51 int num = 0; 52 53 for (x1 = 0; x1 <= B; x1 ++) 54 for (y1 = 0; y1 <= A; y1 ++) // various possibilities of the first point of exhaustion: 55 for (x2 = 0; x2 <= B; x2 ++) 56 for (y2 = 0; y2 <= A; y2 ++) // list of various possibilities for the second vertex: 57 {58 if (x1 <x2 & y1 <y2) 59 num ++; 60} 61 62 return num; 63}