如何在 C# 或 C++ 中使用条件语句 check/compare 四个变量是否相同或不同

How to check/compare whether four variables are same or different using conditional statement in C# or C++

我有 4 个变量,其值为 a=50;b=100;c=500;d=1000; 如果所有值都相同则count = 1,否则如果任何两个值相同则count = 3,如果任何三个值相同则count = 2。 我可以很容易地找出是否所有值都相同,但在其余两种情况下。

哪个是用最少的代码找到计数的最佳条件语句。

我应用的代码:

int a=50,b=100,c=500,d=100;
if (a == b && a == c&& a ==d)
            {
                i=1;
            }
            else if (a != b)
            {
                if (a !=c)
                {
                    if (a != d)
                    {
                        i = 4;
                    }
                }
            }

我对如何搜索条件感到困惑,例如:-a = 50; b=100;c=100;d=1000;

在我得到不同的值后,我需要具体说明 similar.How 有多少个值,这是否可能,即:如果 a=50;b=50 c=100;d=1000;所以 count 将是 3 但相似的值又如何呢,即 int s=2; for 50s1 =1 for 100s2 =1 for 1000

获取不同值的数量:

int i = new [] { a, b, c, d }.Distinct().Count();

获取每个不同值的计数:

Dictionary<int,int> counts = new [] { a, b, c, d }
    .GroupBy(t=>t)
    .ToDictionary(g=>g.Key, g=>g.Count());

这为您提供了一个字典,将每个不同的值映射到它在列表中出现的次数。

经过大量尝试,我找到了一个确实可以满足我需要的代码

  int a ,b,c,d;  
    List<int> lst = new List<int> { a, b, c, d};
                int res = (from x in lst
                           select x).Distinct().Count();