获取仅包含数字x和数字y的所有数字

时间:2017-01-09 19:12:18

标签: c# algorithm

很抱歉,如果问题有点令人困惑,我的情况我需要两个得到包含5和2的所有数字。输入将是起始编号和结束编号,我需要找到这两个数字之间只包含数字的所有数字5和数字2。

例如

Input - starting number - 1 end number -30
Output - 2,5,22,25.

如何在C#中执行此操作?

编辑::由于很少有用户问我试过了什么,下面是我试过的示例片段,但我觉得这不是一个正确的方法。这将是昂贵的

public static void main()
{
    int start = 1
    int end = 30

    for(int i = start; i<=end; i++)
    {
         if(CheckNumber(i))
         {
             Console.WriteLine(i);
         } 

     }
}

static bool CheckNumber(int num)
{
    int rem = 0;
    while(num != 0)
    {
         rem = num % 10;  
         if(rem != 2 || rem!=5)
             return false;
         num = num/10;
    }
    return true;
}

4 个答案:

答案 0 :(得分:4)

使用递归和数学:

void FiveTwo( int a, int b, int base ) {
    int n = base*10; // Number with all 2's & 5's and a 0 on the end
    if ( n+2<=b ) {
        FiveTwo(a,b,n+2);  // Find numbers bigger than n+2
        if ( a<=n+2 )
            Console.WriteLine( n+2 );  // Not too small to print
    }      
    if ( n+5<=b ) {
        FiveTwo(a,b,n+5);  // Find numbers bigger than n+5
        if ( a<=n+5 )
            Console.WriteLine( n+5 );  // Not too small to print
    } 
}

这样称呼:

FiveTwo( low, high, 0 );

答案 1 :(得分:1)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace ConsoleApp
{
    class Program
    {
        static void Main()
        {
            IEnumerable<int> numbers = Enumerable.Range(1, 30);

            foreach (int n in numbers)
            {
                var m = Regex.Match(n.ToString(), "^[25]+$");

                if (m.Success)
                    Console.WriteLine(n);
            };

            Console.ReadKey();
        }
    }
}

答案 2 :(得分:0)

 private void myFunc()
    {
        List<int> Result = new List<int>();

        for (int i = 0; i <= 30; i++)

        {
                if (IsMyResult(i.ToString()) == true)
                    Result.Add(i);  
        }
    }

    private bool IsMyResult(string x)
    {
        string[] numbersToSearch = { "2", "5" }; // you can add here all the numbers you want to be inside the number you are searching

        foreach (char digit in x)
        {
            if (numbersToSearch.Contains(digit.ToString()))
            {
                // do nothing it containes desired digit
            }
            else
            {
                // return false its not the result
                return false;
            }
        }

        // if code reach here it must be true
        return true;
    }

答案 3 :(得分:0)

我建议实现 generator ,它返回IEnumerable<T>;然后,您可以在 Linq 的帮助下轻松创建任意查询:

// In order to avoid int.MaxValue, long.MaxValue borders let's return strings 
private static IEnumerable<string> Generator(params int[] digits) {
  var vals = digits
    .Distinct()
    .OrderBy(x => x)
    .Select(x => x.ToString())
    .ToArray();

  Queue<string> agenda = new Queue<string>(vals);

  while (true) {
    string value = agenda.Dequeue();

    yield return value;

    foreach (var v in vals)
      agenda.Enqueue(value + v);
  }
}

试验:

// First 15 numbers contain 2 and 5 only
string report1 = string.Join(", ", Generator(2, 5).Take(15));

Console.WriteLine(report1);

// Numbers contain 2 and 5 only less than 30
string report2 = string.Join(", ", Generator(2, 5).TakeWhile(c => int.Parse(c) <= 30)); 

Console.WriteLine(report2);

// Numbers contain 2 and 5 only between 100 and 1000
string report3 = string.Join(", ", Generator(2, 5)
  .SkipWhile(c => int.Parse(c) < 100)
  .TakeWhile(c => int.Parse(c) < 1000));

Console.WriteLine(report3);

输出:

2, 5, 22, 25, 52, 55, 222, 225, 252, 255, 522, 525, 552, 555, 2222

2, 5, 22, 25

222, 225, 252, 255, 522, 525, 552, 555