这道题本身比我之前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的时候的确是最优的,即使代码什么都不改