2014-03-29 94 views
0

我正在嘗試解決一個codechef練習問題subtraction game 1與c編程和代碼塊作爲IDE。我發現一種閱讀輸入比scanf()函數更快的方式,但是當我運行我的程序時,我收到錯誤「未定義引用'getchar_unlocked'錯誤」。你們能告訴我我做錯了什麼嗎?還有其他方法可以更快地讀取輸入嗎?對'getchar_unlocked'錯誤的未定義引用

#include<stdio.h> 
inline int fastread() 
{ 
     int noRead=0; 
     char p=getchar_unlocked(); 
     for(; p<33;) { 
       p=getchar_unlocked(); 
     }; 
     while(p>32) { 
       noRead = (noRead << 3) + (noRead << 1) + (p - '0'); 
       p=getchar_unlocked(); 
     } 
     return noRead; 
}; 
unsigned int gcd(unsigned int a, unsigned int b) 
{ 
    if (b == 0) 
     return a; 
    else 
     return gcd(b, a % b); 
} 
int main() 
{ 
    int t,i,answer=0; 
    unsigned int n; 
    t = fastread(); 
    while(t--) 
    { 
     n = fastread(); 
     unsigned int a[n]; 
     for(i=0;i<n;i++) 
      a[i]=fastread(); 
     answer = gcd(a[0],a[1]); 
     for(i=2;i<n;i++) 
      answer = gcd(a[i],answer); 
     printf("%u\n",answer); 
    } 
    return 0; 
} 
+0

我認爲'getchar_unlocked'在Windows中被棄用,因爲它是線程不安全的'getchar()'.Sp的版本,可能它在代碼塊庫中不可用。除非速度因素太多,否則請儘量避免getchar_unlocked。 –

+0

可能有人應該問MS爲什麼他們不支持POSIX? – mfro

+0

@Jayesh這意味着我不能使用getchar_unlocked。你能否提出其他更快的閱讀輸入方式? –

回答

2

報價this答案在SO

getchar_unlocked在Windows中被推薦使用,因爲它是線程不安全 版本的getchar的()。

getchar_unlocked具有較少的開銷相比,scanf或CIN是不是C或C++的標準功能,您可以隨時使用的getchar()對於這裏的目的。

或者您可以編寫一個函數getchar_unlocked()以返回getchar()的值以供機器測試用途,前提是您必須在您的在線問題中使用它。