[Programming Questions] Candy puzzle C + + implementation input Unknown integer

Source: Internet
Author: User
Tags ming


Focus, implementing an unknown integer input:
Vector<int> Vec;    string T; int num1; Getline (cin,t); Istringstream ISTR (t); while (ISTR >> num1) vec.push_back (num1+1);//indicates how to implement getting an unknown integer




Problem Description: Xiao Ming is a kindergarten teacher. One day kindergarten director to the children each hair a candy, children get to find some students get candy color and their own, some classmates candy color and their own different. Suppose each child only knows how many classmates and himself have got the same color candy. After class, a part of the children excited to tell Xiao Ming teacher, and let Xiao Ming teacher guess, at least how many classmates got the candy. For example, three children told the teacher Xiao Ming the result is as follows: The first child found that there are 1 people and their own candy color, the second child also found that 1 people and their own candy color, the third child found that there are 3 people and their own candy color. The 12th child can think of each other with their own color, such as red, the third child may not be red (otherwise the 12th child will find that there are 2 people and their own candy color), assuming he got the blue candy, then at least another 3 students to get the blue candy, Finally, at least 6 children got the candy. Now please help Xiao Ming to solve the puzzle. Input Description: Assume that some of the children's answer with a space interval, such as1 1 3Output Description: Direct printing at least how many children get candy like6Input Example 1:1 1 3Output Example 1:6Input Example 2:0 0 0Output Example 2:3Example Illustration 2: Three children have not found that people and their own color is the same, so the minimum is three children candy color is different

Answer:

#include <bits/stdc++.h>#include<vector>using namespacestd;intmain () {vector<int>VEC;  string  t;  int  NUM1; Getline (cin,t); Istringstream ISTR (t); while (ISTR >> num1) vec.push_back (num1+1  );//indicates how to obtain an unknown integer sort (Vec.begin (), Vec.end ()); intresult =0; intCount =0; BOOLFlag =false; inttmp;  for(inti =0; I<vec.size (); i++){        //cout<<vec[i]<<endl;        if(!flag) {tmp=Vec[i]; Count=1; Flag=true; }Else if(TMP = =Vec[i]) {Count++; }Else{flag=false; intnum = count/tmp; inty = count%tmp; if(Y! =0) Result= result + (num+1)*tmp; ElseResult + = num*tmp; I--; }    }    intnum = count/tmp; inty = count%tmp; if(Y! =0) Result= result + (num+1)*tmp; ElseResult + = num*tmp; cout<<result<<Endl; }

[Programming Questions] Candy puzzle C + + implementation input Unknown integer

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.