hdu 1234_sort的陷阱

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1234

题目比较简单,用sort时遇到了点问题,这个函数要求对于调用的两个参数交换位置时不能得到相同的true的结果。 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; 
int t,m,i;
char fw[20],lg[20];
struct N{
	char id[20],w[20],g[20];
}n[1000005];
bool cmp1(N a,N b)
{
	return strcmp(a.w,b.w) < 0 ? true : false;
	/*
	如果换成:return strcmp(b.w,a.w)则会出错,跟踪源代码得到:

	template inline
  bool __CLRCALL_OR_CDECL _Debug_lt_pred(_Pr _Pred, _Ty1& _Left, _Ty2& _Right,
  const wchar_t *_Where, unsigned int _Line)
  {    // test if _Pred(_Left, _Right)  and _Pred is strict weak ordering
  if (!_Pred (_Left, _Right))
  return (false);
  else if (_Pred(_Right, _Left))
  _DEBUG_ERROR2("invalid operator<", _Where, _Line);
  return (true);
  }

  这个函数要求对于调用的两个参数交换位置时不能得到相同的true的结果。
	*/
}
bool cmp2(N a,N b)
{
	return strcmp(a.g,b.g) > 0 ? true : false;
}
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&m);
		for(i = 0; i < m; i++) 
		{
			scanf("%s%s%s",&n[i].id,&n[i].w,&n[i].g);
		}
		sort(n, n + m,cmp1);
		strcpy(fw,n[0].id);
		sort(n, n + m,cmp2);
		strcpy(lg,n[0].id);
		printf("%s %s\n",fw,lg);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值