정수 파싱
정수 파싱
Convert a decimal string such as "-123" to an int by hand. The task supplies valid input whose value fits in int.
비양성(non-positive) 값을 한 자리씩 구성하십시오: result = result * 10 - digit. "123"의 경우 0 → -1 → -12 → -123를 추적하고, 입력이 양수였으므로 마지막에 부호를 반전시킵니다. 한 자리 문자는 c - '0'로 숫자로 변환됩니다.
이는 음수 극한에서 중요합니다. 사이트의 32-비트 int에서 INT_MIN는 -2147483648이지만, 2147483648는 Fits하지 않습니다. 먼저 양의 크기를 구성하면 부호가 있는 오버플로우(signed overflow)가 발생합니다. 중간 값을 음수로 유지하면 주어진 전제 조건 하에서 두 극한을 모두 처리할 수 있습니다.
이 과제는 함수입니다: 정답 확인을 사용하여 체크기의 다른 문자열로 실행하십시오. 체크기는 main를 제공합니다.
난이도 5/5 · 400코인 + 오늘 첫 완료 시 작은 보너스 추가
Complete int parse_int(const char *s) without conversion functions such as atoi. Input is a non-null, null-terminated string of one or more decimal digits with an optional leading -; its value fits in int, including INT_MIN 및 INT_MAX. No spaces, leading + or invalid input need handling.
출력을 보려면 '실행'을 클릭하세요.