-2

我的任务是:编写一个程序,读取两个正整数并打印它们之间存在多少个数字 p,这样除以 5 的提示符为 0(含)。示例:p(17,25) = 2。

Console.Write("Enter min: ");
            int min = int.Parse(Console.ReadLine());
            Console.Write("Enter max: ");
            int max = int.Parse(Console.ReadLine());
            Console.WriteLine("The numbers devidable by 5 without remainder from {0} to {1} are: ",min,max);
            for (int i = min; i <= max; i++)
            {
                if (i % 5 == 0)
                {

                    Console.WriteLine(i);
                }
            }

这将打印出该范围内可被 5 整除的数字......我如何计算有多少并在控制台中打印计数?

4

8 回答 8

8

对于积极的论点,你可以在 O(1) 中做:

int DivisibleBy5From0To(int n)
{
    return (n / 5) + 1;
}

int DivisibleBy5FromTo(int lo, int hi)
{
    return DivisibleBy5From0To(hi) - DivisibleBy5From0To(lo - 1);
}

对于可能不是积极的论点,您需要使用Math.Floor(n / 5.0)而不是n / 5.

于 2012-11-04T22:52:28.247 回答
4

也许:

int numMod5Between = Enumerable.Range(first, second - first + 1)
                               .Where(i => i % 5 == 0)
                               .Count();
于 2012-11-04T22:49:28.130 回答
3
Console.WriteLine(Enumerable.Range(min,max-min+1).Count(n => n % 5 == 0));
于 2012-11-04T22:50:46.650 回答
2
        int total = 0;
        for (int i = min; i <= max; i++)
        {
            if (i % 5 == 0)
            {
                total = total + 1;
            }
        }
        //print total
于 2012-11-04T22:48:13.730 回答
2

这很容易,当您在 for 循环中有特殊情况时,只需在其中增加一个计数器。

            Console.Write("Enter min: ");
            int min = int.Parse(Console.ReadLine());
            Console.Write("Enter max: ");
            int max = int.Parse(Console.ReadLine());
            Console.WriteLine("The numbers devidable by 5 without remainder from {0} to {1} are: ",min,max);
            int count = 0;
            for (int i = min; i <= max; i++)
            {
                if (i % 5 == 0)
                {

                    Console.WriteLine(i);
                    count++;
                }
            }
            Console.WriteLine("Total number dividable by 5 is: " + count.ToString());
于 2012-11-04T22:49:22.443 回答
1
    int count=0;
    Console.Write("Enter min: ");
                int min = int.Parse(Console.ReadLine());
                Console.Write("Enter max: ");
                int max = int.Parse(Console.ReadLine());
                Console.WriteLine("The numbers devidable by 5 without remainder from {0} to {1} are: ",min,max);
                for (int i = min; i <= max; i++)
                {
                    if (i % 5 == 0)
                    {
                        count++;
                    }
                }
Console.WriteLine(count);

您添加一个count变量,将其设置为 0,并在找到可分割的数字时将其增加,最后打印count.

于 2012-11-04T22:48:21.987 回答
1
Console.Write("Enter min: ");
        int min = int.Parse(Console.ReadLine());
        Console.Write("Enter max: ");
        int max = int.Parse(Console.ReadLine());
        Console.WriteLine("The numbers devidable by 5 without remainder from {0} to {1} are: ",min,max);
        int count = 0;
        for (int i = min; i <= max; i++)
        {
            if (i % 5 == 0)
            {

                Console.WriteLine(i);
                count++;
            }
        }
        Console.WriteLine(count);

新行是int count = 0;count++;跟随Console.WriteLine(count);。逻辑是每次i % 5 == 0都是真的然后你增加计数。

于 2012-11-04T22:49:49.260 回答
0
 Console.Write("Enter min: ");
        int min = int.Parse(Console.ReadLine());
        Console.Write("Enter max: ");
        int max = int.Parse(Console.ReadLine());
        Console.WriteLine("The numbers dividable by 5 without remainder from {0} to {1} are: ", min, max);
        for (int i = min; i <= max; i++)
        {
            if (i % 5 != 0)

            continue;
            Console.WriteLine(i);

这是使用 continue 语句解决此问题的另一种方法。

于 2017-04-07T14:04:35.753 回答