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.
Slightly changed default code. Don't forget free ALL allocated memory if you use something similar in your projects
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<stdbool.h>#define PACKAGE_NAME_MAX_LENGTH 6#define TOWN_NAME_MAX_LENGTH 6typedefstruct{char*id;intweight;}Package;typedefstruct{intmin_weight;intmax_weight;Package*packages;intpackages_count;}PostOffice;typedefstruct{char*name;PostOffice*offices;intoffices_count;}Town;Packagecreate_package(){Packagepackage;package.id=(char*)malloc(PACKAGE_NAME_MAX_LENGTH*sizeof(char));scanf("%s\n%d",package.id,&package.weight);returnpackage;}PostOfficecreate_post_office(){PostOfficepost_office;scanf("%d %d %d",&post_office.packages_count,&post_office.min_weight,&post_office.max_weight);post_office.packages=(Package*)malloc((post_office.packages_count+50)*sizeof(Package));returnpost_office;}Towncreate_town(){Towntown;town.name=(char*)malloc(TOWN_NAME_MAX_LENGTH*sizeof(char));scanf("%s",town.name);scanf("%d",&town.offices_count);town.offices=(PostOffice*)malloc(town.offices_count*sizeof(PostOffice));returntown;}voidsend_all_acceptable_packages(constTown*source,constintsource_office_index,Town*target,constinttarget_office_index);voidadd_package_to_office(PostOffice*office,constPackagepackage);Town*town_with_most_packages(constTown*towns,constinttowns_count);Town*find_town(constTown*towns,constinttowns_count,constchar*town_name);boolis_package_acceptable(constPostOffice*target_office,constPackage*package);voidprint_all_packages(constTown*town);// ----------------- .h file above -----------------voidsend_all_acceptable_packages(constTown*source,constintsource_office_index,Town*target,constinttarget_office_index){PostOffice*target_office=&target->offices[target_office_index];Packagecurrent_package;intsource_office_packages=source->offices[source_office_index].packages_count;Package*new_office_packages=(Package*)malloc(source_office_packages*10*sizeof(Package));intnew_office_packages_count=0;for(intpackage=0;package<source_office_packages;package++){current_package=source->offices[source_office_index].packages[package];if(is_package_acceptable(target_office,¤t_package)){add_package_to_office(target_office,current_package);}else{new_office_packages[new_office_packages_count]=current_package;new_office_packages_count++;}}target_office=NULL;source->offices[source_office_index].packages_count=new_office_packages_count;source->offices[source_office_index].packages=new_office_packages;}voidadd_package_to_office(PostOffice*office,constPackagepackage){intpackages_in_office=office->packages_count;office->packages[packages_in_office]=package;office->packages_count++;}Town*town_with_most_packages(constTown*towns,constinttowns_count){Town*town_with_most_packages=NULL;intmax_packages=0;intpackages_in_town;intoffices_count;for(inttown=0;town<towns_count;town++){packages_in_town=0;offices_count=towns[town].offices_count;for(intoffice=0;office<offices_count;office++){packages_in_town+=towns[town].offices[office].packages_count;}if(packages_in_town>max_packages){town_with_most_packages=&towns[town];max_packages=packages_in_town;}}returntown_with_most_packages;}Town*find_town(constTown*towns,constinttowns_count,constchar*town_name){for(inttown=0;town<towns_count;town++){if(!strcmp(towns[town].name,town_name)){return&towns[town];}}returnNULL;}boolis_package_acceptable(constPostOffice*target_office,constPackage*package){returnpackage->weight>=target_office->min_weight&&package->weight<=target_office->max_weight;}voidprint_all_packages(constTown*town){printf("%s:\n",town->name);for(intoffice=0;office<town->offices_count;office++){printf("\t%d:\n",office);for(intpackage=0;package<town->offices[office].packages_count;package++){printf("\t\t\%s\n",town->offices[office].packages[package].id);}}}intmain(){inttowns_count;intoffices_count;intpackages_count;Town*source_town=NULL;Town*target_town=NULL;chartarget_town_name[TOWN_NAME_MAX_LENGTH];charsource_town_name[TOWN_NAME_MAX_LENGTH];inttarget_index;intsource_index;intqueries_count;intquery_type;scanf("%d",&towns_count);Town*towns=(Town*)malloc(towns_count*sizeof(Town));for(inttown=0;town<towns_count;town++){towns[town]=create_town();offices_count=towns[town].offices_count;for(intoffice=0;office<offices_count;office++){towns[town].offices[office]=create_post_office();packages_count=towns[town].offices[office].packages_count;for(intpackage=0;package<packages_count;package++){towns[town].offices[office].packages[package]=create_package();}}}scanf("%d",&queries_count);while(queries_count--){scanf("%d",&query_type);switch(query_type){case1:scanf("%s",source_town_name);source_town=find_town(towns,towns_count,source_town_name);print_all_packages(source_town);break;case2:scanf("%s %d",source_town_name,&source_index);scanf("%s %d",target_town_name,&target_index);source_town=find_town(towns,towns_count,source_town_name);target_town=find_town(towns,towns_count,target_town_name);send_all_acceptable_packages(source_town,source_index,target_town,target_index);break;case3:printf("Town with the most number of packages is %s\n",town_with_most_packages(towns,towns_count)->name);break;}}returnEXIT_SUCCESS;}
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 →
Slightly changed default code. Don't forget free ALL allocated memory if you use something similar in your projects