UESTC 59 数据大搜索

数据大搜索

Time Limit: 1000 MS     Memory Limit: 64 MB

Submit Status

写一个程序,把一个字符串中的数字子序列找出来并转换成十进制整数输出。

Input

第一行是整数nn,表示测试的数据组数,下面是nn行无空格的字符串(长度不超过200)。题目保证字符串中含的整数位数小于99位,不考虑负数情形。

Output

每一行输入对应一行输出,一行中若有多个数据,每个数据后有一个空格。

Sample input and output

Sample Input Sample Output
1
00tUrA-Hc:T#7yN`;J0123Y+'-rD%\SV`{)e'9;Lt[^$}~0
0 7 123 9 0

Source

wxiaoping C语言上机实验


UESTC Online Judge

Copyright (C) 2012 - 2018 Ruins He(@ruinshe), Jianjin Fan(@pfctgeorge) and Yun Li(@mzry1992). Project home

Any Problem, Please Report On Issues Page.

挺简单的。

直接上代码

#include <iostream>
#include <cstring>

using namespace std;

int main(){
    int t;
    char s[210], c[210];
    cin >> t;
    while(t--){
        memset(c, '\0', sizeof(c));
        memset(s, '\0', sizeof(s));
        cin >> s;
        int l, t, f;
        l = strlen(s);
        t = 0;
        f = 0;
        for(int i = 0; i < l + 1; i++){
            if(s[i] >= '0' && s[i] <= '9'){
                c[t++] = s[i];
                f = 1;
            }
            else{
                if(f){
                    int j;
                    for(j = 0; j < t; j++){
                        if(c[j] != '0')   break;
                    }
                    if(j >= t) cout << "0";
                    else{
                        for(; j < t; j++)   cout << c[j] - '0';
                    }
                    cout << " ";
                    memset(c, '\0', sizeof(c));
                    t = 0;
                    f = 0;
                }
            }
        }
        cout << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zx__zh/article/details/82024491
59