【rlz000】字串找数

Time Limit: 3 second
Memory Limit: 2 MB

问题描述

输入一个字符串,内有数字和非数字字符。如A123X456Y7A,302ATB567BC,统计共有多少个整数, 并输出字符串中所有连续(指不含非数字字符)的数字所组成的整数。如果没找到输出0,最大整数不会超过LongInt的范围。

Sample Input

A123X456Y7A
Sample Output

3:123,456,7(换行)
Sample Input

kjdSDASDsadasd
Sample Output

0

【题目链接】:http://noi.qz5z.com/viewtask.asp?id=rlz00

【题解】

字符串处理题;
找到第一个为数字的地方;然后用个while嵌套一下就能搞了;
用子串函数搞搞.
vector很方便;

【完整代码】

#include <cstdio>
#include <string>
#include <vector>
#include <iostream>

using namespace std;

string s;
vector <string> a;

int main()
{
    //freopen("F:\rush.txt","r",stdin);
    cin >> s;
    int len = s.size();
    for (int i = 0;i <= len-1;i++)
        if (isdigit(s[i]))
        {
            int j = i+1;
            while (j<=len-1 && isdigit(s[j]))
                j++;
            a.push_back(s.substr(i,(j-i)));
            i = j;
        }
    len = a.size();
    printf("%d",len);
    if (len>0)
        putchar(':');
    for (int i = 0;i <= len-2;i++)
        cout << a[i] << ",";
    if (len > 0)
        cout << a[len-1]<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7632045.html