【数据结构 | PTA】排序

7-1 选择法排序

本题要求将给定的n个整数从大到小排序后输出。

输入格式:

输入第一行给出一个不超过10的正整数n。第二行给出n个整数,其间以空格分隔。

输出格式:

在一行中输出从大到小有序的数列,相邻数字间有一个空格,行末不得有多余空格。

输入样例:

4
5 1 7 6

输出样例:

7 6 5 1
#include<stdio.h>

int main(void) {
    int n;
    scanf("%d", &n);

    int array[10];
    for (int i = 0; i < n; i++) {
        scanf("%d", &array[i]);
    }

    // n 个数据,选择排序 n-1 次即可得出排序结果
    for (int i = 0; i < n - 1; i++) {
        // 从剩下的 n-i 个元素中查找,如果存在比 array[i] 大的数,则交换它们的值
        for (int j = i + 1; j < n; j++) {
            if (array[j] > array[i]) {
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }

    for (int i = 0; i < n; i++) {
        printf("%d", array[i]);
        // 相邻数字间有一个空格,行末不得有多余空格
        if (i != n - 1) {
            printf(" ");
        }
    }

    return 0;
}

7-2 冒泡法排序

N个整数按从小到大排序的冒泡排序法是这样工作的:从头到尾比较相邻两个元素,如果前面的元素大于其紧随的后面元素,则交换它们。通过一遍扫描,则最后一个元素必定是最大的元素。然后用同样的方法对前N−1个元素进行第二遍扫描。依此类推,最后只需处理两个元素,就完成了对N个数的排序。

本题要求对任意给定的K(<N),输出扫描完第K遍后的中间结果数列。

输入格式:
输入在第1行中给出NK(1≤K<N≤100),在第2行中给出N个待排序的整数,数字间以空格分隔。

输出格式:
在一行中输出冒泡排序法扫描完第K遍后的中间结果数列,数字间以空格分隔,但末尾不得有多余空格。

输入样例:

6 2
2 3 5 1 6 4

输出样例:

2 1 3 4 5 6
#include <stdio.h>
int main()
{
    int a[100];
    int i,j,t;
    int N,K;
    scanf("%d%d",&N,&K);
    for(j=0; j<N; j++)
    {
        scanf("%d",&a[j]);
    }
    for(i=0; i<K; i++)
    {
        for(j=0; j<N-1; j++)
        {
            if(a[j]>a[j+1])
            {
                t=a[j];
                a[j]=a[j+1];
                a[j+1]=t;
            }
        }
    }
    for(j=0; j<N-1; j++)
    {
        printf("%d",a[j]);
        printf(" ");
    }
    printf("%d",a[N-1]);
    return 0;
}

7-3 两个有序链表序列的合并

已知两个非降序链表序列S1与S2,设计函数构造出S1与S2合并后的新的非降序链表S3。

输入格式:

输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。

输出格式:

在一行中输出合并后新的非降序链表,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL

输入样例:

1 3 5 -1
2 4 6 8 10 -1

输出样例:

1 2 3 4 5 6 8 10

在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
struct Node
{
	int dat;
	struct Node *next;
};
typedef struct Node node;
node *headp;
node *make_new_node(node *head)
{
	node *p;
	p=(node*)malloc(sizeof(node));
	head=p;
	p->next=NULL;
	return head;
}
int get_merge(node *head1,node *head2)
{
	node *pos1,*pos2,*headl;
	if(head1->next==NULL&&head2->next==NULL)
	{
		return 0;//输出null;
	}
	if(head1->next==NULL||head2->next==NULL)
	{
		return 1;//只需输出一个链表的值
	}
	pos1=(node*)malloc(sizeof(node));
	headl=pos1;
	headp=headl;
	head1=head1->next;
	head2=head2->next;
	while(head1&&head2)
	{
		if(head1->dat<head2->dat)
		{
			headl->next=head1;
			head1=head1->next;
		}
		else
		{
			headl->next=head2;
			head2=head2->next;
		}
		headl=headl->next;
	}
	if(!head1&&!head2) return 2;
	if(head1!=NULL)
	{
		headl->next=head1;
	}
	if(head2!=NULL)
	{
		headl->next=head2;
	}
	return 2;
}
void dis_link(node *head)
{

	node *p=head->next,*pr;
	while(p->next!=NULL)
	{
		printf("%d ",p->dat);
		p=p->next;
	}
	printf("%d\n",p->dat);
}
void free_node(node *head)
{
	node *p;
	node *pr=head;
	while(pr->next!=NULL)
	{
		p=pr->next;
		free(pr);
		pr=p;
	}
	free(pr);
}
int main(void)
{
	node *head1=NULL,*head2=NULL;
	head1=make_new_node(head1);
	head2=make_new_node(head2);
	int data;
	node *ll=head1;
	while(scanf("%d",&data),data!=-1)
	{
		node *tep;
		tep=(node*)malloc(sizeof(node));
		ll->next=tep;
		tep->next=NULL;
		tep->dat=data;
		ll=tep;
	}
	ll=head2;
	while(scanf("%d",&data),data!=-1)
	{
		node *tep;
		tep=(node*)malloc(sizeof(node));
		ll->next=tep;
		tep->next=NULL;
		tep->dat=data;
		ll=tep;
	}
	int flag=get_merge(head1,head2);
	if(flag==1)
	{
		if(head1==NULL)
		{
			dis_link(head2);
			free_node(head2);
		}
		else
		{
			dis_link(head1);
			free_node(head1);
		}
	}
	else if(flag==0)
	{
		printf("NULL\n");
		free_node(head1);
		free_node(head2);
	}
	else
	{
		dis_link(headp);
		free_node(headp);
	}
	return 0;
}

7-4 PAT排名汇总

计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科学的评价计算机程序设计人才,为企业选拔人才提供参考标准(网址http://www.patest.cn)。

每次考试会在若干个不同的考点同时举行,每个考点用局域网,产生本考点的成绩。考试结束后,各个考点的成绩将即刻汇总成一张总的排名表。

现在就请你写一个程序自动归并各个考点的成绩并生成总排名表。

输入格式:
输入的第一行给出一个正整数N(≤100),代表考点总数。随后给出N个考点的成绩,格式为:首先一行给出正整数K(≤300),代表该考点的考生总数;随后K行,每行给出1个考生的信息,包括考号(由13位整数字组成)和得分(为[0,100]区间内的整数),中间用空格分隔。

输出格式:
首先在第一行里输出考生总数。随后输出汇总的排名表,每个考生的信息占一行,顺序为:考号、最终排名、考点编号、在该考点的排名。其中考点按输入给出的顺序从1到N编号。考生的输出须按最终排名的非递减顺序输出,获得相同分数的考生应有相同名次,并按考号的递增顺序输出。

输入样例:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

输出样例:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4
#include<iostream>
#include<vector>
#include<cstdio>
#include<set>
#include<map>
#include<string>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<ctype.h>
#include<stack>
#include<queue>
#include<list>
#include<time.h>
#include<sstream>
#define random(x) (rand()%x)
using namespace std;
 
struct Person{
    string id;      //考号
    int sorce;      //成绩
    int groupID;    //所在组
    int groupPM;    //组排名
    int allPM;      //总排名
}per[30005];
 
bool cmp(Person a,Person b){
    if(a.sorce==b.sorce)    //成绩相同按考号排名
        return a.id<b.id;
    return a.sorce>b.sorce; //成绩不同按成绩排名
}
 
int main(){
    int n;
    int sum=0;  //不断更新per的储存长度
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        for(int j=sum;j<sum+x;j++){
            cin>>per[j].id>>per[j].sorce;
            per[j].groupID=i;    //p[j]号学生组号为i
        }
        sort(per+sum,per+sum+x,cmp);    //组排序
        per[sum].groupPM=1;    //组中第一个人排名置为1
        for(int k=1+sum;k<x+sum;k++){    //确定各组中每个人排名
            if(per[k].sorce==per[k-1].sorce){    //相同的分数有相同的排名,就是并列
                per[k].groupPM = per[k-1].groupPM;
            }
            else{
                per[k].groupPM=k-sum+1;    //分数不同则排名+1
            }
        }
        sum+=x;    //更新per[]实际长度
    }
    //总排名和组排名原理相同
    sort(per,per+sum,cmp);
    per[0].allPM=1;
    for(int i=1;i<sum;i++){
        if(per[i].sorce==per[i-1].sorce){
            per[i].allPM=per[i-1].allPM;
        }
        else{
            per[i].allPM=i+1;
        }
    }
    cout<<sum<<endl;
    for(int i=0;i<sum;i++){
        cout<<per[i].id<<" "<<per[i].allPM<<" "<<per[i].groupID<<" "<<per[i].groupPM<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋说

感谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值