代码之家  ›  专栏  ›  技术社区  ›  Cassie

查找73个回文

  •  0
  • Cassie  · 技术社区  · 7 年前

    我试图找到73个十进制数,其中二进制表示是回文。我还需要得到这些数字的总和。 我对Scala完全陌生,所以我一直在尝试创建这个函数来定义回文并对其进行总结。问题是,我真的不知道如何将其组合成一个函数,并包括应该有73个数字。

    我已经有了一个定义回文的简单函数:

    def isPalindrome(someNumber: String): Boolean = someNumber.length > 1 && someNumber.reverse.mkString == someNumber
    

    我已经为我的主要职能制定了一些蓝图。我尝试将找到的所有回文数字写入列表(以便稍后使用一些过滤器):

    def findPalindromes(list: List[Int]): Int = {
      for(i <- 0 to 100){
        if(isPalindrome(Integer.toBinaryString(i))) {
          (xs: List[Int]) => i :: xs
        }
        sum(list)
      }
    }
    

    我知道一些集合函数,但我没有太多使用它们的经验。因此,如果您能向我解释在这种情况下可以使用哪些集合和函数,我将不胜感激。 我将非常感谢任何帮助!

    2 回复  |  直到 7 年前
        1
  •  2
  •   Jabari Dash    7 年前

    你的蓝图很好。只是不要假设前73个回文存在于前100个整数中。甚至可以从负数开始,因为它的二进制表示可能是回文。为了简单起见,我将从0开始,寻找前73个正回文。对不起,我不懂Scala,我懂Java。算法将是相同的。

    输出:

    ...
    341 is a palindrome
    341 in binary is 101010101
    
    349 is a palindrome
    349 in binary is 101011101
    
    357 is a palindrome
    357 in binary is 101100101
    
    sum:9245
    

    代码:

    public class Palindrome {
    
      public static void main(String[] args) {
        int count;
        int sum;
        int i;
    
        count = 0;
        sum = 0;
    
        // Start at integer 0
        i = 0;
    
        // Loop until we count 73 palindromes
        while (count < 73) {
    
          if (isPalindrome(i)) {
            System.out.println(i + " is a palindrome");
            System.out.println(i + " in binary is " + Integer.toBinaryString(i));
            System.out.println();
    
            // Increment the sum
            sum += i;
    
            // Incrememnt the counter
            count++;
          }
    
          // Increment the index
          i++;
        }
    
        System.out.println("sum:" + sum);
    
      }
    
      public static boolean isPalindrome(int n) {
    
        // By default, we assumt the String to be a palindrome
        boolean palindrome = true;
        String string;
        int length;
    
        // Convert to binary
        string = Integer.toBinaryString(n);
    
        // Get length of string
        length = string.length();
    
        // Loop half way into the string
        for (int i = 0; i < length/2 - 1; i++) {
    
          // Compare the ith character from beginning of string
          // to the ith character going from the end of stirng
          if (string.charAt(i) != string.charAt(length-i-1)) {
    
            // If they are not equal, set boolean to false, and break
            palindrome = false;
            break;
          }
        }
        return palindrome;
      }
    }
    
        2
  •  1
  •   Brian    7 年前

    filter take 可以在这里使用,而不是循环。

    将向基于传递的谓词返回的新集合中添加元素。对你来说是这样的 isPalindrome

    可以使用get n 集合中的元素。i、 e.通过过滤器的前73个元件。

    因此,如果你将这些链接在一起,你会得到:

    def findPalindromes(list: List[Int]): Int = {
      list.filter(x => isPalindrome(Integer.toBinaryString(x))).take(73).sum
    }
    

    Range(1, 10000).toList . 另一个改进可能是在找到73个回文时停止此操作,而不是找到所有回文并取前73个回文。一个简单的计数器可以用于此或 Stream

    这个 流动 版本相当优雅:

    def findPalindromes(n: Int) = Stream.from(1).filter(x => isPalindrome(Integer.toBinaryString(x))).take(n)
    

    流动 具有 sum :

    scala> findPalindromes(73).sum res10: Int = 35619