上文链接:https://blog.csdn.net/slient_love/article/details/104284351
类似题
/* 多关键字排序
描述: 给定一组记录n(n<100)小明各个时期的考试成绩 ,格式为日期+成绩,中间以空格隔开,记录之间
分行输入 ,例如
2008/6/3 80
2009/1/1 56
….
其中日期输入要求年份1996-2100 月份1-12 日期1-31
现要求以分数为关键字从大到1小对其进行排序,若分数相同则按日期从小到大排序
输入样例
4
2017/1/1 95
2017/6/10 85
2017/3/2 95
2017/1/1 65
输出样例
2017/1/1 95
2017/3/2 95
2017/6/10 85
2017/1/1 65
思路:这是一个典型的多关键字排序问题,可建立恰当结构体,利用stl自带的排序算法实现快速解题
*/
代码展示:
#include<iostream>
#include<algorithm>
#include <cstdio>
using namespace std;
#define MAX 100
#define IsLeap(x) x%4==0&&x%100!=0 ||x%400==0
//月份日期对应表
int Day[13]={
0,31,28,31,30,31,30,31,31,30,31,30,31
};
//记录结构体
typedef struct
{
char date[100];
int grade;
}Record;
//自定义排序规则
bool cmp(Record x,Record y)
{
if(x.grade!=y.grade)
return x.grade>y.grade;
else{
int Y,m,d,Y1,m1,d1;
sscanf(x.date,"%d/%d/%d",&Y,&m,&d);
sscanf(y.date,"%d/%d/%d",&Y1,&m1,&d1);
if(Y!=Y1)
return Y<Y;
if(m!=m1)
return m<m1;
if(d!=d1)
return d<d1;
}
}
//日期有效性检测
bool isValid(char* str)
{
int y,m,d;
bool valid=true;
sscanf(str,"%d/%d/%d",&y,&m,&d);
if((y<1996)||(y>2100))
valid=false;
if((m<1)||(m>12))
valid=false;
if((IsLeap(y))&&(m==2)) //闰年加1
Day[m]=Day[m]+1;
if((d<1)||(d>Day[m]))
valid=false;
return valid;
}
int main()
{ // 记录数组
Record res[MAX];
bool flag[MAX];
int n;
while(cin>>n)
{
// 输入记录
for(int i=0;i<n;i++)
cin>>res[i].date>>res[i].grade;
// 根据自定义排序规则排序
sort(res,res+n,cmp);
// 格式化输出,过滤掉无效日期
for(int i=0;i<n;i++)
{
if(isValid(res[i].date))
cout<<res[i].date<<" "<<res[i].grade<<endl;
}
}
return 0;
}
sscanf函数用法:
函数原型:
Int sscanf( string str, string fmt, mixed var1, mixed var2 … );
int scanf( const char *format [,argument]… );
说明:
sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源。
其中format可以是一个或多个 {%[ *] [width] [{h | l | I64 | L}]type | ’ ’ | ‘/t’ | ‘/n’ | 非%符号}
注:
1.* 亦可用于格式中, (即 %*d 和 %s) 加了星号 () 表示跳过此数据不读入. (也就是不把此数据读入参数中)
2. {a|b|c}表示a,b,c中选一,[d], 表示可以有d也可以没有d。
3. width表示读取宽度。
4. {h | l | I64 | L}:参数的size,通常h表示单字节size,I表示2字节 size,L表示4字节size(double例外),l64表示8字节size。
5. type:这就很多了,就是%s,%d之类。
6. 支持集合操作:
%[a-z] 表示匹配a到z中任意字符,贪婪性(尽可能多的匹配)
%[aB’] 匹配a、B、'中一员,贪婪性
%[^a] 匹配非a的任意字符,贪婪性
用法示例:
(1)sscanf(“123456”, “%3s”, str); //取指定长度的字符串
printf(“str=%s\n”, str); //str=“123”;
(2)sscanf(“hello world!”, “%[^ ]”, str); //取到指定字符为止的字符串,取遇到空格为止字符串
printf(“str=%s\n”, str); //str=hello ;
(3)sscanf(“123456abcdedfABCDEF”, “%[1-9a-z]”, str); //取仅包含指定字符集的字符串
printf(“str=%s\n”, str); //str=123456abcded,只取数字和小写字符
(4)sscanf(“BCDEF123456abcdedf”, “%[^a-z]”, str); //取到指定字符集为止的字符串
printf(“str=%s\n”, str); // str=BCDEF123456, 取遇到大写字母为止的字符串
(5)int a,b,c;
sscanf(“2015.04.05”, “%d.%d.%d”, &a,&b,&c); //取需要的字符串
printf(“a=%d,b=%d,c=%d”,a,b,c); // a=2015,b=4,c=5
参考链接:
https://blog.csdn.net/qq_15034457/article/details/79759810
https://blog.csdn.net/zhouwei1221q/article/details/44890617