C# 测试整数列表是奇数还是偶数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18818680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:17:53  来源:igfitidea点击:

Testing if a list of integer is odd or even

c#

提问by Arthur Mamou-Mani

Trying to determine if my list of integer is made of odd or even numbers, my desired output is a list of true an/or false. Can I perform the following operation on the list lst or do I need to create a loop? A is the output.

试图确定我的整数列表是由奇数还是偶数组成,我想要的输出是真和/或假的列表。我可以对列表 lst 执行以下操作还是需要创建一个循环?A是输出。

    List <int> lst = new List <int>();
    A = IsOdd(lst);

采纳答案by Michael0x2a

You could try using Linq to project the list:

您可以尝试使用 Linq 来投影列表:

var output = lst.Select(x => x % 2 == 0).ToList();

This will return a new list of bools such that {1, 2, 3, 4, 5}will map to {false, true, false, true, false}.

这将返回一个新的 bool 列表,以便{1, 2, 3, 4, 5}映射到{false, true, false, true, false}.

回答by TGH

Just use the modulus

只需使用模数

loop through the list and run the following on each item

循环遍历列表并对每个项目运行以下命令

if(num % 2 == 0)
{
  //is even
}
else
{
  //is odd
}

Alternatively if you want to know if all are even you can do something like this:

或者,如果您想知道是否全部都是偶数,您可以执行以下操作:

bool allAreEven = lst.All(x => x % 2 == 0);

回答by Free Coder 24

There's at least 7 different ways to test if a number is odd or even. But, if you read through these benchmarks, you'll find that as TGH mentioned above, the modulus operation is the fastest:

至少有 7 种不同的方法可以测试一个数字是奇数还是偶数。但是,如果您阅读这些基准测试,您会发现正如上面提到的 TGH,取模运算是最快的:

if (x % 2 == 0)
               //even number
        else
               //odd number

Here are a few other methods (from the website) :

以下是其他一些方法(来自网站):

//bitwise operation
if ((x & 1) == 0)
       //even number
else
      //odd number

//bit shifting
if (((x >> 1) << 1) == x)
       //even number
else
       //odd number

//using native library
System.Math.DivRem((long)x, (long)2, out outvalue);
if ( outvalue == 0)
       //even number
else
       //odd number

回答by Newby25

        #region even and odd numbers
        for (int x = 0; x <= 50; x = x + 2)
        {

            int y = 1;
            y = y + x;
            if (y < 50)
            {
                Console.WriteLine("Odd number is #{" + x + "} : even number is #{" + y + "} order by Asc");
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("Odd number is #{" + x + "} : even number is #{0} order by Asc");
                Console.ReadKey();
            }

        }

        //order by desc

        for (int z = 50; z >= 0; z = z - 2)
        {
            int w = z;
            w = w - 1;
            if (w > 0)
            {
                Console.WriteLine("odd number is {" + z + "} : even number is {" + w + "} order by desc");
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("odd number is {" + z + "} : even number is {0} order by desc");
                Console.ReadKey();
            }
        }

回答by user9667279

--simple codes--

--简单代码--

        #region odd / even numbers  order by desc

        //declaration of integer
        int TotalCount = 50;
        int loop;

        Console.WriteLine("\n---------Odd Numbers -------\n");

        for (loop = TotalCount; loop >= 0; loop--)
        {
            if (loop % 2 == 0)
            {
                Console.WriteLine("Even numbers : #{0}", loop);
            }
        }

        Console.WriteLine("\n---------Even Numbers -------\n");

        for (loop = TotalCount; loop >= 0; loop--)
        {
            if (loop % 2 != 0)
            {
                Console.WriteLine("odd numbers : #{0}", loop);
            }
        }

        Console.ReadLine();

        #endregion