算法[第四版]-图灵程序设计丛书-笔记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_20122925/article/details/79922119

第一章-笔记

1.1.9 : 编写一段代码,将一个正整数 N 用二进制表示并转换为一个 String 类型的值 s。 

public class Conversionbinary {

	public static void main(String[] args) {
		
		String s = "";
		int N = 8;
		for (int n = N; n > 0; n /= 2) {
			s = (n % 2) + s;
		}
		System.out.println(s);
		System.out.println(Integer.toBinaryString(N)); //内置方法

	}

}

1.1.14 : 编写一个静态方法lg(),接受一个整型参数N,返回不大于log2N的最大整数。不要使用Math库。 

public class Exercise14 {

	public static int lg(int N)
	{
		int  a=2;//  a为底数
		int  t=0;
		while(N>1)
		{
			t++;
			N/=a;
		}
		return t;
	}
	public static void main(String[] args) {
		
       System.out.println(lg(5));
	}

}



猜你喜欢

转载自blog.csdn.net/qq_20122925/article/details/79922119
今日推荐