C프로그램으로 알고리즘 공부를 하고 있는 학생입니다.링크텍스트
이번 문제는 간단한 입출력 문제입니다.
이것을 보고 제가 판단 내린 것은 숫자 사이에 문자(,)를 처리하기 위해서 입력 전체를 string으로 받아서 정수에 해당되는 값만 더하는 방식으로 문제를 해결했습니다.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
int N;
char str[10];
int str_len;
int total;
scanf("%d", &N); // 테스트 케이스 개수
for(int i = 0; i < N; i++) {
total = 0; // 답을 출력할 변수
scanf("%s", str);
getchar(); // \n 문자 받아들이기
str_len = strlen(str);
for(int j = 0; j < str_len; j++)
if(isdigit(str[j])) // 문자가 정수라면
total += (str[j] - '0');
printf("%d\n", total);
}
return 0;
}
문제를 해결하고 다른 분들의 소스코드를 보니 더 간단하게 해결하는 방법이 있었습니다.
#include <stdio.h>
int main() {
int t, a, b;
scanf("%d", &t);
while(t--) {
scanf("%d,%d", &a, &b);
printf("%d\n", a + b);
}
}