3438 - 简单的问题A

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

小明有一堆字符串,字符串由 数字 + 子字符串 + 数字 + 子字符串组成(子字符串不包含数字),例如1abc12def;

0<=数字<=100;小明想知道那一堆字符串中数字和为多少?

题目输入

先输入T(T<= 10)有T组数据

输入N(N<= 10)有N个字符串,然后输入N个字符串,每个字符串不超过10位。

题目输出

输出数字总和,每组数据一行。

输入/输出样例

输入格式

1
2
1abc23dd 4s..1'

输出格式

29

C语言解答

#include<stdio.h>
#include<string.h>
void main()
{
	int sum,s,i,j,k;
	char ch[80];
	scanf("%d",&i);
	for(i;i>0;i--)
	{
		scanf("%d",&j);
		sum=0;
		for(j;j>0;j--)
		{
			scanf("%s",ch);
			k=s=0;
			for(;ch[k];k++)
			{
				if(ch[k]>='0'&&ch[k]<='9')
					s=s*10+ch[k]-'0';
				else
				{
					sum+=s;s=0;
				}
			}
			sum+=s;
		}
		printf("%d\n",sum);
	}
}

C++解答

#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
	int re, n, x, y;
	char a[111];
	cin >> re;
	while(re--)
	{
		cin >> n;
		int sum = 0;
		for(int i = 0 ; i < n ; i ++)
		{
			scanf("%s",a);
			int s = 0;
			for(int j = 0 ; a[j] ; j ++)
			{
				if(a[j] >= '0' && a[j] <= '9')
					s = s * 10 + (a[j] - '0');
				else
				{
					sum += s;
					s = 0;
				}
			}
		}
		cout << sum << endl;
	}
}