2020.1.1大一寒假集训二(桶排序与结构体排序)

A题   谁考了第k名-排序(NEFU OJ 1481)

#include 

using namespace std;
struct student{
    int xh;
    double cj;

}a[105];

bool cmp(student a,student b)        //结构体排序
{
    if(a.cj!=b.cj)
        return a.cj>b.cj;
}
int main()
{
    int n,k;
    while(cin >> n>> k)
    {
        for(int i=1;i<=n;i++)
        {
            cin >> a[i].xh;
            cin >> a[i].cj;
        }
        sort(a+1,a+1+n,cmp);
        printf("%d %g",a[k].xh,a[k].cj);
    }
    return 0;
}

B题 奇数单增序列(NEFU OJ 1482)

#include 

using namespace std;

int main()
{
    int n;
    while(cin >> n)
    {
        int a[505],x;
        int tmp=0;            //记录奇数的个数
        for(int i=1;i<=n;i++)
        {
            cin >> x;
            if(x&1)
                a[++tmp]=x;
        }
        sort(a+1,a+1+tmp);
        for(int i=1;i<=tmp;i++)
        {
            cout << a[i];
            if(i!=tmp)
                cout <<",";
            else
                cout << endl;
        }
    }
    return 0;
}

C题 成绩排序(NEFU OJ 1483)

#include 

using namespace std;

struct student{
    char name[25];
    int cj;

}a[25];

bool cmp(student a,student b)
{
    if(a.cj!=b.cj)
        return a.cj>b.cj;
    else
    {
        if(strcmp(a.name,b.name)<0)    //strcmp比较字符串
            return 1;
        else
            return 0;
    }
}
int main()
{
    int n;
    while(cin >> n)
    {
        for(int i=1;i<=n;i++)
        {
            getchar();
            scanf("%s %d",a[i].name,&a[i].cj);
        }
        sort(a+1,a+1+n,cmp);
        for(int i=1;i<=n;i++)
        {
            cout << a[i].name << " ";
            cout << a[i].cj << endl;
        }
    }
    return 0;
}

D题 没必要的排序1(NEFU OJ 1659)

#include 

using namespace std;

int main()
{
    int n,k;
    while(cin >> n >> k)
    {
        int a[1005];
        long long ans=0;
        for(int i=1;i<=n;i++)
            cin >> a[i];
        sort(a+1,a+1+n);
        for(int i=n;i>=n-k+1;i--)
            ans+=a[i];
        cout << ans << endl;
    }

    return 0;
}

E题 没必要的排序2(NEFU OJ 1650)

#include 

using namespace std;

int main()                    //桶排序
{
    int n,k;
    while(cin >> n)
    {
        cin >> k;
        int a[100005]={0},x,maxx=0;
        long long ans=0;
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            cin >> x;
            a[x]++;
            if(maxx=1;i--)
        {
            if(a[i]!=0)
            {
                while(1)
                {
                    ans+=i;
                    a[i]--;
                    j++;
                    if(a[i]==0||k==j)
                        break;
				}
            }
            if(k==j)        //忘了就会错
                break;
        }
        cout << ans << endl;
    }
    return 0;
}

F题 老和尚的导员(NEFU OJ 554)

#include 

using namespace std;

struct stu
{
    int x,y,z,k;
    int xh;
    int ans=0;
} a[105];

bool cmp( stu a,stu b)
{
    if(a.ans!=b.ans)
        return a.ans>b.ans;
    else
    {
        if(a.x!=b.x)
            return a.x > b.x;
        else
        {
            if(a.y!=b.y)
                return a.y > b.y;
            else
            {
                if(a.z!=b.z)
                    return a.z > b.z;
                else
                    return a.k > b.k;
            }
        }
    }
}
int main()
{
    int n;
    while(cin >>n )
    {
        memset(a,0,sizeof(a));
        for(int i=1; i<=n; i++)
        {
            cin >> a[i].x >> a[i].y >>a[i].z >> a[i].k;
            a[i].ans+=a[i].x+a[i].y +a[i].z +a[i].k;
            a[i].xh=i;
        }
        sort(a+1,a+1+n,cmp);
        for(int i=1; i<=n; i++)
            cout << a[i].xh << " " << a[i].ans << endl;
    }
    return 0;
}

G题 健忘的老和尚(NEFU OJ 556)

#include 

using namespace std;

struct stu{
    char name[105];
    int cj;
}a[1005];

bool cmp(stu a,stu b)
{
    if(a.cj!=b.cj)
        return a.cj> n >> m >> o)
    {
        int i;
        for(i=1;i<=n;i++)
        {
            getchar();
            scanf("%s %d",a[i].name,&a[i].cj);
        }
        sort(a+1,a+1+n,cmp);
        for(i=n-m+1;i<=n;i++)
            printf("%s\n",a[i].name);
        for(i=1;i<=o;i++)
            printf("%s\n",a[i].name);
    }
    return 0;
}

H题 戏说三国(NEFU OJ 873)

#include 
using namespace std;
struct student
{
    double s,x,y,z,x1,y1,z1;
    char name[30];
};
bool cmp(student m,student n)
{
    if(m.s!=n.s)return m.s>n.s;
    else if(m.x1!=n.x1)return m.x1>n.x1;
    else if(m.y1!=n.y1)return m.y1>n.y1;
    else if(m.z1!=n.z1)return m.z1>n.z1;
}
student p[100001];
int main()
{
    int t,n,i,I;
    double a,b,c;
    while(scanf("%d",&t)!=-1)
    {
        for(I=1;I<=t;I++)
        {
            scanf("%d%lf%lf%lf",&n,&a,&b,&c);
            for(i=1;i<=n;i++)
            {
                scanf("%s%lf%lf%lf",p[i].name,&p[i].x,&p[i].y,&p[i].z);
                p[i].x1=p[i].x*b*0.01;
                p[i].y1=p[i].y*a*0.01;
                p[i].z1=p[i].z*c*0.01;
                p[i].s=p[i].x1+p[i].y1+p[i].z1;
            }
            sort(p+1,p+n+1,cmp);
            printf("Case #%d:\n",I);
            for(i=1;i<=n;i++)
            printf("%s %.4lf %.4lf %.4lf %.4lf\n",p[i].name,p[i].s,p[i].x1,p[i].y1,p[i].z1);
        }
    }
    return 0;
}

I题 相约摩洛哥(NEFU OJ 874)

#include
using namespace std;
struct node{
	char team[15];
	int at, bt, ct;
	int aw, bw, cw;
	int tot, cnt;
}com[100100];
bool comp(node x, node y)
{
	return x.tot>n)
	{
		for(int i=1; i<=n; i++)
			scanf("%s%d%d%d", com[i].team, &com[i].at, &com[i].bt, &com[i].ct);
		for(int i=1; i<=n; i++)
		{
			scanf("%d%d%d", &com[i].aw, &com[i].bw, &com[i].cw);
			com[i].cnt=0;
			com[i].tot=0;
		}
		for(int i=1; i<=n; i++)
		{
			if(com[i].at != -1)
			{
				com[i].cnt++;
				com[i].tot += com[i].at + (com[i].aw-1)*20;
			}
			if(com[i].bt != -1)
			{
				com[i].cnt++;
				com[i].tot += com[i].bt + (com[i].bw-1)*20;
			}
			if(com[i].ct != -1)
			{
				com[i].cnt++;
				com[i].tot += com[i].ct + (com[i].cw-1)*20;
			}
		}
		sort(com+1, com+n+1, comp);
		for(int i=1; i<=n; i++)
			printf("%s %d %d\n", com[i].team, com[i].cnt, com[i].tot);
	}
	return 0;
}

J题 结构体排序题一(NEFU  OJ 1297)

#include 

using namespace std;

struct zb{
    int x,y;
}data[105];

// 降降
bool cmp1(const zb a,const zb b)
{
    if(a.x!=b.x)
        return a.x>b.x;
    else
        return a.y>b.y;
}

//降升
bool cmp2(const zb a,const zb b)
{
    if(a.x!=b.x)
        return a.x>b.x;
    else
        return a.yb.y;
}

int main()
{
    int tx,ty,n;
    while(cin >> tx >> ty >> n)
    {
        for(int i=1;i<=n;i++)
            cin >> data[i].x >> data[i].y;
        if(!tx&&!ty)
            sort(data+1,data+1+n,cmp1);
        if(!tx&&ty)
            sort(data+1,data+1+n,cmp2);
        if(tx&&ty)
            sort(data+1,data+1+n,cmp3);
        if(tx&&!ty)
            sort(data+1,data+1+n,cmp4);
        for(int i=1;i<=n;i++)
        {
            printf("(%d,%d)\n",data[i].x,data[i].y);

        }
    }
    return 0;
}

 

你可能感兴趣的:(大一寒假培训,NEFU,寒假培训)