1. /**
2. * 冒泡排序算法
3. */
4. public class bubblesort {
5. public static void sort(int[] values) {
6. int temp;
7. for (int i = 0; i < values.length; ++i) {
8. for (int j = 0; j < values.length - i - 1; ++j) {
9. if (values[j] > values[j + 1]) {
10. temp = values[j];
11. values[j] = values[j + 1];
12. values[j + 1] = temp;
13. }
14. }
15. }
16. }
17. }
2. *遞歸算法,求n的階乘
4. public class recursion {
5. int result=1;
6. public int nrecursion(int n) {
7. if (n > 0) {
8. result = result * n;
9. nrecursion(n-1);
10. }
11. return result;
12. }
13. }
2. * 求最大公約數和最小公倍數
4. public class convention {
5. /**
6. * 求兩數的最大公約數
7. */
8. int divisor(int m,int n){
9. if(m%n==0){
10. return n;
11. }else{
12. return divisor(n,m%n);
13. }
14. }
15. /**
16. * 求兩數的最小公倍數
17. */
18. int gbs(int a,int b){
19. int gbs = 0;
20. gbs = a*b/divisor(a,b);
21. return gbs;
22. }
23. }
java分解質因數
public static string getnumformx(int num){
stringbuffer result=new stringbuffer("");
for(int i=2;i<=num;){
if(num%i==0){
system.out.println("ok");
result.append(i).append("*");
num=num/i;
i=2;
}else{
i++;
}
return result.substring(0,result.length()-1);
}