📜  分组国家

📅  最后修改于: 2021-04-29 14:59:48             🧑  作者: Mango

一群人坐在一排,编号从1到n。每个人都被问到相同的问题,“你们国家中有多少人?”
人们提供的答案可能不正确。同一国家的人民总是坐在一起。如果所有答案均正确,请确定其他国家/地区的数量,否则请打印“无效答案”。

例子:

Input : ans[] = {1, 3, 2, 2}
Output : Invalid Answer
The second person says there are 3 
people from his country however
the person sitting next to him says
there are 2 people. Hence this is 
an invalid answer. 

Input : ans[] = {1, 1, 2, 2, 4, 4, 
                            4, 4}
Output : 4
There are 1 person each representing
two distinct countries. In the next 
one there are two people and in the
fourth one there are 4 people from
the same country.

资料来源:ThoughtWorks应用资格考试

这是一个可以在线性时间内解决的基本问题。我们将使用一个变量curr_size,它告诉我们正在考虑的当前国家/地区的大小。无论人数多少,下一个“人数”人数都应给出相同的答案,以便形成有效的群体。如果有人给出了不同的答案,或者可用人数少于该人数,则答案为无效。下面是想法的实现:

C++
// CPP program to count no of distinct
// countries from a given group of people
#include 
using namespace std;
  
void countCountries(int ans[], int N)
{
    int total_countries = 0, i = 0;
    bool invalid = false;
  
    while (i < N) {
        int curr_size = ans[i];
  
        // Answer is valid if adjacent sitting 
        // num people give same answer
        int num = ans[i];
        while (num > 0) {
  
            // someone gives different answer
            if (ans[i] != curr_size) {
                cout << "Invalid Answer\n";
                return;
            }
            else
                num--;
  
            // check next person
            i++;
        }
  
        // one valid country group has
        // been found
        total_countries++;
    }
  
    cout << "There are " << total_countries 
         << " distinct companies in the group.\n";
}
  
// driver program to test above function
int main()
{
    int ans[] = { 1, 1, 2, 2, 4, 4, 4, 4 };
    int n = sizeof(ans) / sizeof(ans[0]);
    countCountries(ans, n);
    return 0; 
}


Java
// Java program to count no of distinct
// countries from a given group of people
  
class Country
{
    public static void countCountries(int ans[],
                                      int N)
    {
        int total_countries = 0, i = 0;
        boolean invalid = false;
  
        while (i < N) {
            int curr_size = ans[i];
  
            // Answer is valid if adjacent sitting 
            // num people give same answer
            int num = ans[i];
            while (num > 0) {
  
            // someone gives different answer
            if (ans[i] != curr_size) {
                System.out.print( "Invalid Answer\n" );
                return;
            }
            else
                num--;
  
            // check next person
            i++;
        }
  
        // one valid country group has
        // been found
        total_countries++;
        }
  
        System.out.print( "There are " + total_countries +
            " distinct companies in the group.\n" );
    }
  
    // driver code
    public static void main(String[] args)
    {
        int ans[] = { 1, 1, 2, 2, 4, 4, 4, 4 };
        int n = 8;
        countCountries(ans, n);
    }
}
  
// This code is contributed by rishabh_jain


Python3
# Python3 program to count no of distinct
# countries from a given group of people
  
def countCountries(ans, N):
    total_countries = 0
    i = 0
    invalid = 0
  
    while (i < N) :
        curr_size = ans[i]
  
        # Answer is valid if adjacent sitting 
        # num people give same answer
        num = ans[i]
        while (num > 0) :
  
            # someone gives different answer
            if (ans[i] != curr_size) :
                print("Invalid Answer")
                return;
            else:
                num = num - 1
  
            # check next person
            i = i + 1
  
        # one valid country group has
        # been found
        total_countries = total_countries + 1;
  
    print ("There are ", total_countries,
          " distinct companies in the group.")
  
# Driven code
ans = [ 1, 1, 2, 2, 4, 4, 4, 4 ];
n = len(ans);
countCountries(ans, n);
  
# This code is contributed by "rishabh_jain".


C#
// C# program to count no. of distinct
// countries from a given group of people
using System;
  
class Country {
      
    // function to count no. of distinct
    // countries from a given group of people
    public static void countCountries(int []ans,
                                      int N)
    {
        int total_countries = 0, i = 0;
  
  
        while (i < N) {
            int curr_size = ans[i];
  
            // Answer is valid if adjacent sitting 
            // num people give same answer
            int num = ans[i];
            while (num > 0) {
  
            // someone gives different answer
            if (ans[i] != curr_size) {
                Console.Write( "Invalid Answer\n" );
                return;
            }
            else
                num--;
  
            // check next person
            i++;
        }
  
        // one valid country group 
        // has been found
        total_countries++;
        }
  
        Console.Write("There are " + total_countries +
                      " distinct companies in the group.\n" );
    }
  
    // Driver Code
    public static void Main()
    {
        int []ans = { 1, 1, 2, 2, 4, 4, 4, 4 };
        int n = 8;
        countCountries(ans, n);
    }
}
  
// This code is contributed by nitin mittal


PHP
 0)
        {
  
            // someone gives different 
            // answer
            if ($ans[$i] != $curr_size)
            {
                echo"Invalid Answer\n";
                return;
            }
            else
                $num--;
  
            // check next person
            $i++;
        }
  
        // one valid country group has
        // been found
        $total_countries++;
    }
  
    echo "There are " , $total_countries 
        , " distinct companies in the group.\n";
}
  
    // Driver Code
    $ans = array(1, 1, 2, 2, 4, 4, 4, 4 );
    $n = sizeof($ans);
    countCountries($ans, $n);
      
// This code is contributed by nitin mittal.
?>


输出:

There are 4 distinct companies in the group.