An imaging device furnishes digital images of two machined surfaces thateventually will be assembled incontact with each other. The roughness of this final contact is to be estimated.
A digital image is composed of the two characters,"X"And""(Space). There are always 25 columns to animage, but the number of rows,
N, Is variable. Column one (1) will alwayshave"X"In it and will bepart of the left surface. The left surface can extend to the right fromcolumn one (1) as contiguous
X'S.
Similarly, column 25 will always have"X"In it and will be partof the right surface. The right surfacecan extend to the left from column 25 as contiguous
X'S.
Digital-Image View of Surfaces
Left Right XXXX XXXXX
XXX XXXXXXX
XXXXX XXXX
XX XXXXXX
. .
. .
. .
XXXX XXXX
XXX XXXXXX
1 25
In each row of the image, there can be zero or more space charactersseparating the left surface from the right surface. There will never be more than a single blank
RegionIn any row.
For each image given, you are to determine the total ''void "that willexist after the left surface has beenbrought into contact with the right surface. the ''void "is the totalcount of the spaces that remains betweenthe left and right surfaces after theyhave
Been brought into contact.
The two surfaces are brought into contact by displacing them strictlyhorizontally towards each other until arightmost
"X"Of the left surface of some row is immediately to theleft of the leftmost
"X"Of the right surface of that row. There is no rotation or twisting of these two surfaces as they are broughtinto contact; they remain rigid, and only move horizontally.
Note:The original image may show the two surfaces already in contact, in which case no displacement enters into the contact roughness estimation.
Input
The input consists of a series of digital images. Each image data set hasthe following format:
-
First line-
-
A single unsigned integer,
N, With value greater thanzero (0) and less than 13. Thefirst digit
NWill be the first character on a line.
-
NextNLines-
-
Each line has exactly 25 characters; one or more X'S, then zero or more spaces, then one or more
X'S.
The end of data is signaled by a NULL data set having a zero on the firstline of an image data set and no further data.
Output
For each image you receive as a data set, you are to reply with the totalvoid (count of spaces remainingafter the surfaces are brought into contact ). use the default output fora single integer on a line.
Sample Input (character
"B"For reading of reading. The actual input file will use the ASCII-space character, not
"B").
4XXXXBBBBBBBBBBBBBBBBXXXXXXXXBBBBBBBBBBBBBBBXXXXXXXXXXXXBBBBBBBBBBBBBBBBXXXXXXBBBBBBBBBBBBBBBBBXXXXXX2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXXXXXXXBBBBBBBBBBBBBBXX0
Sample Output
400
This was mentioned three times, with the following major errors:
1. After receiving N, the getchar () is not used to eat a carriage return.
2. The calculation method is incorrect.
3. unfamiliar with the fgets Function
The fgets function is used to read strings from files. The fgets function is called in the following format: fgets (str, n, fp). In this example, fp is the file pointer, str is the starting address of the string, and n is an int type variable. The function is to read n-1 characters from the file indicated by fp into the space where str is the starting address. If it is not fully read by n-1 characters, if you have read a linefeed or an EOF, the read operation ends. The string that you read contains the linefeed that you finally read. Therefore, when calling the fgets function, you can only read a maximum of N-1 characters. After reading, the system automatically adds '\ 0' at the end and returns the result using str as the function value.
The AC code is as follows:
#include <stdio.h>#include <string.h>int main(void){#ifdefTESTfreopen("data.in", "r", stdin);freopen("data.out", "w", stdout);#endifint i, j;int n;char a[27];int min, sum, t[14];while (scanf("%d", &n) != EOF && n) {getchar();sum = 0;min = 30;memset(t, 0, sizeof(t));for (i=0; i<n; ++i) {fgets(a, 27, stdin);for (j=0; j<25; ++j)if (a[j] == ' ')++t[i];if (t[i] < min)min = t[i];sum += t[i];}printf("%d\n", sum-n*min);}return 0;}
You can also set the length of array a to 26, but you must add getchar () after the fgets call (). Set 27 to a carriage return and a '\ 0' at the end of each string '.