将非负十进制整数n转换成b进制。&&任何一个正整数都可以用2的幂次方表示。

题目1:将非负十进制整数n转换成b进制。(其中b=2~16)
题目2:任何一个正整数都可以用2的幂次方表示。例如:
    137=27+23+2^0    
同时约定幂次方用括号来表示,即ab 可表示为a(b)。
   由此可知,137可表示为:
     2(7)+2(3)+2(0)
进一步:7= 22+2+20 (21用2表示)
     3=2+2^0
所以最后137可表示为:
     2(2(2)+2+2(0))+2(2+2(0))+2(0)
   又如:
     1315=2^10 +2^8 +2^5 +2+2^0
所以1315最后可表示为:
   2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
  输入:正整数(n≤20000)
输出:符合约定的n的0,2表示(在表示中不能有空格)
输入格式 Input Format
一个正整数
输出格式 Output Format
符合约定的n的0,2表示(在表示中不能有空格)
样例输入 Sample Input
73
样例输出 Sample Output
2(2(2)+2)+2(2+2(0))+2(0)
题目一:

import java.util.Scanner;

public class jinzhizhuanhuan1 {
	
	private static String bi = "0123456789ABCDEF";
	
	public static void main(String[] args) {
		//test_();
		test();
	}

	public static void test_() {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入您要转换的数:");
		int num = Integer.parseInt(sc.nextLine());
		System.out.println("请输入您要将该数转换为何进制:");
		int b = Integer.parseInt(sc.nextLine());
		System.out.println("递归实现");
		System.out.println(change1(num, b));
		System.out.println("非递归实现");
		System.out.println(change2(num, b));
	}
	
	public static String change1(int num, int b) {
		//	递归实现
		int remainder = num % b;
		if (num == 0) {
			return "";
		}
		return change1(num / b, b) + bi.charAt(remainder);
	}
	
	public static String change2(int num, int b) {
		//	非递归实现
		StringBuilder res = new StringBuilder("");
		while (num != 0) {
			res.append(bi.charAt(num % b));
			num /= b;
		}
		return res.reverse().toString();
	}
	
	public static void test() {
		//	测试模块
		int n = 20;
		int a = 20000;
		for (int i = 0; i < n; i++) {
			int num = (int)(a * Math.random()) + 1;
			int b = (int)(15 * Math.random()) + 2;
			System.out.println("num=" + num + " b=" + b);
			System.out.println("递归实现");
			System.out.println(change1(num, b));
			System.out.println("非递归实现");
			System.out.println(change2(num, b));
		}
		
	}
}

题目二:

import java.util.Scanner;

public class erdemici {
	public static void main(String[] args) {
		//demo();
		test();
	}

	public static void demo() {
		Scanner sc = new Scanner(System.in);
		int num = Integer.parseInt(sc.nextLine());
		System.out.println(fun(num));
	}
	
	public static String fun(int num) {
		if (num == 1) {
			return "2(0)";
		}
		if (num == 2) {
			return "2";
		}
		int mi = fundNum(num);
		num = num - (int)Math.pow(2, mi);
		if (num == 0) {
			return "2(" + fun(mi) + ")";
		}
		if (mi == 1) {
			return "2+" + fun(num);
		}
		return "2(" + fun(mi) + ")+" + fun(num);
	}
	
	
	public static int fundNum(int num) {
		//	找小于等于num的2的n次幂中最大的值对应的n
		int n = 0;
		while (true) {
			if((int)Math.pow(2, n) > num) {
				break;
			}
			n++;
		}
		return --n;
	}
	
	public static void test() {
		int[] nums =  {137, 1315, 73};
		String[] ress = {"2(2(2)+2+2(0))+2(2+2(0))+2(0)",
				"2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)",
				"2(2(2)+2)+2(2+2(0))+2(0)"};
		for (int i = 0; i < nums.length; i++) {
			String res = fun(nums[i]);
			System.out.println(res);
			if (ress[i].equals(res)) {
				System.out.println(nums[i] + "正确");
			}
		}
	}
	
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值