栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 1480 Optimal Programs

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 1480 Optimal Programs

#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>using namespace std;#define MAXV 30000#define INF 999999#define MAXN 11#define MAXS 11#define ADD 1#define DIV 2#define DUP 3#define MUL 4#define SUB 5int S[MAXN][MAXS],atop[MAXN],y[MAXN];int N,A[MAXN],CA[MAXN],minlen,mCount=1;void Print(int c){switch(c){case ADD:printf("ADD");break;case SUB:printf("SUB");break;case MUL:printf("MUL");break;case DUP:printf("DUP");break;case DIV:printf("DIV");break;}}void dfs(int len){int i,j,a[MAXN],b[MAXN];if(len-1<minlen){for(j=0;j<N;j++)if(S[j][0]!=y[j]||atop[j]!=0) break;if(j==N){minlen=len-1;memcpy(A,CA,sizeof(A));}}if(len>minlen||len>10)return;for(i=1;i<=5;i++){int con=-1; bool LastoneChanged=false;for(j=0;j<N&&con==-1;j++){if(i!=DUP){if(atop[j]==0){con=j;break;}if(i==DIV&&S[j][atop[j]]==0){con=j;break;}b[j]=S[j][atop[j]--];a[j]=S[j][atop[j]--];switch(i){case ADD:S[j][++atop[j]]=a[j]+b[j];break;case DIV:S[j][++atop[j]]=a[j]/b[j];break;case MUL:S[j][++atop[j]]=a[j]*b[j];break;case SUB:S[j][++atop[j]]=a[j]-b[j];break;}CA[len]=i;if(abs(S[j][atop[j]])>MAXV){LastoneChanged=true;con=j;break;}}else{atop[j]++;S[j][atop[j]]=S[j][atop[j]-1];CA[len]=DUP;}}if(con==-1)dfs(len+1);if(i!=DUP)for(j=0;j<N&&(con==-1||((LastoneChanged==false&&j<con)||(LastoneChanged==true&&j<=con)));j++){S[j][atop[j]]=a[j];S[j][++atop[j]]=b[j];}elsefor(j=0;j<N&&(con==-1||((LastoneChanged==false&&j<con)||(LastoneChanged==true&&j<=con)));j++)atop[j]--;}}int main(){int i;while(scanf("%d",&N)&&N){for(i=0;i<N;i++)scanf("%d",&S[i][0]);for(i=0;i<N;i++)scanf("%d",&y[i]);memset(atop,0,sizeof(atop)); minlen=INF;dfs(1);printf("Program %dn",mCount++);if(minlen==INF)printf("Impossiblenn");else if(minlen==0)printf("Empty sequencenn");else{Print(A[1]);for(i=2;i<=minlen;i++){printf(" "); Print(A[i]);}printf("nn");}}return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/366783.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号