zoukankan      html  css  js  c++  java
  • ZOJ 2851 Code Formatter(简单字符统计)

    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

    2

    include <stdio.h>

     

    int main()

    {

            int a,b;

            while(scanf("%d %d",&a, &b) != EOF)

                   printf("%d\n",a+b);            

           

    }

    ##

     

    ##

    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.


    Author: YANG, Chao
    Source: Zhejiang Provincial Programming Contest 2007

    解题报告:这道题就是统计空格,和tabs的个数,其中一个tabs相当于4个空格,

    代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int MAX = 1010;
    char str[MAX];
    int main()
    {
    int n, len, i, tab, trail, j, ans;
    scanf("%d", &n);
    getchar();//度掉回车
    for (i = 0; i < n; ++i)
    {
    tab = 0;
    ans = 0;
    trail = 0;
    while (gets(str))
    {
    if (strcmp(str, "##") == 0)//遇见##表明一个样例的结束
    {
    break;
    }
    trail = 0;
    len = strlen(str);
    for (j = 0; j < len; ++j)
    {
    if (str[j] == '\t')
    {
    tab ++;
    trail += 4;
    continue;
    }
    else if (str[j] == ' ')
    {
    trail ++;
    continue;
    }
    trail = 0;
    }
    ans += trail;
    }
    printf("%d tab(s) replaced\n", tab);
    printf("%d trailing space(s) removed\n", ans);
    }
    return 0;
    }



  • 相关阅读:
    眼睛的颜色 博弈
    codevs1281 矩阵乘法 快速幂 !!!手写乘法取模!!! 练习struct的构造函数和成员函数
    10 25日考试 数学题目练习 斐波拉契 打表
    线段树 模板
    榨取kkksc03 luogu1855 dp 裸二维费用背包
    低价购买 洛谷1108 codevs4748 dp
    [转] 经典排序算法
    [USACO08DEC] Trick or Treat on the Farm
    [NOIP2009] 靶形数独(搜索+剪枝)
    各种蒟蒻模板【如此简单】
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2429026.html
Copyright © 2011-2022 走看看