怎么把结果变成逆序输出啊 我用的递归调用,感觉是压栈,先进后出。。。
package M2;
import java.util.ArrayList;
class Number {
int i;
public void function(int num) {
if(su(num)){
System.out.println(num);
}
for (i = 2; i < num; i++) {
count++;
if (num % i == 0) { // 如果余数为0
function(num / i); // 递归,但是先进后出!!!
System.out.print(num / i + " ");
}
}
}
public void print() {
for (int a = 0; a < x; a++) {
System.out.println(arr[a]);
}
}
/**
* 判断是否是素数
* */
public boolean su(int num) {
int j = 2;
for (j = 2; j < num; j++) {
if (num % j == 0) {
break;
}
}
// 素数返回true
if (num == j) {
return true;
}
return false;
}
}
package M2;
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
Number n=new Number();
n.function(num);
n.print();
}
}
|
|