B - 数据大搜索 (UESTC - 59 )

版权声明:版权所有--转载的小伙伴请告知博主并注明来源哦~ https://blog.csdn.net/u011145745/article/details/82048670

题目连接: B - 数据大搜索

来源: UESTC - 59 

Problem Description

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

Input

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

Output

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

Sample Input

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

Sample Output

0 7 123 9 0

Hint:题目也不算难,仔细考虑一下可以做出来的(我的思路可能比其他小伙伴的有些不同@.@,有机会把小伙伴的也放过来)

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int n, i, j, t, l, b[200];
    scanf("%d", &n);
    while(n--)
    {
        char a[220];
        scanf("%s", a);
        l = strlen(a);
        t = 0;
        for(i = 0; i <= l; i++)  // i<=l的话可能会出错,所以去掉=,加上最后注释掉的部分就OK了
        {
            if(a[i] >= '0' && a[i] <= '9')
            {
                b[t++] = a[i] - '0';
            }
            else
            {
                if(t)
                {
                    int num = 0, x = 1;
                    for(j = t - 1; j >= 0; j--)
                    {
                        num += b[j] * x;
                        x *= 10;
                    }
                    printf("%d ", num);
                    t = 0;
                }
            }
        }
//        if(a[l - 1] >= '0' && a[l - 1] <= '9')
//        {
//            int num = 0, x = 1;
//            for(j = t - 1; j >= 0; j--)
//            {
//                num += b[j] * x;
//                x *= 10;
//            }
//            printf("%d ", num);
//        }
//        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u011145745/article/details/82048670
59