ZOJ-2851-Code Formatter【4th浙江省赛】

ZOJ-2851-Code Formatter

                    Time Limit: 2 Seconds      Memory Limit: 65536 KB

Some companies have special requirements for source code format, and it is also good for programmers to keep consistent code style. You are asked to write a simple code formatter for the company to help the poor programmers.

The first thing you need to do is to check whether the source code contains tabs (represented as the escape character ‘\t’), since different terminals have different ways to display tabs, it’s better not to use them, but replace them with spaces. The code formatter should replace each tab of the source code with 4(four) blank spaces.

Then you need to remove trailing spaces of the source file. Trailing spaces are one or more consecutive whitespaces right before the EOL (end of line, represented as the escape character ‘\n’), and they usually have no meaning in most programming language, so they can be safely removed.

Input
The input contains multiple test cases!

The first line is an integer N indicating the number of test cases. Each test case is the source which contains not more than 100 lines given to you to format. A single line containing only “##” marks the end of a test case.

Output
For each test case, output a log of the formatter in two lines of the following format:

A tab(s) replaced B trailing space(s) removed Where A is the number of tabs replaced and B is the number of trailing spaces removed.

Sample Input

Sample Output

4 tab(s) replaced
22 trailing space(s) removed
0 tab(s) replaced
0 trailing space(s) removed

Note
In order to show the whitespaces precisely, all the characters in sample input are underlined. They are not the underscore character.

题目链接:ZOJ-2851

题目大意:
1. 求‘\t’的数目
2. 求末尾(最后一个非空白字符)开始之后有多少个空白。’ ‘一个空格算一个,‘\t’算四个

题目思路:这道题目比较坑的是题目意思,理解了就好做。

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;

int main(){
    int n;
    cin >> n;
    while(n--)
    {
        int cnt_x = 0,cnt_t = 0; 
        string s;
        while(getline(cin,s))
        {
            if (s == "##") break;
            for (int i = 0; i < s.size(); i++)
            {
                if (s[i] == '\t') cnt_t++;
            }
            for (int i = s.size() - 1; i >= 0; i--)
            {
                if (s[i] == '\t') cnt_x += 4;
                else if (s[i] == ' ') cnt_x++;
                else break;
            }
        }
        printf("%d tab(s) replaced\n",cnt_t);
        printf("%d trailing space(s) removed\n",cnt_x);
    } 
    return 0;
}

你可能感兴趣的:(ZOJ,2851,Code-Forma)