uva 11111 Generalized Matrioshkas

大意是:判断一个玩具内能否容下其它小玩具,当且仅当其它玩具尺寸之和小于外层玩具时输出Matrioshka

//1Y的代码,很好,其实就是模拟一下,注意一下细节

 

#include <stdio.h>
#include <string.h>
#define MAX 10010
struct node
{ int size,volume;};
struct node a[MAX],temp[MAX];
int n,top,outside;
int JUDGE()
{
    int i;
    memset(a , 0 ,sizeof(a));
 // printf("test a volume\n");for(i=0; i<n; i++) printf("%d ",a[i].volume);  printf("\n");
    outside=-1; top=0;
    for(i=0; i<n; i++)
    {
        if(temp[i].size<0) 
        {
            a[top].size=temp[i].size;   top++; 
            if(outside>=0) 
            { 
                a[outside].volume+=(0-temp[i].size);
                if(a[outside].volume>=(0-a[outside].size))  return 0;
            }
            outside++;  a[outside].volume=0;
        }
        else  // temp[i].size>0
        {
            a[top].size=temp[i].size;
            if( (a[top].size+a[top-1].size) )   return 0;
            else  //说明配对成功
            {  top--;  outside--;  }
        }

    }
    if(top<=0)  return 1;
    else        return 0; 
}
int main()
{
    int i;  int end;  char ch;
    while(1)
    {
        i=end=0;
        while(1)
        {
            if(scanf("%d",&temp[i].size)==EOF) {end=1; break;} i++;
            ch=getchar();  if(ch=='\n')  break;
        }
        if(end) return 0;
        n=i; //for(i=0; i<n; i++)  printf("%d ",temp[i].size);  printf("\n");
        if(JUDGE())  printf(":-) Matrioshka!\n");
        else         printf(":-( Try again.\n");
    }
    return 0;
}

 

 

 

 

posted @ 2012-10-05 22:41  Titanium  阅读(298)  评论(0编辑  收藏  举报