Software engineering with little work--looking for "buckets" (c + +)

Source: Internet
Author: User

First, design ideas

The idea is similar to looking for a water king, this time just counter and suspect variables are set to an array. Each time you select an ID compared to three suspects, if there is the same count, if three are not the same, three counters are reduced by one. If it is reduced to 0, the new value is given to the suspect.

Second, the source code

1 //bucket. Cpp:defines The entry point for the console application.2 //3 4#include"stdafx.h"5#include"iostream.h"6 #defineMAXSIZE 307 8 voidFindwater (intId[],intLenintwater[])9 {Ten     intcounter[3];//counter Onecounter[0]=counter[1]=counter[2]=0; Awater[0]=water[1]=water[2]=-1;//Initialize list of suspects -      for(intI=0; i<len;i++) -     { the         if(counter[0]==0)//Assigning values First -         { -counter[0]++; -water[0]=Id[i]; +         } -         Else if(counter[1]==0) +         { Acounter[1]++; atwater[1]=Id[i]; -         } -         Else if(counter[2]==0) -         { -counter[2]++; -water[2]=Id[i]; in         } -         Else if(id[i]==water[0])//If counter is 0, re-assign the value to         { +counter[0]++; -         } the         Else if(id[i]==water[1]) *         { $counter[1]++;Panax Notoginseng         } -         Else if(id[i]==water[2]) the         { +counter[2]++; A         } the         Else                        //if the IDs of the three suspects are different, the three counters are reduced by one until the counter is 0 . +         { -counter[0]--; $counter[1]--; $counter[2]--; -          } -     } the } - Wuyi intMainintargcChar*argv[]) the { -     intId[maxsize];//Post record form Wu     intnum,waterking[3];//number of posts, buckets -cout<<"Please enter the number of posts:"; AboutCin>>num; $cout<<"Please enter the Post ID:"; -      for(intI=0; i<num;i++) -     { -Cin>>Id[i]; A     } + Findwater (id,num,waterking); thecout<<"buckets are:"; -      for(i=0;i<3; i++) $     { thecout<<waterking[i]<<" "; the     } thecout<<Endl; the     return 0; -}

Third, the experiment

Iv. Experience

At first, I refined the problem, thought that the choice of three IDs must be different, the program is very messy, the complexity is not O (N2), and finally did not debug successfully. Later I searched the Internet, found that because they each post more than 1/4, so can not be offset, so there is no additional judgment conditions. So I put the nested ifelse have been deleted, finishing a bit on the finished.

Algorithm, or from logic and mathematics to better optimize the code.

Software engineering with little work--looking for "buckets" (c + +)

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.