经典的从一堆真硬币中称出一个假硬币的题
#include <stdio.h>
#include <string.h>
bool flag[1010];
bool temp[1010];
int getnum[1010];
int dy[1010], xy[1010];
int n, m;
void init()
{
memset(flag, 0, sizeof(flag));
memset(dy, 0, sizeof(dy));
memset(xy, 0, sizeof(xy));
}
int work()
{
int cnt = 0, k;
for ( int i = 1 ; i <= n ; i++ )
{
if ( !flag[i] )
{
k = i;
cnt++;
}
if ( cnt > 1 )
return 0;
}
return k;
}
int main()
{
while ( 2 == scanf("%d%d", &n, &m) )
{
init();
while ( m-- )
{
int num, i;
char ope;
scanf("%d", &num);
for ( i = 0 ; i < 2*num ; i++ )
scanf("%d", getnum+i);
scanf("%*c%c", &ope);
if ( '=' == ope )
for ( i = 0 ; i < 2*num ; i++ )
flag[getnum[i]]= 1;
else if ( '>' == ope )
{
memset(temp, 0, sizeof(temp));
for ( i = 0 ; i < num ; i++ )
{
dy[getnum[i]]= 1;
temp[getnum[i]]= 1;
}
for ( ; i < 2*num ; i++ )
{
xy[getnum[i]]= 1;
temp[getnum[i]]= 1;
}
for ( i = 1 ; i <= n ; i++ )
{
if ( !temp[i] )
flag[i]= 1;
}
}
else if ( '<' == ope )
{
memset(temp, 0, sizeof(temp));
for ( i = 0 ; i < num ; i++ )
{
xy[getnum[i]]= 1;
temp[getnum[i]]= 1;
}
for ( ; i < 2*num ; i++ )
{
dy[getnum[i]]= 1;
temp[getnum[i]]= 1;
}
for ( i = 1 ; i <= n ; i++ )
{
if ( !temp[i] )
flag[i]= 1;
}
}
}
for ( int i = 1 ; i <= n ; i++ )
{
if ( dy[i] && xy[i] )
flag[i]= 1;
}
printf("%d\n", work());
}
return 0;
}