PTA---7-41 PAT排名汇总(C语言)

7-41 PAT排名汇总

题目链接—点我

思路

  1. 首先我们将要存什么东西搞明白:1.学生考号 2.学生成绩 3.考场号
  2. 这里不存它的排名,因为我们可以用数组下标存就行
  3. 然后就是排序了,排序先按成绩排,然后成绩相同按照学号的非递减排序,这里我们采用快速排序,下面说下快排的思想

快速排序:
样例 ,对2 5 1 3 7 4排序
先确定一个基准数,就拿第一个2开始,然后在0~5之间前后判断(使得最后一次排序后,基准数后面的数都比它大,前面的数都比它小),这里记住,先要从后面判断(为什么自己想想吧Q_Q)
这样判断,如果我们是从大到小排序,那么我们后面的数要更大咯,如果后面数更大(比2大),那么就继续找,因为我们要找比它小的
1.现在我们从后面找比2小的,找到一个1,然后退出这个循环
2.从前面开始找,5比2大,找到,退出循环
3.然后由于此时还没相碰(前后找下标会变,退出整个循环时会相碰(下标相同)),交换5和1
重复1,2操作,现在发现从后面开始找的那个下标和从前面开始找的那个下标相撞了,退出整个循环
交换基准数2与退出循环的那个1,一次排序后,变为1 2 5 3 7 4,是不是让2左边的数比它小,右边的数比它大了呢。在这样重复递归(然后将2的左边排序,再将右边排序)
是不是很难理解呢?看看基本的代码就好了!

void quicksort(int l,int r,int *a)
{
	if(l >= r)
	return ;
	//基准数归位 
	int t = a[l];
	int i=l,j=r;
	while(i != j)
	{
		while(a[j] >= t and i < j)
		j--;
		while(a[i] <= t and i < j)
		i++;
		
		if(i<j)
		{
			int s = a[j];
			a[j] = a[i];
			a[i] = s;
		}
	}
	//归位 
	a[l] = a[i];
	a[i] = t;
	//cout<<i<<endl;
	
	quicksort(l,i-1,a);				//left
	quicksort(i+1,r,a);				//right
	return ;
} 
  1. 然后对于我们这题呢,需要做出一些小小的改动
  2. 对于成绩,那肯定先大后小,然后相同的话,就比较考号,这里考号是13位的数字(可以使用long long 或者 char *来存),这里我们选择后者
  3. 那么问题来了?怎么判断哪个考号大,哪个考号小?
    下面看一段程序
int bigger(const char *s1,const char *s2)
{
    for(int i=0;i<13;i++)
        if(s1[i] > s2[i])
            return 1;
        else if(s1[i] < s2[i])
        	return 0;
    return 1;
}

是不是很巧妙呢(这里的难点就是如果相等该返回哪个值,留给你们自己思考哈)

  1. 下面说下排序的条件,如果成绩相同,考号按递增排序,那么如果我们从后面找,满足成绩相同,如果考号比前面基准数小,才要排序吧(相反,比它大,继续找),这样就是退出第一个寻找循环的条件啦,下面一个从前往后找,一样的咯
    qsort代码
void qsort(int l,int r)
{
	if(l >= r)
	return ;
	
    int i = l;
    int j = r;
    
    struct stu t = a[l];
    while(i != j)
    {
        while(i < j && (a[j].score < t.score || a[j].score == t.score && bigger(a[j].id,t.id)))
            j--;
        //while(i < j and a[j].score == t.score and bigger(a[j].id,t.id))
            //j--;
        
        while(i < j && (a[i].score > t.score || a[i].score == t.score && bigger(t.id,a[i].id)))
            i++;
        //while(i < j and a[i].score == t.score and bigger(t.id,a[i].id))
            //i++;
        
        if(i < j)
        {
            struct stu s = a[i];
            a[i] = a[j];
            a[j] = s;
        }
    }
    a[l] = a[i];
    a[i] = t;
    
    qsort(l,i-1);
    qsort(i+1,r);
    
    return ;
}
  1. 然后就是总排名了,这里我们需要用两个变量存总排名,一个用来计算当前排名,一个用来存前一个排名(用于如果成绩相同排名一样的输出)

  2. 然后就是分区排名了,这个直接用两个数组来存,一个存当前所有区的排名情况,例如b1[2] = 2就代表2区的第一名已经有了,然后一个存前一个人排名(用于如果成绩相同排名一样的输出)

     		==到这,所有的思路就已经结束了.........==
    

33msAC代码(附有一大堆测试程序…)

#include <stdio.h>
#include <string.h>
struct stu
{
    char id[14];                //考号
    int score;                  //分数
    int kc;                     //考场
};
struct stu a[30000];
int bigger(const char *s1,const char *s2)
{
    for(int i=0;i<13;i++)
        if(s1[i] > s2[i])
            return 1;
        else if(s1[i] < s2[i])
        	return 0;
    return 1;
}
void qsort(int l,int r)
{
	if(l >= r)
	return ;
	
    int i = l;
    int j = r;
    
    struct stu t = a[l];
    while(i != j)
    {
        while(i < j && (a[j].score < t.score || a[j].score == t.score && bigger(a[j].id,t.id)))
            j--;
        //while(i < j and a[j].score == t.score and bigger(a[j].id,t.id))
            //j--;
        
        while(i < j && (a[i].score > t.score || a[i].score == t.score && bigger(t.id,a[i].id)))
            i++;
        //while(i < j and a[i].score == t.score and bigger(t.id,a[i].id))
            //i++;
        
        if(i < j)
        {
            struct stu s = a[i];
            a[i] = a[j];
            a[j] = s;
        }
    }
    a[l] = a[i];
    a[i] = t;
    
    qsort(l,i-1);
    qsort(i+1,r);
    
    return ;
}
void Copy(int *b2,int *b1,int n)
{
	for(int i=1;i<=n;i++)
	b2[i] = b1[i];
}
int main()
{
	///*
    int n,j,i,top = 0;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        int k;
        scanf("%d",&k);
        for(j=0;j<k;j++)
        {
            char id[14];
            int score;
            scanf("%s %d",id,&score);
            a[top].score = score;
            a[top].kc = i;
            strcpy(a[top].id,id);
            top++;
        }
    }
    //测试程序
    //for(int i=0;i<top;i++)
    //printf("%s %d\n",a[i].id,a[i].score);
	//puts("");	
    qsort(0,top-1);
    
    int levall = 1,b1[n+1],b2[n+1],score = a[0].score;
    
    for(i=1;i<=n;i++)
        b1[i] = 1,b2[i] = 1;
    ///* 
    printf("%d\n",top);
    printf("%s %d %d %d\n",a[0].id,1,a[0].kc,1);
    int llevall = 1;            //上一个总排名
    levall = 2;                   //总排名
    
    Copy(b2,b1,n);
    b1[a[0].kc]++;	
    for(i=1;i<top;i++)
    {
        if(a[i].score == a[i-1].score)
        {
            printf("%s %d %d %d\n",a[i].id,llevall,a[i].kc,b2[a[i].kc]);
            levall++;
            b1[a[i].kc]++;
        }
        else
        {
            printf("%s %d %d %d\n",a[i].id,levall,a[i].kc,b1[a[i].kc]);
            llevall = levall;
            levall++;
            
		    Copy(b2,b1,n);
		    b1[a[i].kc]++;					//考场的排名 
        }
    }
    //*/ 
    //测试样例
    /*
    strcpy(a[0].id,"1234567890005");
    a[0].kc = 1;
    a[0].score = 100;
    
    strcpy(a[1].id,"1234567890014");
    a[1].kc = 2;
    a[1].score = 100;
    
    qsort(0,1);
    for(int i=0;i<2;i++)
    printf("%s %d\n",a[i].id,i);
    */
    //排序后的测试程序
    //for(int i=0;i<top;i++)
    //printf("%s %d\n",a[i].id,a[i].score);
    return 0;
}
  • 12
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集 PTA-数据结构与算法题目集
根据引用,C语言代码pta7-1可以实现输入一个1到7的数字,输出对应的星期名的缩写。以下是一个实现该功能的C语言代码示例: ``` #include <stdio.h> int main() { int n; char* s[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; printf("请输入一个数字(1-7):"); scanf("%d", &n); printf("%s\n", s[n-1]); return 0; } ``` 这段代码会提示用户输入一个1到7的数字,然后根据输入的数字输出对应的星期名的缩写。例如,输入1会输出"Mon",输入2会输出"Tue",以此类推。注意,由于数组的索引是从0开始的,所以我们需要将输入的数字减去1来得到正确的索引值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [PTA 第五章算法题汇总](https://blog.csdn.net/billlee7942/article/details/106250002)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [PTA-MOOC《Python程序设计浙江大学》拼题题目集第五章编程题题目及代码答案](https://blog.csdn.net/weixin_45115928/article/details/104516791)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值