POJ-3195

#include <stdio.h>

int main()
{
    int toys[1000], top = -1;
    int size, sum = 0;
    
    while (scanf("%d", &size) != EOF) {
          if (size < 0)
             toys[++top] = size;
          else {
               sum = 0;
               while (toys[top] > 0 && sum < size) {
                     sum += toys[top--];
               }
               if (sum >= size || toys[top] + size != 0) {
                  top = -1;
                  printf(":-( Try again.\n");
                  while ((sum = getchar()) != '\n' && sum != EOF)
                        ;
               }
               else  if (top == 0) {
                        if ((sum = getchar()) == '\n' || sum == EOF) {
                           top--;
                           printf(":-) Matrioshka!\n");
                        }
                        else {
                             top = -1;     
                             printf(":-( Try again.\n");
                             while ((sum = getchar()) != '\n' && sum != EOF)
                                   ;
                        } 
               }
               else 
                    toys[top] = -toys[top];
          }
          if (top != -1 &&((sum = getchar()) == '\n' || sum == EOF)) {
             top = -1;     
             printf(":-( Try again.\n");
          }
    }
    return 0; 
} 

你可能感兴趣的:(POJ-3195)