• HDU ACMSTEPS 1.3.4


    最先看到这题,犹豫了下,发现这题的输入需要做比较复杂的字符串处理。

    随后还是编了代码出来。

    版本1:AC。

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 
      5 #define STU_MAX 1000
      6 #define NAME_LEN 11
      7 #define TIME_LEN 11
      8 
      9 #ifdef DEBUG
     10 #include <assert.h>
     11 #endif
     12 
     13 typedef struct Student {
     14     
     15     char name[NAME_LEN];
     16     int acnum;
     17     int time;
     18 
     19 };
     20 
     21 int cmp(const void* a, const void* b) 
     22 {
     23     Student* stu1 = (Student*)a;
     24     Student* stu2 = (Student*)b;
     25 
     26     if (stu1->acnum != stu2->acnum)
     27         return (stu2->acnum - stu1->acnum);
     28     if (stu1->acnum == stu2->acnum && stu1->time != stu2->time)
     29         return (stu1->time - stu2->time);
     30     return strcmp(stu1->name,stu2->name); 
     31 }
     32 
     33 int main(int argc, char* argv[])
     34 {
     35 
     36     //topic number & punish score
     37     int topic_num,score_minus;
     38 
     39     scanf("%d %d",&topic_num,&score_minus);
     40 
     41     //malloc space for storage
     42     Student *stud_list = (Student*)malloc(sizeof(Student)*STU_MAX);
     43     memset(stud_list,0,sizeof(Student)*STU_MAX);
     44 
     45     //asume that only six student
     46     int stu_cnt = 0;
     47 
     48     char name[NAME_LEN] = {0};
     49     
     50     for (; scanf("%s",name) != EOF; stu_cnt++) {
     51         
     52         memcpy(stud_list[stu_cnt].name,name,strlen(name));
     53 
     54         //malloc two division of array
     55         //for topic cost time storage
     56         char** topics = (char**)malloc(sizeof(char*)*topic_num);
     57         memset(topics,0,sizeof(char*)*topic_num);
     58         
     59         for(int j=0; j<topic_num; j++) {
     60 
     61             //storage the input cost time for
     62             //every topic
     63             topics[j] = (char*)malloc(sizeof(char)*TIME_LEN);
     64             memset(topics[j],0,sizeof(char)*TIME_LEN);
     65             
     66             scanf("%s",topics[j]);
     67         }
     68 
     69         //cnt ac number
     70         //cnt cost time
     71         int ac_cnt = 0;
     72         int ac_cost = 0;
     73 
     74         for (int j=0; j<topic_num; j++) {
     75 
     76             //find braket
     77             char* start_braket = NULL;
     78             char* end_braket = NULL;
     79 
     80             start_braket = strstr(topics[j],"(");
     81             if (start_braket != topics[j]) {
     82                 end_braket = strstr(topics[j],")");
     83             }
     84 
     85             //deal ac time with braket
     86             if (start_braket != NULL && end_braket != NULL) {
     87 
     88                 //deal punish time
     89                 char* sub_tmp = (char*)malloc(sizeof(char)*TIME_LEN);
     90                 memset(sub_tmp,0,sizeof(char)*TIME_LEN);
     91                 memcpy(sub_tmp,start_braket+1,strlen(start_braket)-strlen(end_braket)-1);
     92                 int minus_time = atoi(sub_tmp)*score_minus;
     93 
     94                 //deal ac time
     95                 memset(sub_tmp,0,sizeof(char)*TIME_LEN);
     96                 memcpy(sub_tmp,topics[j],strlen(topics[j])-strlen(start_braket));
     97                 int ac_time = atoi(sub_tmp);
     98 
     99                 //free memory
    100                 free(sub_tmp);
    101 
    102                 ac_cnt++;
    103                 ac_cost += ac_time+minus_time;
    104 
    105             } else {
    106                 int ac_time = atoi(topics[j]);
    107                 if(ac_time > 0) {
    108                     ac_cnt++;
    109                     ac_cost += ac_time;
    110                 }
    111             }
    112         }
    113         
    114         stud_list[stu_cnt].acnum = ac_cnt;
    115         stud_list[stu_cnt].time = ac_cost;
    116 
    117         //free memory
    118         free(topics);
    119 
    120     }
    121 
    122     //sort the student
    123     qsort(stud_list,stu_cnt,sizeof(Student),cmp);
    124     
    125     //print result
    126     for (int i = 0; i < stu_cnt; ++i) {
    127         printf("%-10s %2d %4d
    ", stud_list[i].name,stud_list[i].acnum,stud_list[i].time);
    128     }
    129 
    130     //free memory
    131     free(stud_list);
    132 
    133     return 0;
    134 }

    这个版本的执行耗时为 0MS,执行内存为 900K,略大。 代码大小为 2943 字节。

    后来发现,在输入的处理上,可以用sscanf来代替。遂,重写一个版本。

    版本2:AC。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 
     5 #define STU_MAX 1000
     6 #define NAME_LEN 11
     7 
     8 typedef struct Student{
     9     char name[NAME_LEN];
    10     int num;
    11     int cost;
    12 };
    13 
    14 int cmp(const void *a, const void *b)
    15 {
    16     Student* stu1 = (Student*)a;
    17     Student* stu2 = (Student*)b;
    18     if (stu1->num != stu2->num) 
    19         return (stu2->num - stu1->num);
    20     if (stu1->cost != stu2->cost)
    21         return (stu1->cost - stu2->cost);
    22     return strcmp(stu1->name,stu2->name);
    23 }
    24 
    25 int main(int argc, char** argv)
    26 {
    27 
    28     int n,m;
    29     scanf("%d %d",&n,&m);
    30 
    31     char name[NAME_LEN] = {0};
    32 
    33     int stuindex = 0;
    34 
    35     Student* stus = (Student*)malloc(sizeof(Student)*STU_MAX);
    36     memset(stus,0,sizeof(Student)*STU_MAX);
    37 
    38     while(scanf("%s",name) != EOF) {
    39 
    40         memcpy(stus[stuindex].name,name,strlen(name));
    41 
    42         for(int i=0; i<n; i++) {
    43             
    44             int t,d;
    45             
    46             char *time = (char*)malloc(sizeof(char)*20);
    47             scanf("%s",time);
    48 
    49             int res = sscanf(time,"%d(%d)",&t,&d);
    50             if (res == 1 && t > 0) {
    51                 stus[stuindex].num++;
    52                 stus[stuindex].cost += t;
    53             } else if (res == 2) {
    54                 stus[stuindex].num++;
    55                 stus[stuindex].cost += t+d*m;
    56             }
    57         }
    58 
    59         stuindex++;
    60 
    61     }
    62 
    63     //score stus!
    64     qsort(stus,stuindex,sizeof(Student),cmp);
    65 
    66     //print
    67     for (int i = 0; i < stuindex; ++i) {
    68         printf("%-10s %2d %4d
    ", stus[i].name,stus[i].num,stus[i].cost);
    69     }
    70 
    71     return 0;
    72 }

    代码确实简洁不少,而且不用处理复杂的字符串格式。

    这个版本的代码,执行耗时为 15MS,执行内存为290K,正常。 代码大小为 1372 字节。

    2个版本,各有优劣吧。 从编码复杂度来说,版本2更为合适。

  • 相关阅读:
    数据存储 twisted
    数据存储 mongodb
    数据存储 redis
    数据存储 txt
    同时使用有线内网与无线外网
    使用xshell从服务器下载文件
    everything使用技巧
    【吴恩达机器学习】第8章 正则化
    python文件重命名
    【统计学习】 第5章 决策树
  • 原文地址:https://www.cnblogs.com/coddingfun/p/3321989.html
Copyright © 2020-2023  润新知