Machined Surfaces |
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 an "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 an "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 region in 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.
The input consists of a series of digital images. Each image data set hasthe following format:
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.
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.
4 XXXXBBBBBBBBBBBBBBBBXXXXX XXXBBBBBBBBBBBBBBBXXXXXXX XXXXXBBBBBBBBBBBBBBBBXXXX XXBBBBBBBBBBBBBBBBBXXXXXX 2 XXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXX 1 XXXXXXXXXBBBBBBBBBBBBBBXX 0
4 0 0
这提WA了3次,主要错误有:
1. 在接收了n之后没有使用getchar()来吃掉一个回车
2. 计算的方法错误
3. 对fgets函数不熟悉
fgets函数用来从文件中读入字符串。fgets函数的调用形式如下:fgets(str,n,fp);此处,fp是文件指针;str是存放在字符串的起始地址;n是一个int类型变量。函数的功能是从fp所指文件中读入n-1个字符放入str为起始地址的空间内;如果在未读满n-1个字符之时,已读到一个换行符或一个EOF(文件结束标志),则结束本次读操作,读入的字符串中最后包含读到的换行符。因此,确切地说,调用fgets函数时,最多只能读入n-1个字符。读入结束后,系统将自动在最后加'\0',并以str作为函数值返回。
AC代码如下:
#include <stdio.h> #include <string.h> int main(void) { #ifdef TEST freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); #endif int 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; }