閱讀295 返回首頁    go 阿裏雲 go 技術社區[雲棲]


poj 2591 Set Definition【OJ實驗】

這道題本身比我之前A的題目要簡單,我自己另外在OJ上用這道題做了幾個有趣一點的實驗
poj2591隻是定義集合的方式不同了而已。。。
ans[a2]*2+1,ans[a3]*3+1,還有注意一點就是我改的時候用%I64d不能輸入int型的數,OJ會爆Runtime error
先貼AC的代碼:
#include <iostream>
#include <stdio.h>

using namespace std;

int ans[10000010]={0,1};

int getMin(int a,int b){return a<b?a:b;}


int main()
{
    int a2,a3,i,tmp,n;
    a2=a3=1;
    for(i=2;i<=10000000;i++)
    {
        tmp=getMin(ans[a2]*2+1,ans[a3]*3+1);
        ans[i]=tmp;
        if(tmp==ans[a2]*2+1)
            ++a2;
        if(tmp==ans[a3]*3+1)
            ++a3;
    }
    
	while(scanf("%d",&n)!=EOF)
		printf("%d\n",ans[n]);
    return 0;
}

實驗內容:
1.在OJ上,inline可以寫
2.可以沒有<stdio.h>,隻有<iostream>用標準輸入輸出scanf和printf
3.可以沒有using namespace std;這段代碼一樣正確,而且運行時間大減!!
4.用<stdio.h>比<iostream>運行時間長很多。。。


第一行的是隻用<stdio.h>,Time: 188MS
第二行隻用<iostream>,Time: 63MS
第三行用了<iostream>+using namespace std;	Time: 391MS
第四行用了<iostream>+using namespace std;+<stdio.h>+inline Time: 172MS
第五行用了<iostream>+using namespace std;+<stdio.h>   time:266MS

這樣看來用iostream,並不用std的時候的確是最優的,即使代碼什麼都不改

最後更新:2017-04-03 14:53:58

  上一篇:go GetWindowRect、GetClientRect、ScreenToClient與ClientToScreen
  下一篇:go 再看Core Data中PSC陷入死鎖的問題