[Grids2418] hardwood species solution report + code

Source: Internet
Author: User
# Include <cstring> # include <cstdio> # include <cstdlib> # include <iostream> // # define input/** problem: grids2418-Hardwood Species begin time: P. m. 29th/2/2012 end time: 21:35:04 cost time: 2 hours 5 mins idea: it is a process of simulating Map <string, int>. For details, see the following for each tree 1, the corresponding tree is found in the binary search. Count ++ 2) does not exist, insert a node // remember to record a pointer p pointing to the current array, which is also the number of different types of trees, however, there is nothing to do here. The total number of trees must be of the double type. For the output, remember that G ++ contains % f or above */using. Namespace STD, name and count are available. Count ++ after name is found each time; // function 1: findele (string name): bool // function 2: insertele (string name) /// Function 3: sortspecies (), which is sorted by string and should be sorted by the base number! /// Function 4: increele (string name); // function 5: iseql (string, string) /// this is probably the case of const int max_size = 10010; struct _ tree {int count; char name [35] ;}; _ tree trees [max_size]; int p_trees = 0; int findele (char * Name, int range ); void insertele (char * Name, int range); void increele (int p); void sortele (INT range); int main (INT argc, char * argv []) {char Buf [35]; double Total = 0; int range = 0; // freopen ("B: \ ACM \ grids2418 \ In Put.txt "," r ", stdin); While (gets (BUF )! = NULL) {int K = findele (BUF, p_trees); If (k =-1) {insertele (BUF, p_trees); sortele (p_trees );} else increele (k); Total + = 1 ;}for (INT I = 0; I <p_trees; I ++) {printf ("% S %. 4f \ n ", trees [I]. name, trees [I]. count * 100/total);} return 0;} int comp (const void * a, const void * B) {_ tree node1, node2; int K = 0; node1 = * (_ tree *) A; node2 = * (_ tree *) B; k = strcmp (node1.name, node2.name); Return K;} void sortele (INT range) {// qsort (trees, range, sizeof (_ tree), comp);} void increele (INT p) {trees [p]. count ++;} void insertele (char * Name, int range) {// trees [p_trees]. name = Name; strcpy (trees [p_trees]. name, name); trees [p_trees]. count = 1; p_trees ++;} int iseql (const char * name1, const char * name2) {int K = strcmp (name1, name2); Return K; /// that // The, the latter E> A, so the> that} int findele (char * Name, int range) {// binary search, in trees, find int L = 0, r = range; int mid = (L + r)/2; int res =-1; while (L <= r) {int T = iseql (trees [Mid]. name, name); If (T> 0) // trees [Mid]. name> name {r = mid-1; Mid = (L + r)/2;} If (T <0) // trees [Mid]. name <name {L = Mid + 1; Mid = (L + r)/2;} If (t = 0) {res = mid; break ;}} return res ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.