zoukankan      html  css  js  c++  java
  • hdu 5427 A problem of sorting(字符排序)

    Problem Description
    There are many people's name and birth in a list.Your task is to print the name from young to old.(There is no pair of two has the same age.)
    Input
    First line contains a single integer T≤100 which denotes the number of test cases. 
    
    For each test case, there is an positive integer n(1≤n≤100) which denotes the number of people,and next n lines,each line has a name and a birth's year(1900-2015) separated by one space.
    
    The length of name is positive and not larger than 100.Notice name only contain letter(s),digit(s) and space(s).
    Output
    For each case, output n lines.
     
    Sample Input
    2 
    1 
    FancyCoder 1996 
    2 
    FancyCoder 1996 
    xyz111 1997
     
    Sample Output
    FancyCoder 
    xyz111 
    FancyCoder
     
    Source
     
    具体看代码
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<stdlib.h>
     6 #include<cmath>
     7 using namespace std;
     8 #define N 106
     9 int n;
    10 struct Node{
    11     int year;
    12     char ch[106];
    13 }person[N];
    14 bool cmp(Node a,Node b){
    15     return a.year>b.year;
    16 }
    17 int main()
    18 {
    19     int t;
    20     scanf("%d",&t);
    21     while(t--){
    22         scanf("%d",&n);
    23         char s[150];
    24         getchar();
    25         for(int i=0;i<n;i++){
    26             person[i].year=0;
    27             gets(s);
    28             //puts(s);
    29             int len=strlen(s);
    30             for(int j=0;j<len-5;j++){
    31                 person[i].ch[j]=s[j];
    32             }
    33             person[i].ch[len-5]='';//记得加,否则wa
    34 
    35             for(int j=len-4;j<len;j++){
    36                 person[i].year=person[i].year*10+s[j]-'0';
    37             }
    38             //printf("%s
    ",person[i].ch);
    39             //printf("%d
    ",person[i].year);
    40         }
    41         sort(person,person+n,cmp);
    42         for(int i=0;i<n;i++){
    43             printf("%s
    ",person[i].ch);
    44         }
    45     }
    46 
    47     return 0;
    48 }
    View Code
     
  • 相关阅读:
    转载集合
    TYVJ P1053 字符串的展开 Label:字符 水
    划分数系列问题
    关于inf的问题
    TYVJ P1031 热浪 Label:dijkstra 最短路
    TYVJ P1032 零用钱 Label:贪心
    如何简单形象又有趣地讲解神经网络是什么?知乎
    CString
    利用afxDump来调试自己的程序
    mfc 调试 弹消息
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4784656.html
Copyright © 2011-2022 走看看