记Google的一道面试题(java) Beautiful Numbers

版权声明:个人博客网址 https://29dch.github.io/ GitHub网址 https://github.com/29DCH,欢迎大家前来交流探讨和star+fork! 转载请注明出处! https://blog.csdn.net/CowBoySoBusy/article/details/84929890

Beautiful Numbers
在这里插入图片描述

在这里插入图片描述

思路:
13->三进制->111
1 * 1+1 * 3+1 * 3 * 3=13
13%3=1,13/3=4
4%3=1,4/3=1
1%3=1,1/3=0

1.第一种情况:数据范围比较小
代码:

package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class BeautifulNumber {

  public static void main(String[] args) {
    Scanner in = new Scanner(
        new BufferedReader(new InputStreamReader(System.in)));
    int cases = in.nextInt();
    for (int i = 1; i <= cases; ++i) {
      long n = in.nextLong();
      System.out.println("Case #" + i + ": "
          + beautiful(n));
    }
  }

  private static long beautiful(long n) {
    for (long radix = 2; radix < n; radix++) {
      if (isBeautiful(n, radix)) {
        return radix;
      }
    }

    throw new IllegalStateException("Should not reach here.");
  }

  private static boolean isBeautiful(long n, long radix) {
    while (n > 0) {
      if (n % radix != 1) {
        return false;
      }
      n /= radix;
    }
    return true;
  }
}

效果图:
在这里插入图片描述

2.求解大数据集
思路:
N->r进制->1111…(k个)
N=r^(k-1)
+r^(k-2)
+…+r+1

k=64…2

代码:

package Test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class BeautifulNumberLarge {

    public static void main(String[] args) {
        Scanner in = new Scanner(
                new BufferedReader(new InputStreamReader(System.in)));
        int cases = in.nextInt();
        for (int i = 1; i <= cases; ++i) {
            long n = in.nextLong();
            System.out.println("Case #" + i + ": "
                    + beautiful(n));
        }
    }

    private static long beautiful(long n) {
        for (int bits = 64; bits >= 2; bits--) {
            long radix = getRadix(n, bits);
            if (radix != -1) {
                return radix;
            }
        }

        throw new IllegalStateException("Should not reach here.");
    }

    private static long getRadix(long n, int bits) {
        long minRadix = 2;
        long maxRadix = n;
        while (minRadix < maxRadix) {
            long m = minRadix + (maxRadix - minRadix) / 2;
            long t = convert(m, bits);
            if (t == n) {
                return m;
            } else if (t < n) {
                minRadix = m + 1;
            } else {
                maxRadix = m;
            }
        }
        return -1;
    }

    private static long convert(long radix, int bits) {
        long component = 1;
        long sum = 0;
        for (int i = 0; i < bits; i++) {
            if (Long.MAX_VALUE - sum < component) {
                sum = Long.MAX_VALUE;
            } else {
                sum += component;
            }

            if (Long.MAX_VALUE / component < radix) {
                component = Long.MAX_VALUE;
            } else {
                component *= radix;
            }
        }
        return sum;
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/CowBoySoBusy/article/details/84929890