这道题很明显是模拟题,可惜我一开始还用枚举……悲剧的浪费了很多时间。还有就是,模拟要模拟一切情况并且保证模拟的结果的正确性,这道题主要花费的时间集中在纠正小错误和发现新情况没有模拟到这些问题上,是一道警醒题!我相信这道题肯定有人比我模拟的好,也能用枚举做出来(可能很麻烦)……下面贴出代码,以作纪念。

View Code
  1 /*{
  2 ID:s42k5ek
  3 PROG:milk3
  4 LANG:C++
  5 }*/
  6 #include<stdio.h>
  7 #include<iostream>
  8 #include<string.h>
  9 using namespace std;
 10 
 11 int amount[3];
 12 int leave[30];
 13 
 14 void paixu(int  *first,int a,int b)
 15 {
 16     int i,j;
 17     i=a;j=b;
 18     int cha=first[a];
 19     if(i>=j)
 20         return;
 21     while(i<j)
 22     {
 23         while(first[j]>=first[i]&&i<j)
 24             j--;
 25         if(i<j)
 26         {
 27             first[i]=first[j];
 28             first[j]=cha;
 29             i++;
 30         }
 31         while(first[i]<=first[j]&&i<j)
 32             i++;
 33         if(i<j)
 34         {
 35             first[j]=first[i];
 36             first[i]=cha;
 37             j--;
 38         }
 39     }
 40     paixu(first,a,i-1);
 41     paixu(first,i+1,b);
 42 }
 43 bool judge(int a,int f,int e)
 44 {
 45     int i;
 46     for(i=f;i<=e;i++)
 47     {
 48         if(leave[i]==a)
 49             return 0;
 50     }
 51     return 1;
 52 }
 53 
 54 int main()
 55 {
 56     freopen ("milk3.in","r",stdin);
 57     freopen ("milk3.out","w",stdout);
 58     int a,b,c,i,q=0,p,j;
 59     scanf("%d%d%d",&a,&b,&c);
 60     amount[0]=0;
 61     amount[1]=0;
 62     amount[2]=c;
 63     while(1)
 64     {
 65         if(amount[0]==0)
 66         {
 67             j=judge(amount[2],1,q-1);
 68             if(j)
 69             {
 70                 leave[q]=amount[2];
 71                 q++;
 72             }
 73             else
 74                 break;
 75         }
 76         if(amount[0]==a||(amount[2]==0&&amount[1]==0))
 77         {
 78             amount[2]+=amount[0];
 79             amount[0]=0;        
 80         }
 81         else if(amount[1]==0)
 82         {
 83             if(amount[2]>=b)
 84             {
 85                 amount[1]=b;
 86                 amount[2]-=b;
 87             }
 88             else
 89             {
 90                 amount[1]+=amount[2];
 91                 amount[2]=0;
 92             }
 93         }
 94         else if(amount[1]!=0)
 95         {
 96             if(amount[1]>=a-amount[0])
 97             {    
 98                 amount[1]-=(a-amount[0]);
 99                 amount[0]=0;
100                 amount[2]+=a;
101             }
102             else
103             {
104                 amount[0]+=amount[1];
105                 amount[1]=0;
106             }
107         }
108     }
109     p=q;
110     amount[0]=0;
111     amount[1]=0;
112     amount[2]=c;
113     while(1)
114     {
115         if(amount[1]==b)
116         {
117             amount[1]=0;
118             amount[2]+=b;
119         }
120         else if(amount[0]==0)
121         {
122             j=judge(amount[2],p,q-1);
123             if(j)
124             {
125                 leave[q]=amount[2];
126                 q++;
127             }
128             else
129                 break;
130             if(amount[2]>=a)
131             {
132                 amount[0]=a;
133                 amount[2]-=a;
134             }
135             else
136             {
137                 amount[0]+=amount[2];
138                 amount[2]=0;
139             }
140         }
141         else if(amount[0]!=0)
142         {
143             if(amount[0]>=b-amount[1])
144             {    
145                 amount[0]-=(b-amount[1]);
146                 amount[1]=0;
147                 amount[2]+=b;            
148             }
149             else
150             {
151                 amount[1]+=amount[0];
152                 amount[0]=0;
153             }
154         }
155     }
156     if(c>=b)
157     {
158         leave[q]=c-b;
159         q++;
160     }
161     paixu(leave,0,q-1);
162     for(i=0;i<q;i++)
163     {
164         if(leave[i]!=leave[i+1]&&i!=q-1)
165             printf("%d ",leave[i]);
166         else if(i==q-1)
167             printf("%d\n",leave[i]);
168     }        
169     return 0;
170 }

 

posted on 2012-07-12 17:04  醉春雨  阅读(154)  评论(0)    收藏  举报