uva 11111 - Generalized Matrioshkas

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

如果wrong掉,不妨试一下这些数据

-100 -5 -4 -10 4 5 10 100

-1 -1 1 1

-1 1 -1 1

  
    
#include < stdio.h >
int num[ 10000 ];
int main()
{
char c;
int i,j,top,can,temp,local,found,n = 0 ;
while (scanf( " %d%c " , & num[n ++ ], & c) != EOF)
{
if (c != ' \n ' )
continue ;
if (n & 1 )
{
printf(
" :-( Try again.\n " );
n
= 0 ;
continue ;
}
top
= n - 1 ;
temp
= 0 ;
for (i = 0 ;i < n && num[i] < 0 ;i ++ );
local
= i;
can
= 1 ;
while (top >= local)
{
for (i = top - 1 ;i >= 0 && num[i] + num[top] != 0 ;i -- )
{
if (num[i] == num[top])
{
can
= 0 ;
break ;
}
}
if (i ==- 1 || can == 0 )
{
can
= 0 ;
break ;
}
temp
= i + 1 ;
for (i = temp + 1 ;i < top;i ++ )
{
found
= 0 ;
if (num[temp] + num[i] == 0 )
{
for (j = temp + 1 ;j < i;j ++ )
if (num[j] == num[i])
{
can
= 0 ;
break ;
}
if (can == 0 )
break ;
num[top]
-= num[i];
temp
= i + 1 ;
found
= 1 ;
}
}
if (num[top] <= 0 || can == 0 || found == 0 )
{
can
= 0 ;
break ;
}
top
-- ;
while (num[top] < 0 && top >= 0 )
top
-- ;
}
if (can)
printf(
" :-) Matrioshka!\n " );
else
printf(
" :-( Try again.\n " );
n
= 0 ;
}
return 0 ;
}

你可能感兴趣的:(ios)