UVA-1588

只用C来写

题目:https://vjudge.net/problem/UVA-1588

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define LEN 1000
 4 
 5 int l(int lenth1,int lenth2,int keep)
 6 {
 7     int lenth;
 8     int m=lenth1-lenth2;
 9     if(keep<=m)lenth=lenth1;
10     else lenth=keep+lenth2;
11     return lenth;
12 }
13 
14 int lp(int lenth1,int lenth2,int keep)
15 {
16     return (lenth2-keep-1+lenth1);
17 }
18 int main()
19 {
20     char down[LEN]={'0'};
21     char up[LEN]={'0'};
22     while(scanf("%s",down)!=EOF)
23     {
24 
25         int lenth1,lenth2,i;
26         int check=0,keep,LONG1,LONG2;
27         int LONG;
28         char* temp;
29         scanf("%s",up);
30         lenth1=strlen(down);
31         lenth2=strlen(up);
32         if(lenth1<lenth2)
33         {
34             char u[LEN];
35             int mid;
36             strcpy(u,down);
37             for(i=0;i<LEN;i++)down[i]='';
38             strcpy(down,up);
39             for(i=0;i<LEN;i++)up[i]='';
40             strcpy(up,u);
41             mid=lenth1;
42             lenth1=lenth2;
43             lenth2=mid;
44         }
45         temp=&up[0];
46         for(i=0;;i++)
47         {
48             if(i==lenth1){LONG1=lenth1+lenth2;break;}
49             keep=i;
50             for(;temp<=&up[lenth2-1];temp++,i++)
51             {
52                 int sum;
53                 if(down[i]!='')sum=(*temp-'0')+(down[i]-'0');
54                 if(down[i]=='')sum=0;
55                 if(sum==4){check=0;break;}
56                 if(sum!=4)check=1;
57             }
58             if(check==1){LONG1=l(lenth1,lenth2,keep);break;}
59             i=keep;
60             temp=&up[0];
61         }
62         temp=&up[lenth2-1];
63         check=0;
64         for(i=lenth2-2;;i--)
65         {
66             if(i==-1){LONG2=lenth1+lenth2;break;}
67             keep=i;
68             for(;temp>=&up[0];temp--,i--)
69             {
70                 int sum;
71                 if(down[i]!='')sum=(*temp-'0')+(down[i]-'0');
72                 if(down[i]=='')sum=0;
73                 if(sum==4){check=0;break;}
74                 if(sum!=4)check=1;
75             }
76             if(check==1){LONG2=lp(lenth1,lenth2,keep);break;}
77             i=keep;
78             check=0;
79             temp=&up[lenth2-1];
80         }
81         LONG=(LONG1>LONG2)?LONG2:LONG1;
82         printf("%d
",LONG);
83         for(i=0;i<LEN;i++)up[i]='';
84         for(i=0;i<LEN;i++)down[i]='';
85     }
86     return 0;
87 }
原文地址:https://www.cnblogs.com/savennist/p/12284161.html