【问题标题】:How to count number of 1s without using an array?如何在不使用数组的情况下计算 1 的数量?
【发布时间】:2017-02-15 12:13:15
【问题描述】:
static void Main(string[] args)
{
    int m, count = 0;

    Console.WriteLine("Enter the Limit : ");
    m = int.Parse(Console.ReadLine());

    int[] a = new int[m];
    Console.WriteLine("Enter the Numbers :");

    for (int i = 0; i < m; i++)
    {
        a[i] = Convert.ToInt32(Console.ReadLine());
    }

    foreach (int o in a)
    {
        if (o == 1)
        {
            count++;
        }
    }

    Console.WriteLine("Number of 1s in the Entered Number : "+count);
    Console.ReadLine();
}

这里把每个值都存入数组,并检查每个值是否等于1。但我需要这个任务而不使用数组。你能帮我们吗?

【问题讨论】:

  • “不使用数组”是什么意思?在你的情况下,你认为它还能如何工作?除了集合(数组、列表等)之外,您会将输入存储在哪里。您的问题到底是什么?
  • 如果我们向您展示一个列表解决方案,您满意吗?
  • 你的问题很不清楚。在您的 GUI 中,用户可以输入 m 不同的数字,例如 1,也可以输入 12 甚至 111。因此,即使只有 一个 单个数字,1 的计数也可能要大得多 1。所以您是否在 all 中寻找 all 1 i> 数字,还是仅限制用户每个数字输入一个数字,即仅表示从 0 到 9 的数字?换句话说:如果您的用户输入2m,然后输入12341111 为实际数字会发生什么?输出是2 还是5

标签: c# arrays comparison


【解决方案1】:

输入时只检查输入,不存储:

static void Main(string[] args)
{
            int m, count = 0;

            Console.WriteLine("Enter the Limit : ");
            m = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter the Numbers :");

            for (int i = 0; i < m; i++)
            {
              if(Console.ReadLine() == "1")
                 count++;
            }

            Console.WriteLine("Number of 1's in the Entered Number : "+count);

            Console.ReadLine();
}

【讨论】:

    【解决方案2】:

    您可以简单地在将其添加到数组的位置进行计数

            int m, count = 0;
            Console.WriteLine("Enter the Limit : ");
            m = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the Numbers :");
            for (int i = 0; i < m; i++)
            {
               count += Console.ReadLine() == "1" ? 1 : 0;
            }
    
            Console.WriteLine("Number of 1's in the Entered Number : "+count);
    
            Console.ReadLine();
    

    【讨论】:

      【解决方案3】:

      您可以使用 LINQ 并删除 for 循环以及数组。

      Console.WriteLine("Enter the Limit : ");
      int m = int.Parse(Console.ReadLine());
      
      Console.WriteLine("Enter the Numbers :");
      
      int count =
          Enumerable
              .Range(0, m)
              .Select(n => Console.ReadLine())
              .Where(x => x == "1")
              .Count();
      
      Console.WriteLine("Number of 1's in the Entered Number : " + count);
      
      Console.ReadLine();
      

      【讨论】:

        【解决方案4】:

        我建议使用更有意义的变量名称并添加输入验证 Int32.TryParse 而不是 Convert.ToInt32

        只需执行if (o == 1) 签入第一个for,然后忘记第二个。

        【讨论】:

          【解决方案5】:

          您可以使用列表代替数组。 代码在这里

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Threading.Tasks;
          
          namespace NumberofOnes
          {
              public class Program
              {
                  static void Main(string[] args)
                  {
                      int m, count = 0;
                      Console.WriteLine("Enter the Limit : ");
                      m = int.Parse(Console.ReadLine());
                      List<int> a = new List<int>();
          
                      Console.WriteLine("Enter the Numbers :");
                      for (int i = 0; i < m; i++)
                      {
                          a.Add( Convert.ToInt32(Console.ReadLine()));
                      }
                      foreach (int o in a)
                      {
                          if (o == 1)
                          {
                              count++;
                          }
                      }
                      Console.WriteLine("Number of 1's in the Entered Number : " + count);
          
                      Console.ReadLine();
          
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-06-15
            • 1970-01-01
            • 2020-12-11
            • 2021-04-21
            • 2023-04-09
            • 2011-07-25
            • 1970-01-01
            • 2018-02-07
            相关资源
            最近更新 更多