關於std::ios::sync_with_stdio(false)

來源:互聯網
上載者:User

標籤:als   problem   c++   基本   輸出   sort   false   預留位置   lan   

  很多C++的初學者可能會被這個問題困擾,經常出現程式無故逾時,最終發現問題處在cin和cout上,(甚至有些老oier也會被這個問題困擾,每次能打scanf和printf,然後一堆的預留位置巨麻煩),這是因為C++中,cin和cout要與stdio同步,中間會有一個緩衝,所以導致cin,cout語句輸入輸出緩慢,這是就可以用這個語句,取消cin,cout與stdio的同步,說白了就是提速,效率基本與scanf和printf一致。然後就可放心的使用cin,cout了;下面放一個例題,大水題排隊接水:

#include<bits/stdc++.h>#define N 1010using namespace std;int n;long long sum;long double ans;struct Num{int val,id;}a[N];bool cmp(Num x,Num y){return x.val<y.val;}void ready(){  cin>>n;  for(int i=1;i<=n;i++){    cin>>a[i].val;a[i].id=i;}  sort(a+1,a+n+1,cmp);}void work(){  for(int i=1;i<=n;i++){    cout<<a[i].id<<" ";    sum+=(long long)(a[i].val*(n-i));}  cout<<endl;  ans=(long double)(sum)/double(n);  printf("%.2Lf",ans);}int main(){  std::ios::sync_with_stdio(false);  ready();work();return 0;}

 

關於std::ios::sync_with_stdio(false)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.