Kickdown, ACM/ICPC NEERC 2006, UVa1588

来源:互联网 发布:工商局可以投诉淘宝吗 编辑:程序博客网 时间:2024/05/16 09:55

题目:给出两个长度分别为n1,n2(n1,n2<=100)且每列高度只为1或2的长条。需要将它们放入一个高度为3的容器,问能够容纳它们的最短容器长度

思路: 从左到右依次检查,再将数组翻转


这里似乎有点小问题,考虑所有的翻转情况,上下数组移动情况时,会出现比标准答案更小的结果


 1 2 3 4 5 6 7 8 9101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
#include<iostream>#include<cstring>using namespace std;int cal(char bottom[],char top[]){char temp_b[202]="",temp_t[202]="";strcpy(temp_b,bottom);strcpy(temp_t,top);int blen=strlen(temp_b),tlen=strlen(temp_t);int i=0;while(1){bool key=1;for(int j=0;j<tlen;j++){if(i+j==blen){strcat(temp_b,"0");blen++;continue;}if(temp_b[i+j]+temp_t[j]>'3'+'0'){key=0;break;}}if(key){/*for(int k=0;k<i;k++)cout << " ";cout << temp_t << endl << temp_b;cout << endl <<"num=" << blen << endl; */return blen;}i++;}}void re(char str[]){int len=strlen(str);for(int i=0,j=len-1;i<j;i++,j--){char tem=str[i];str[i]=str[j];str[j]=tem;}}int main(){char bottom[101]="",top[101]="";while(cin >> bottom >> top){/*int min=201,tem;for(int i=0;i<2;i++){for(int j=0;j<2;j++){tem=cal(bottom,top);min=(min<tem)? min:tem;re(top);}re(bottom);}cout << min << endl;*/int min=cal(bottom,top),tem=cal(top,bottom);min=min<tem? min:tem;re(bottom);re(top);tem=cal(bottom,top);min=min<tem? min:tem;tem=cal(top,bottom);min=min<tem? min:tem;cout << min << endl; }return 0;}

题目来源:算法竞赛入门经典(第2版) 刘汝佳

原创粉丝点击