Work in class-find the Navy

Source: Internet
Author: User

Topic:

Ideas:

The beauty of programming expands this problem by complementing the analysis on the basis of finding a "water king", before traversing the array with only two values: one is the ID in the array, and the other is the number of occurrences. This time we need to find out 3 IDs (that is, output 3 results), you need to open up a new array to save separately. First of all, save the first 3 IDs, each time you traverse the next ID, the first to determine whether the next and the current saved 3 IDs have the same, if there is the same ID corresponding to the number of times plus 1, if the number of different times minus 1. Again, if there is an ID number of zero, replace it with the next ID and set the number to 1. Unlike before, the number of occurrences may not be 0, and all the times are reduced by 1.

Source:

1#include <iostream.h>2 #defineN 163 classType4 {5      Public:6 Type ()7     {8Count=0;9id=-1;Ten     } One     intcount; A     intID; - }; - intMain () the { -         intid[n]={7,Ten,7,4,5,5,Ten,7,7,7,Ten,Ten,5,5,Ten,4};  - Type candidate1; - Type Candidate2; + Type candidate3; -     inti; +      for(i=0; i<n;i++) A     { at         if(id[i]==candidate1.id) -                 { -candidate1.count++; -                 } -         Else if(id[i]==candidate2.id) -                 { incandidate2.count++; -                 } to         Else if(id[i]==candidate3.id) +                 { -candidate3.count++; the                 } *         Else if(candidate1.count==0) $         {Panax NotoginsengCandidate1.id=Id[i]; -candidate1.count++; the         } +         Else if(candidate2.count==0) A         { theCandidate2.id=Id[i]; +candidate2.count++; -         } $         Else if(candidate3.count==0) $         { -Candidate3.id=Id[i]; -candidate3.count++; the         } -         ElseWuyi         { thecandidate1.count--; -candidate2.count--; Wucandidate3.count--; -         } About     } $cout<<candidate1.id<<Endl; -cout<<candidate2.id<<Endl; -cout<<candidate3.id<<Endl; -     return 0; A } +                 
View Code

Operation Result:

Summarize:

Because also to consider the time complexity, so in accordance with the last code to modify, each time the current saved three IDs respectively expand (If...else), and the next ID to compare, in its corresponding number of operations plus, minus.

See useful classes and structs on the forums to define new arrays so that you don't iterate through new arrays every time you compare them, shortening the complexity of time. Long time did not involve, now see and recall some, warm so know new, also know when can use what structure and method.

Work in class-find the Navy

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.