入:AJJJH FJ 378 ,DH DSAUH 1507
出:375+1507=1882
分析:
扫描字符串,发现数字标记数字起始位置。当数字变回非数字字符时,提取数字并将其转换为数字,累加数字求和。
C代码:
#include < stdio.h >
#include < stdlib.h >
#include < math.h >
#include < string.h >
#define MAX 256
int main(int argc, char **argv)
{
char buffer[MAX], ch;
int i = 0, len;
int start_flg = 0, start_pos = 0, sum = 0;
memset(buffer, '\0', sizeof(char) * MAX);
ch = getchar();
while (ch != 0x0A) {
buffer[i] = ch;
ch = getchar();
i++;
}
len = strlen(buffer);
for (i = 0; i <= len; i++) {
if (!start_flg && buffer[i] >= '0' && buffer[i] <= '9') {
start_pos = i;
start_flg = 1;
} else if (start_flg && (buffer[i] < '0' || buffer[i] > '9')) {
buffer[i] = '\0';
int tmp = atoi(buffer + start_pos);
sum += tmp;
printf("%d\n", tmp);
start_flg = 0;
}
}
printf("%d\n", sum);
return 0;
}
没有评论:
发表评论