Java 7,220字节
String c(int n){String r=n+",",c;for(;!r.matches("^"+(c=(n=d(n))+",")+".*|.*,"+c+".*");r+=c);return r;}int d(int n){int s=0;for(String i:(n+"").split(""))s+=f(new Long(i));return s;}long f(long x){return x<2?1:x*f(x-1);}
说明:
String c(int n){ // Method with integer parameter and String return-type
String r=n+",", // Result-String (which starts with the input integer + a comma
c; // Temp String
for(;!r.matches( // Loop as long as the result-String doesn't match the following regex:
"^"+(c=(n=d(n))+",")+".*|.*,"+c+".*"); // "^i,.*|.*,i,.*" where `i` is the current integer
// `n=d(n)` calculates the next integer in line
// `c=(n=d(n))+","` sets the temp String to this integer + a comma
r+=c // And append the result-String with this temp String
); // End of loop
return r; // Return the result-String
} // End of method
int d(int n){ // Separate method (1) with integer parameter and integer return-type
int s=0; // Sum
for(String i:(n+"").split("")) // Loop over the digits of `n`
s+=f(new Long(i)); // And add the factorial of these digits to the sum
// End of loop (implicit / single-line body)
return s; // Return the sum
} // End of separate method (1)
long f(long x){ // Separate method (2) with long parameter and long return-type (calculates the factorial)
// (NOTE: 2x `long` and the `new Long(i)` is shorter than 2x `int` and `new Integer(i)`, hence long instead of int)
return x<2? // If `x` is 1:
1 // return 1
: // Else:
x*f(x-1); // return `x` multiplied by the recursive-call of `x-1`
} // End of method (2)
测试代码:
在这里尝试。
class M{
String c(int n){String r=n+",",c;for(;!r.matches("^"+(c=(n=d(n))+",")+".*|.*,"+c+".*");r+=c);return r;}int d(int n){int s=0;for(String i:(n+"").split(""))s+=f(new Long(i));return s;}long f(long x){return x<2?1:x*f(x-1);}
public static void main(String[] a){
System.out.println(new M().c(132));
}
}
输出:
132,9,362880,81369,403927,367953,368772,51128,40444,97,367920,368649,404670,5810,40442,75,5160,842,40346,775,10200,6,720,5043,151,122,5,120,4,24,26,722,5044,169,363601,1454,