#第二周1001题解#

来源:互联网 发布:魅影传说坐骑进阶数据 编辑:程序博客网 时间:2024/04/30 21:11

While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,noticed that the telephone number of his brother-in-law H. Smith had the following peculiar property: The sum of the digits of that number was equal to the sum of the digits of the prime factors of that number. Got it? Smith’s telephone number was 493-7775. This number can be written as the product of its prime factors in the following way:
4937775= 3*5*5*65837

The sum of all digits of the telephone number is 4+9+3+7+7+7+5= 42,and the sum of the digits of its prime factors is equally 3+5+5+6+5+8+3+7=42. Wilansky was so amazed by his discovery that he named this kind of numbers after his brother-in-law: Smith numbers.
As this observation is also true for every prime number, Wilansky decided later that a (simple and unsophisticated) prime number is not worth being a Smith number, so he excluded them from the definition.
Wilansky published an article about Smith numbers in the Two Year College Mathematics Journal and was able to present a whole collection of different Smith numbers: For example, 9985 is a Smith number and so is 6036. However,Wilansky was not able to find a Smith number that was larger than the telephone number of his brother-in-law. It is your task to find Smith numbers that are larger than 4937775!

Input
The input file consists of a sequence of positive integers, one integer per line. Each integer will have at most 8 digits. The input is terminated by a line containing the number 0.

Output
For every number n > 0 in the input, you are to compute the smallest Smith number which is larger than n,and print it on a line by itself. You can assume that such a number exists.

Sample Input

4937774
0

Sample Output

4937775
题目的意思是找一个数的所有质因子的位数和和本身位数和相同的数,刚开始的想法是,先进行分解质因数,得到的和和原数比较如果相等,返回此数,结果提交总是超时,计算了下复杂度也就n,所以就想应该是算法需要特殊的,百度了一下,发现用递归的过了,就用了分治法,这样每次求得质因子的和的过程都是最小开销。
java代码:

package 第二周;import java.util.ArrayList;import java.util.Scanner;public class Main1002 {    public static long getSumPrimeFacters(int n){//获得质因子的和        long sum=0,temp=0,nn=n;          for(long i=2;i*i<=n;++i)          {              if(nn%i==0)                  temp=getM(i);              while(nn%i==0)              {                  sum+=temp;                  nn/=i;              }          }          if(nn==n)              return -1;          if(nn!=1)                   sum+=getM(nn);          return sum;         }    public static int cut(int k) {// 分治法思想,如果是素数,就返回sum,否则,就将该数分成两部分,再来求各部分的质因子的sum        int sum = 0;        if (isSu(k))            sum = getM(k);        else {            for (int i = (int) Math.sqrt(k + 0.5); i > 1; i--)                if (k % i == 0)                    sum = cut(i) + cut(k / i);        }        return sum;    }    public static boolean isSu(int n) {//判断是否为素数        boolean a = true;        if (n == 2) {            return true;        }        for (int i = 2; i <= (int) Math.sqrt(n); i++) {            if (n % i == 0) {                return false;            }        }        return a;    }    public static int getF(int n) {        int sum = 0;        int i = 2;        while (true)         {            if (n % i == 0) {                sum += getM(i);                if (isSu(n /= i)) {                    break;                }            } else {                i++;            }        }        return sum += getM(n);    }    public static int getM(long i) {//各位之和        int sum = 0;        while (i != 0) {            sum += i % 10;            i = i / 10;        }        return sum;    }    public static int getSum(ArrayList<Integer> arr) {//获得动态数组digits之和        int sum = 0;        for (int i = 0; i < arr.size(); i++) {            if (arr.get(i) < 10) {                sum += arr.get(i);            } else {                sum += getM(arr.get(i));            }        }        return sum;    }    public static ArrayList<Integer> getSu(int n) {//将每个质因子存到动态数组中        ArrayList<Integer> arr = new ArrayList();        for (int i = 2; i <= n; i++) {            if (n % i == 0 && isSu(i)) {                arr.add(i);                n = n / i;                i = 2;                if (isSu(n)) {                    arr.add(n);                    break;                }            }        }        return arr;    }    public static void main(String args[]) {        Scanner sc = new Scanner(System.in);        while (true) {            int n = sc.nextInt();            if (n <= 0) {                break;            }            for(int i = n+1;;i++){                if(!isSu(i)&&getM(i)==getSumPrimeFacters(i)){                    System.out.println(i);                    break;                }            }        }    }}
0 0