Little Red Hat

Source: Internet
Author: User
Description

A group of people who like Little Red Riding Hood held a special party. However, some people were painted with Big Wolf icons by the evil WM hats, but no one can see whether the hats on their heads have been painted. They can only see whether the hats on other heads are big gray wolf patterns. Now, I will tell you the number of big gray wolf hats on others' heads, smart you, can you tell me how many big gray wolves have been put on the head by WANG Xiao? Of course, if you find some people lie, just output-1.

Input contains multiple groups of test data. Each group of arrays has two rows.
The first line reads N, which indicates the number of participants (n <= 100)
In the second row, N numbers are read at a time. A [I] indicates the number of big wolf on the other heads seen by I. Output one number for each group of data.
1 2 1
4
1 1 1 2 Sample output2
-1


Algorithm:

I didn't know how to start when I first saw this question, but I quickly found a situation for analysis. For example, there are five guys.

If no big wolf icon exists, then: 0 0 0 0

If there is one: 0 1 1 1 1

If there are two: 1 1 2 2 2

If there are three: 2 2 2 3 3

If there are four: 3 3 3 3 4

If there are 5: 4 4 4 4 4

We can see the above rule. If there are n people, m of them have the Big Wolf icon, and the result is m M-1 N-M (if M is 0, is n 0)

Others are details. The code below


#include <iostream>
#include <vector>
using namespace std;

#define num 100

int main ()
{
int n;
while (cin >> n)
{
bool flag = true;
int a [100] = {0};
vector <int> vetmp;
for (int i = 0; i <n; i ++)
{
int input;
cin >> input;
// If the input is greater than the number of people minus one
if (input> n-1 && flag == true)
{
flag = false;
cout <<-1 << endl;
}
else
a [input] ++; // It is used to count the number of people, which is equivalent to counting and sorting
}
if (flag == false)
continue;
int sum = 0, max = -1, min = -1;
for (int i = 0; i <num; i ++)
{
if (a [i]! = 0)
{
sum ++;
if (min ==-1)
min = i;
else max = i;
}
} // end for

// In this case, no wolf or no wolf
if (sum == 1 && max ==-1 && (min == 0 || min == n-1))
{
if (min == 0)
cout << 0 << endl;
else cout << n << endl;
}
// Some people have wolves, some people have no wolves
else if (sum == 2 && a [min] == max && min == max-1)
{
cout << min + 1 << endl;
}
else
{
cout <<-1 << endl;
}
} // end while
} 
Related Article

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.