Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
怎样才能加速?总是Time limit exceeded.谢谢#include <iostream.h> const char endchar='Z'; const int size=150; int mylen(char *source) //自定义的求字符串长度函数,以endchar为结束标志 { int i=0; for(;*source!=endchar;source++) i++; return i-1; //-1是为了去掉最前的符号位 } void main() { char result[size];result[0]='k'; char buf[size];buf[0]='k'; char buf2[size];buf2[0]='k'; char old[size];old[0]='k'; char ch; int exp,pointpos,i,j,h,k,len,temp,current,oldlen; bool flag=false; while(!cin.eof()) { flag=false; for(i=1,h=1;;h++) { cin.get(ch); if(ch==' ') break; if(ch!='.' && ch!='0') { result[i]= ch; flag=true; ++i; } else if(ch=='.') pointpos= 6-h; else if(ch=='0' && flag==true) { result[i]=ch; ++i; } } result[i]=endchar; len=mylen(result); oldlen=len; for(i=1;i<=len;i++) result[i]=result[i]-'0'; for(j=1;j<=len;j++) buf[j]=result[len+1-j]; for(j=1;j<=len;j++) { result[j]=buf[j]; old[j]=buf[j]; } cin>>exp; for(k=1;k<exp;k++) { result[0]='k'; len=mylen(result); for(j=1;j<=len;j++) buf[j]=result[j]; for(j=1;j<=len+1;j++) result[j]=0; for(i=1;i<=len;i++) { temp=0; for(j=1;j<=oldlen;j++) { current=result[j+i-1]+buf[i]*old[j]+temp; temp=current/10; result[j+i-1]=current%10; } result[j+i-1]=temp; } if(result[j+i-2]==0) result[j+i-2]=endchar; else result[j+i-1]=endchar; } //开始输出部分 buf[0]='k'; buf2[0]='k'; pointpos*=exp; len=mylen(result); //点小数点 if(pointpos<len) { for(i=1,j=1;j<=len;) { if(i!=pointpos+1) { buf2[i]=result[j]; i++; j++; } else { buf2[i]='.'; i++; } } buf2[i]=endchar; } else { for(i=1,j=1;j<=len;) { buf2[i]=result[j]; i++; j++; } for(;i<=pointpos;i++) buf2[i]=0; buf2[i]='.'; buf2[i+1]=endchar; } len=mylen(buf2); flag=false; for(i=1;i<=pointpos;i++) { if(buf2[i]!=0) { buf2[i-1]=endchar; flag=true; break; } } if(flag==false) buf2[pointpos+1]=endchar; for(i=len;buf2[i]!=endchar;i--) { if(buf2[i]!='.') cout<<int(buf2[i]); else cout<<buf2[i]; } cout<<endl; cin.ignore(1); } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator