(가장빠른입출력방법)알고리즘 문제풀다가 다른분께서 올려놓은코드 이해를 못하겠습니다.

uiucpass의 이미지

inline static int read() {
if (!(idx - bytes)) {
bytes = fread(buf, sizeof(char), sizeof(buf), stdin);
idx = 0;
}
return buf[idx++];
}
inline static int parse() {
int ret = 0,
num = read();

while (num - 0x0A && num - 0x20) {
ret = ret*10 + (num & 0x0F);
num = read();
}
return ret;
}

https://www.acmicpc.net/source/3904066

에 있는 코드이고요

다른 빠른 분들도 저렇게 비슷하게 입출력을 받으시던데

코드분석이 잘 안됩니다.
ret = ret*10 + (num & 0x0F); 은 11을 입력했다고 치면 문자열 11을 숫자 11로 만들어주는 역할같은데
어떻게 이렇게 되는지 잘 모르겠습니다. 그리고 이렇게 입력을 받고, 제가 gcc로 컴파일해서 출력해보면 아무것도 나오지않는데, 백준가서 제출하면 통과가 되더군요;;

고수님들 제발 어떻게 저렇게 동작하는지 알려주시면 정말 감사하겠습니다. ㅜㅜ

p.s 그리고 왜 cin 과 scanf 그리고 fread의 속도가 차이가 이렇게 많이나는지 궁금합니다.
cin의 경우는 stdio를 sync해줘서 느리다고 하는데 이 말이 잘 이해가 가지않습니다.

https1234의 이미지

저 주소로 들어가보니까 404 ??????????? 만 뜨는데 저만 그런가요?

raymundo의 이미지

저도 저 링크는 404가 뜨는군요. 아마 문제를 푼 사람에게만 보이나 싶은데,

다른 건 몰라도 문자열 "11"을 숫자 11로 만들어주는 코드야 아스키코드를 보시면
'0'부터 '9'까지의 아스키코드가 0x30~0x39 니까 0x0F와 AND연산 하면 앞의 3은 날아가고 뒤의 0~9만 남죠.
다음 글자를 읽을 때 현재까지의 결과에 10을 곱해서 자릿수를 올려주는 거고요.

좋은 하루 되세요!

세벌의 이미지

링크 들어가 보니 로그인 화면이 뜨네요.

KLDP에서 소스 코드 올릴 때는 code 태그 쓰세요.
https://kldp.org/node/158191

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.