414 - Machined Surfaces

注: 以下提到的 B 都是代指空格.

题意:
题目本身不难, 但是本人英语实在太差, 读了半天愣是没能理解题意, 在网上看了解释后, 题目就很快解出来了.把输入的 N 行 X B 组合中的连续 X 进行平移,  从而压缩B的存在(从左向右平移, 每一行与下一行是当成连通的一行进行处理的), 当其中一个连续 B 被压缩完毕时, 问这 N 行 X B 组合中还剩下多少个 B ?

思路:
取出每行 B 中最小的个数, 把其他行的 B 的个数减去这个最小的 B 得到的差的和即为所求. 注意两点:
1. cin 不会读取换行符, getline 会读取换行符, 所以 cin 之后, 需要再显式 cin.get() 把换行符读掉, 否则 getline 会少读取一行.
2. isspace() 判断是否为空格.

题目:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=show_problem&problem=355

代码:

# include <iostream>
# include <string>
# include <cctype>
# include <vector>
using namespace std;

int main(int argc, char const *argv[])
{
  int lineNum = 0;
  
  cin >> lineNum;
  cin.ignore();

  string str;
  vector<int> spaceCounts;

  while (lineNum > 0) {
    int minNum = 25;
    spaceCounts.clear();

    for (int i=0; i<lineNum; i++) {
      getline(cin, str);

      int spaceCount = 0;
      for (int j=0; j<str.length(); j++) {
        if (isspace(str[j])) ++spaceCount;
      }

      spaceCounts.push_back(spaceCount);
      minNum = min(minNum, spaceCount);
    }

    int remainSpace = 0;
    for (int i=0; i<spaceCounts.size(); i++) {
      remainSpace += (spaceCounts[i] - minNum);
    }

    cout << remainSpace << endl;

    cin >> lineNum;
    cin.ignore();
  }

  return 0;
}

环境:C++ 4.5.3 - GNU C++ Compiler with options: -lm -lcrypt -O2 -pipe -DONLINE_JUDGE

你可能感兴趣的:(uva,Surfaces,414,Machined)