[刷题笔记]HDU2093-sscanf实战以及sort函数的运用

题目:HDU2093

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2093

结果:6次 (┬_┬);

要点:

  1. 复杂条件的sort函数的使用,即cmp函数的书写;
  2. sscanf的实际应用;
感想:
  1. 各种不熟练啊!多练多写;
  2. o( ̄皿 ̄///) !教练,我要学C++!

代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct student
{
    char name[11];
    int k, s;
}stu[1000]={0};
int cmp(student a, student b)
{
    if(a.k!=b.k)
        return a.k>b.k;
    else if(a.s!=b.s)
        return a.s<b.s;
    else 
        return strcmp(a.name, b.name)<0;
}
int main()
{
    int m, n, i = 0, rs, a, b, j;
    char all[10];
    scanf("%d%d", &m, &n);
    while (scanf("%s",stu[i].name)!=EOF)
    {
        for (j=0;j<m;j++)
        {
            scanf("%s",all);
            rs = sscanf(all,"%d(%d)", &a, &b);
            if(rs==1&&a>0)
            {
                stu[i].s += a;
                stu[i].k++;
            }
            else if(rs==2)
            {
                stu[i].k++;
                stu[i].s += a + b * n;
            }
        }
        i++;
    }
    sort(stu,stu+i,cmp);
    for(j=0;j<i;j++)
        printf("%-10s %2d %4d\n", stu[j].name, stu[j].k, stu[j].s);
    system("pause");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值