We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAX_STRING_LENGTH 6structpackage{char*id;intweight;};typedefstructpackagepackage;structpost_office{intmin_weight;intmax_weight;package*packages;intpackages_count;};typedefstructpost_officepost_office;structtown{char*name;post_office*offices;intoffices_count;};typedefstructtowntown;voidprint_all_packages(townt){printf("%s:\n",t.name);for(inti=0;i<t.offices_count;i++){printf("\t%d:\n",i);for(intoffice=0;office<t.offices[i].packages_count;office++){printf("\t\t%s\n",t.offices[i].packages[office].id);}}}voidsend_all_acceptable_packages(town*source,intsource_office_index,town*target,inttarget_office_index){post_office*src=source->offices+source_office_index;post_office*src_1=target->offices+target_office_index;packagetemple[src->packages_count];inttemple_size=0;for(inti=0;i<src->packages_count;){if(src->packages[i].weight>=src_1->min_weight&&src->packages[i].weight<=src_1->max_weight){temple[temple_size++]=src->packages[i];for(intj=i;j<src->packages_count-1;j++){src->packages[j]=src->packages[j+1];}src->packages_count--;}elsei++;}src_1->packages=realloc(src_1->packages,(src_1->packages_count+temple_size)*sizeof(package));for(inti=0;i<temple_size;i++){src_1->packages[src_1->packages_count++]=temple[i];}}towntown_with_most_packages(town*towns,inttowns_count){towns_max=*towns;intmax=0;for(inti=0;i<towns->offices_count;i++){max+=towns->offices[i].packages_count;}towns++;towns_count--;while(towns_count){intsum=0;for(inti=0;i<towns->offices_count;i++){sum+=towns->offices[i].packages_count;}if(sum>max){max=sum;s_max=*towns;}towns++;towns_count--;}returns_max;}town*find_town(town*towns,inttowns_count,char*name){while(towns_count){if(!strcmp(towns->name,name)){returntowns;}towns++;towns_count--;}returnNULL;}intmain(){inttowns_count;scanf("%d",&towns_count);town*towns=malloc(sizeof(town)*towns_count);for(inti=0;i<towns_count;i++){towns[i].name=malloc(sizeof(char)*MAX_STRING_LENGTH);scanf("%s",towns[i].name);scanf("%d",&towns[i].offices_count);towns[i].offices=malloc(sizeof(post_office)*towns[i].offices_count);for(intj=0;j<towns[i].offices_count;j++){scanf("%d%d%d",&towns[i].offices[j].packages_count,&towns[i].offices[j].min_weight,&towns[i].offices[j].max_weight);towns[i].offices[j].packages=malloc(sizeof(package)*towns[i].offices[j].packages_count);for(intk=0;k<towns[i].offices[j].packages_count;k++){towns[i].offices[j].packages[k].id=malloc(sizeof(char)*MAX_STRING_LENGTH);scanf("%s",towns[i].offices[j].packages[k].id);scanf("%d",&towns[i].offices[j].packages[k].weight);}}}intqueries;scanf("%d",&queries);chartown_name[MAX_STRING_LENGTH];while(queries--){inttype;scanf("%d",&type);switch(type){case1:scanf("%s",town_name);town*t=find_town(towns,towns_count,town_name);print_all_packages(*t);break;case2:scanf("%s",town_name);town*source=find_town(towns,towns_count,town_name);intsource_index;scanf("%d",&source_index);scanf("%s",town_name);town*target=find_town(towns,towns_count,town_name);inttarget_index;scanf("%d",&target_index);send_all_acceptable_packages(source,source_index,target,target_index);break;case3:printf("Town with the most number of packages is %s\n",town_with_most_packages(towns,towns_count).name);break;}}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Post Transition
You are viewing a single comment's thread. Return to all comments →
just include