阶乘 16进制转换 输出1-9所有的排列组合

阶乘的代码

public class Factorial {
	
	public static Integer factorial(int i){
		if(i<0){
			System.out.println("输入错误");
			return null;
		}else if(i==0){
			return 1;
		}else{
			if(i-1 > 1){
				return i*factorial(i-1);
			}else{
				return i;
			}
		}
	}
	
	public static void main(String[] args) {
		for(int num=-100;num<10;num++){
			System.out.println(num+ "的阶乘是"+ factorial(num));
		}
	}
}

 
负数不知道怎么处理好

 

16进制转换为10进制整数public class Hex2Decimal {

	
	//用Integer的方法
	/*
	public static int hex2Decimal(String str){
		return Integer.parseInt(str, 16);
	}*/
	
	//用Character的方法
	public static int hex2Decimal(String str){
		int len = str.length();
		int sum = 0;
		for(int i=0;i<len;i++){
			char c = str.charAt(i);
			int n = Character.digit(c, 16);
			sum += n*(1<<(4*i));
		}
		return sum;
	}
	
	
	
	public static void main(String[] args) {
		String st = "aa";
		System.out.println(hex2Decimal(st));
	}
}
 

输出1-9所有的排列组合public class Permutations {

	
    private int[] a = new int[10];
   
    public void init(){
        for(int i=1;i<=9;i++){
            a[i]=i;   
        }
    }
   
    private void printNumber(){
        for(int j=1;j<=9;j++){
            System.out.print(a[j]+" ");
        }
        System.out.println();
    }
   
    public void range(int index){
        if(index==9){
            printNumber();
        }
        else{
            for(int i=index;i<=9;i++){
                int k = index;
                swap(i,k);
                range(k+1);               
                swap(i,k);
            }       
        }
    }
   
    private void swap(int i, int j){
        int temp =a[i];
        a[i]=a[j];
        a[j]=temp;
    }
   
    public static void main(String[] args) {
    	Permutations p = new Permutations();
        p.init();
        p.range(1);
    }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值