414 - Machined Surfaces

来源:互联网 发布:淘宝推荐返利 编辑:程序博客网 时间:2024/05/15 03:57


 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 contiguousX'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 contiguousX's.

Digital-Image View of Surfaces

  Left                                Right

XXXX XXXXX tex2html_wrap_inline50

XXX XXXXXXX

XXXXX XXXX

XX XXXXXX

. .

. .

. .

XXXX XXXX

XXX XXXXXX tex2html_wrap_inline52

tex2html_wrap_inline54 tex2html_wrap_inline56

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 blankregion 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.

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 ofN will be the first character on a line.

Next N lines -
Each line has exactly 25 characters; one or more X's, then zero or more spaces, then one or moreX'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 ease of reading.The actual input file will use the ASCII-space character, not"B").

4XXXXBBBBBBBBBBBBBBBBXXXXXXXXBBBBBBBBBBBBBBBXXXXXXXXXXXXBBBBBBBBBBBBBBBBXXXXXXBBBBBBBBBBBBBBBBBXXXXXX2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXXXXXXXBBBBBBBBBBBBBBXX0

Sample Output

400



这提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){#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;}

也可将a数组的长度设为26,但必须在fgets调用之后加上getchar()。此处设置27是为了每串后面会有一个回车和一个'\0'。

原创粉丝点击