简单字符串排序

简单字符串排序
Time Limit:5000MS     Memory Limit:100000KB     64bit IO Format:%lld & %llu

Description

从键盘输入10个学生的姓名和成绩,请按字典序排列学生的姓名并输出(姓名和成绩对应关系保持不变)。

Input

输入共11行,前10行每行是一个学生的姓名,最后一行是10个用空格分开的整数表示对应的10个学生成绩。

Output

输出姓名按字典序排列后的学生姓名和成绩,共10行,每个学生的姓名和成绩占一行,姓名和成绩间用逗号分开。

Sample Input

Bush
White
Mark
Jean
Black
Wood
Jenny
Frank
Bill
Smith
78 85 96 65 46 83 77 88 54 98

Sample Output

Bill,54
Black,46
Bush,78
Frank,88
Jean,65
Jenny,77
Mark,96
Smith,98
White,85
Wood,83
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
using namespace std;
struct student
{
    char str[20];
    int st;
}stud[10],t;
int cmp(student a,student b)
{
    int i;
    for(i=0;i<10;i++)
    {
        if(a.str[i]>b.str[i])
        return 0;
        else if(a.str[i]<b.str[i])
        return 1;
    }
}
int main()
{
    int i,j;
    for(i=0;i<10;i++)
    {
        scanf("%s",&stud[i].str);
    }
    for(j=0;j<10;j++)
    {
        scanf("%d",&stud[j].st);
    }
    for(i=0;i<10;i++)
    sort(stud,stud+10,cmp);
    for(i=0;i<10;i++)
    {
        printf("%s,%d\n",stud[i].str,stud[i].st);
    }
    return 0;
}
</pre><pre name="code" class="cpp">
</pre><pre name="code" class="cpp">#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
struct student
{
    char str[20];
    int st;
}stud[10],t;
int main()
{
    int i,j;
    for(i=0;i<10;i++)
    {
        scanf("%s",&stud[i].str);
    }
    for(j=0;j<10;j++)
    {
        scanf("%d",&stud[j].st);
    }
    for(i=0;i<10;i++)
    for(j=0;j<9-i;j++)
    {
        if(strcmp(stud[j].str,stud[j+1].str)>0)
        {
            t=stud[j];
            stud[j]=stud[j+1];
            stud[j+1]=t;
        }
    }
    for(i=0;i<10;i++)
    {
        printf("%s,%d\n",stud[i].str,stud[i].st);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值