Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
20042 | 王循 | 最大乘积 | C++ | 运行出错 | 0 | 0 MS | 248 KB | 3835 | 2021-05-28 17:24:14 |
#include<iostream> #include<cstring> using namespace std; struct highUnsignedAccuracyNumber{ public: char* data; int length; highUnsignedAccuracyNumber(int tmpLength){ length=tmpLength; data=new char[tmpLength]; memset(data,0,tmpLength); }; ~highUnsignedAccuracyNumber(){ delete data; } }; highUnsignedAccuracyNumber* pInHUAN; highUnsignedAccuracyNumber* pResult=0; highUnsignedAccuracyNumber* mulHighUnsignedAccuracyNumber(highUnsignedAccuracyNumber& h1,highUnsignedAccuracyNumber& h2){ int expLen=h1.length+h2.length; highUnsignedAccuracyNumber* ret=new highUnsignedAccuracyNumber(expLen); int max=-1; for(int i=0;i<h1.length;++i){ for(int j=0;j<h2.length;++j){ int k=i+j; ret->data[k]+=h1.data[i]*h2.data[j]; if(ret->data[k]>9){ ret->data[k+1]+=ret->data[k]/10; ret->data[k]%=10; k+=1; } if(max<k){ max=k; } } } ret->length=max+1; return ret; }; bool compare(highUnsignedAccuracyNumber& h1,highUnsignedAccuracyNumber& h2){ if(h1.length<h2.length){ return true; } else if(h1.length==h2.length){ for(int i=h2.length-1;i>=0;--i){ if(h1.data[i]<h2.data[i]){ return true; } else if(h1.data[i]>h2.data[i]){ break; } } } return false; } void printhighUnsignedAccuracyNumber(highUnsignedAccuracyNumber* val){ bool isStart=false; for(int i=val->length-1;i>=0;--i){ if(isStart){ cout<<(int)val->data[i]; } else if(val->data[i]>0){ isStart=true; ++i; } } cout<<endl; } void dfs(int N,int K,int* pIndexArr,int cur=1,int lay=0){ if(lay<K){ } else{ return; } for(int i=cur;i<N;++i){ if(i+K-1-lay<=N){ pIndexArr[lay]=i; dfs(N,K,pIndexArr,i+1,lay+1); } if(lay==K-1){ int start=0; highUnsignedAccuracyNumber* h1=new highUnsignedAccuracyNumber(1); h1->data[0]=1; for(int i=0;i<K+1;++i){ highUnsignedAccuracyNumber* h2=new highUnsignedAccuracyNumber(pIndexArr[i]-start); for(int j=start,k=0;j<pIndexArr[i];++j,++k){ h2->data[k]=pInHUAN->data[j]; } start=pIndexArr[i]; highUnsignedAccuracyNumber* ret= mulHighUnsignedAccuracyNumber(*h1,*h2); delete h1; delete h2; h1=ret; } if(pResult==0){ pResult=h1; } else if(compare(*pResult,*h1)){ delete pResult; pResult=h1; } } } } int main(int argc, char const *argv[]) { int N,K; cin>>N>>K; pInHUAN=new highUnsignedAccuracyNumber(N); int* pIndexArr=new int[K+1]; cin>>pInHUAN->data; for(int i=0;i<pInHUAN->length;++i){ pInHUAN->data[i]-='0'; } for(int i=0;i<pInHUAN->length/2;++i){ char c; c=pInHUAN->data[i]; pInHUAN->data[i]=pInHUAN->data[pInHUAN->length-1-i]; pInHUAN->data[pInHUAN->length-1-i]=c; } pIndexArr[K]=pInHUAN->length; dfs(N,K,pIndexArr); printhighUnsignedAccuracyNumber(pResult); return 0; }