Codeforces Educational Codeforces Round 2 A. Extract Numbers

题目链接

题意:

给你一串字符串 s;

‘,’ 和';'作为分隔符,其他被视作word,word可以为空;

将这些words分离出来,整数(不包括前导0和浮点数)保存到 string a, 其余保存到 string b;

输出 string a, string b,若没有内容,输出'-'

思路:

将所有‘,’和';'换成空格,用一个string  t从前往后读入

遇到空格

①t.size()==0 即有空word

②t[0] == 0 && t.size() > 1 即前导0

③用flag标记是否为整数word

代码如下:

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
const int N = 1E5 + 10;
string s, a, b, t;

int main()
{
	cin >> s;
    for(int i = 0; i < s.size(); ++i)
        if (s[i] == ',' ||  s[i] == ';') s[i] = ' ';
    bool flag = 0;
    s += " ";
    t = a = b = "";
    for (int i = 0; i < s.size(); ++i)
    {
        if (s[i] == ' ')
        {
            if (flag == 1 || (t[0] == '0' && t.size() > 1) || t.size() == 0)
                b += "," + t; 
	        else a += "," + t;
            flag = 0;
            t = "";
            continue;
        }
        if (s[i] < '0' || s[i] > '9') flag = 1;
        t += s[i];
    }
    if (a != "")
    {
        a.erase(0, 1);
        cout << '"' << a << '"' << "\n";
    } 
	else cout << "-\n";
    if (b != "")
    {
        b.erase(0, 1);
        cout << '"' << b << '"' << "\n";
    } 
	else cout << "-\n";
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值