密码 hpuacm
A题中 23:59:59 这样的时间输出可以当做字符串处理
struct people{
char name[20];
char com[20];
char lea[20];
};
struct people peo[10000];
bool cmp1( struct people p1, struct people p2 )
{
return strcmp(p1.com, p2.com) < 0;
}
bool cmp2( struct people p1, struct people p2 )
{
return strcmp(p1.lea, p2.lea) > 0; //jiangxu
}
while( n-- )
{
scanf("%d", &m );
for( int i=0; i<m; i++ )
{
scanf("%s %s %s", peo[i].name, peo[i].com, peo[i].lea );
}
sort(peo, peo+m, cmp1);
printf("%s", peo[0].name );
sort(peo, peo+m, cmp2);
printf(" %s\n", peo[0].name );
}
B题的指定排序排序cmp可以写成
int c; //quanju bianliang
struct student{
char xuehao[10];
char name[10];
int grade;
};
struct student stu[100000+5];
bool cmp( struct student s1, struct student s2 )
{
if( c == 1 )
{
return strcmp( s1.xuehao, s2.xuehao ) < 0;
}
else if( c == 2 )
{
if( strcmp( s1.name, s2.name ) == 0 )
return strcmp( s1.xuehao, s2.xuehao ) < 0;
else
return strcmp( s1.name, s2.name ) < 0;
}
else
{
if( s1.grade == s2.grade )
return strcmp( s1.xuehao, s2.xuehao ) < 0;
else
return s1.grade < s2.grade;
}
}
C题快速两两求和方法,注意j是从i+1开始的
int index = 0;
for( int i=0; i<n; i++ )
{
for( int j=i+1; j<n; j++ )
{
num[index++] = arry[i]+arry[j];
}
}
E题稳定排序函数
stable_sort(stu_in, stu_in + n, cmp);
此外E题不能在一个for循环里同时检查Error, Not Stable, Right 三中情况,这有可能造成本应该是Error却判断成 Not Stablle的错误。好的解决方法是先检查是否为Error,如果不是在检查另外两种情况。
F题的这种需要先排序然后又恢复原来顺序的情况,可以在定义结构体时加上ID,来进行标记,之后再用ID排序一次来恢复之前的顺序。
struct student{
int number;
char tim[20];
int score;
int id;
};
struct student stu[100+5];
bool cmp2( struct student s1, struct student s2 )
{
return s1.id < s2.id;
}
sort(stu, stu+n, cmp2);
G题先去重然后判断是否有三个连续的数
int index = 0;
for( int i=1; i<n; i++ )
{
if( newsize[index] != ballsize[i] )
newsize[++index] = ballsize[i];
}
bool flag = false;
for( int i=0; i+2<n; i++ )
{
if( newsize[i+2] - newsize[i+1] == 1 && newsize[i+1] - newsize[i] == 1 )
{
flag = true;
break;
}
H题是贪心算法中的区间调度问题,注意的是用sort排序的时候是按结束时间
struct Time{
int s, e;
};
bool cmp( struct Time t1, struct Time t2 )
{
return t1.e < t2.e;
}
sort(arr, arr+n, cmp);
int ans = 0;
int temp = 0;
for( int i=0; i<n; i++ )
{
if( temp <= arr[i].s )
{
ans ++;
temp = arr[i].e;
}
}
printf("%d\n", ans );