CF Sequence Formatting

题意:Polycarp wants to add and remove spaces in the string s to ensure the following:

each comma is followed by exactly one space (if the comma is the last character in the string, this rule does not apply to it),
each “three dots” term is preceded by exactly one space (if the dots are at the beginning of the string, this rule does not apply to the term),
if two consecutive numbers were separated by spaces only (one or more), then exactly one of them should be left,
there should not be other spaces.

input
1,2 ,3,…, 10
output
1, 2, 3, …, 10
input
1,,,4…5……6
output
1, , , 4 …5 … …6
input
…,1,2,3,…
output
…, 1, 2, 3, …

分类需要输出空格的情况
’ , ’ 后面需要一个空格
‘……’ 六个’ . ’ 之间需要一个空格
’ , ’ ’ , ’ 之间需要空格
首位0不能输出
通过一次一次的更新flag的数值来确定字符前面的情况

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
const int N=300,INF=0x3f3f3f3f;
char s[N];
int main()
{
    gets(s);
    int flag=0;
    for(int i=0;s[i];i++)
    {
        if(s[i]=='.')
        {
            if(flag!=0) printf(" ");
            printf("...");
            i+=2;
            flag=1;
        }
        else if(s[i]>='0'&&s[i]<='9')
        {
            if(flag!=0&&flag!=1) printf(" ");
            if(s[i]=='0'&&(s[i+1]>='0'&&s[i+1]<='9')) continue;
            for(;s[i]>='0'&&s[i]<='9';i++)//输出数字
                printf("%c",s[i]);
            i--;
            flag=2;
        }
        else if(s[i]==',')
        {
            if(flag==3) printf(" ");
            printf("%c",s[i]);
            flag=3;
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值