写的比较慢,每周不定时更新~~~~,不一定正确,有错误请指正
1.给出下列表达式的值:
a.(0 + 15) / 2 //7
b.2.0e-6* 100000000.1 //200.000000.2
c.true && false || true && true //true
2.给出以下表达式的类型和值:
a.(1 + 2.236) / 2 //double 1.618
b.1 + 2 + 3 + 4.0 //double 10.0
c.4.1 >= 4 //bool true
d.1 + 2 + "3" //string 33
3.编写一个程序,从命令行得到三个整数参数。如果他们都相等则打印equal,否则打印not equal。
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
if (a == b && b == c) {
System.out.println("equal");
} else {
System.out.println("not equal");
}
}
4.下列语句各有什么问题(如果有的话)?
a. if (a > b) then c = 0;// not then
b. if a > b { c = 0}; //(a > b)
c.if ( a > b) c =0; //true
d. if(a > b ) c = 0 else b = 0; //c=0;
5.编写一段程序,如果double类型的变量x和y都严格位于0和1之间则打印true,否则打印false。
public static void ptint(double x, double y){
System.out.println(x < 1 && x > 0 && y > 0 && y < 1);
}
6.下面这段程序会打印出什么?
int f = 0;
int g = 1;
for (int i = 0; i < 15; i++) {
System.out.println(f);
f = f + g;
g = f - g;
}
/*
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
*/
7.分别给出以下代码段打印出的值:
a. double t = 9.0;
while (Math.abs(t - 9.0 / t) > .001)
t = (9.0 / t + t) / 2.0;
System.out.printf("%.5f\n", t);//3.00009
b. int sum = 0;
for (int i = 0; i < 1000; i++)
for (int j = 0; j < i; j++)
sum++;
System.out.println(sum);//499500
c. int sum = 0;
for (int i = 1; i < 1000; i *= 2)
for (int j = 0; j < 1000; j++)
sum++;
System.out.println(sum);//10000
8.下列语句会打印出什么结果?
a.System.out.println('b');//b
b.System.out.println('b' + 'c'); //98+99 = 197
c.System.out.println((char)('a' + 4)); //e
9.编写一段代码,将一个正整数N用二进制表示并转化成一string类型的值s;
int N = 23456;
String s = "";
for(int n = N; n> 0; n /= 2){
s = (n % 2) + s;
}
System.out.println(s);
System.out.println(Integer.toBinaryString(N));
s = toBinaryString(N, "");
System.out.println(s);
//递归实现
private static String toBinaryString(int n, String s){
if (n > 0) {
return toBinaryString(n / 2, (n % 2) + s);
}else
return s;
}
11.编写一段代码,打印出一个二维不二数组的内容。其中,使用*表示真,空格表示假。
boolean[][] array = {
{ true, false, false },
{ false, false, true },
{ false, true, true }};
int row = 0, clo = 0;
while(clo <= array[0].length) System.out.print(clo++);
System.out.println();
for (boolean[] bools:array ) {
System.out.print(++row);
for(boolean bool : bools)
System.out.print(bool ? "*" : " ");
System.out.println();
}
// 0123
// 1*
// 2 *
// 3 **
12.下列代码会打印什么结果
int[] a = new int[10];
for (int i = 0; i < 10; i++) {
a[i] = 9 - i;
}
for (int i = 0; i < 10; i++) {
a[i] = a[a[i]];
}
for (int i = 0; i < 10; i++) {
System.out.println(a[i]);
}
//0
//1
//2
//3
//4
//5
//6
//7
//8
//9
13.编写一段代码,打印一个M行N列的二维数组的转置(交换行和列)
int[][] array = {{1,3,5},{2,4,6},{3,5,7},{4,6,8}};
int[][] newArray = null;
for(int i = 0, len = array.length; i < len;i ++){
for(int j = 0, len2 = array[i].length; j < len2; j++){
if (newArray == null) {
newArray = new int[len2][len];
}
newArray[j][i] = array[i][j];
}
}
System.out.println(Arrays.deepToString(newArray));
14.编写一个静态方法lg(),接收一个整型参数N,返回不大于log2N的最大整数。不要使用Math库。
public static int lg(int N){
int n, i;
for (i = 0, n = 1; n < N; i++)
n *= 2;
return i - 1;
}
15.
package com.vadonmo.exp.chapter;
import java.util.Arrays;
import com.vadonmo.exp.api.StdOut;
/****
* 编写一个静态方法histogram(),接受一个整型数组a[]和一个整数M为参数并返回一个大小为M的数组,
* 其中第i个元素的值为整数i在参数数组中出现的次数。
* 如果a[]中的值均在0到M-1之间,返回数组中的所有元素之和应该和a.length相等。
*
* @author vadon
*
*/
public class Exp1_1_15 {
public static void main(String args[]) {
int[] a = {1,2,2,5,4,7,7,0,0,5};
StdOut.print(Arrays.toString(histogram(a, 10)));
//[2, 1, 2, 0, 1, 2, 0, 2, 0, 0]
}
public static int[] histogram(int[] a, int M){
int[] array = new int[M];
for (int i = 0, len = a.length; i < len; i++) {
int num = 0;
for (int j = 0; j < len; j++) {
if (a[j] == a[i]) {
num ++;
}
}
array[a[i]] = num;
}
return array;
}
}
16.
package com.vadonmo.exp.chapter;
import com.vadonmo.exp.api.StdOut;
/**
* 给出exRl(6)的返回值
* @author vadon
*
*/
public class Exp1_1_16 {
public static void main(String[] args) {
StdOut.print(exRl(6));
}
public static String exRl(int n) {
if(n <= 0){
return "";
}else {
return exRl(n - 3) + n + exRl(n - 2) + n;
}
}
}
//311361142246
17.
package com.vadonmo.exp.chapter;
/**
* 找出下列递归函数的问题
* @author vadon
*
*/
public class Exp1_1_17 {
public static void main(String[] args) {
System.out.println(exR2(6));
//无限递归没有出口
}
public static String exR2(int n) {
String s = exR2(n - 3) + n + exR2(n - 2) + n;
if (n <= 0) {
return "";
}else {
return s;
}
}
}
18.
package com.vadonmo.exp.chapter;
import com.vadonmo.exp.api.StdOut;
/**
* mystery(2, 25)和mystery(3, 11)返回值是多少
*
* @author vadon
*
*/
public class Exp1_1_18 {
public static void main(String args[]) {
StdOut.print(mystery(2, 25) + "\n");
StdOut.print(mystery(3, 11) + "\n");
StdOut.print(mysteryMul(2, 25) + "\n");
StdOut.print(mysteryMul(3, 11));
}
public static int mystery(int a, int b) {
if (b == 0) {
return 0;
}
if (b % 2 == 0) {
return mystery(a + a, b / 2);
}
return mystery(a + a, b / 2) + a;
}
public static int mysteryMul(int a, int b) {
if (b == 0) {
return 1;
}
if (b % 2 == 0) {
return mystery(a * a, b / 2);
}
return mystery(a * a, b / 2) * a;
}
}
50
33
96
135
19.
package com.vadonmo.exp.chapter;
import java.math.BigInteger;
import com.vadonmo.exp.api.StdOut;
/**
* 计算机用这段程序在一个小时之内能够得到F(N)结果的最大N值是多少?
* 开发F(N)的一个更好的实现,用数组保存已经计算过的值。
* @author vadon
*
*/
public class Exp1_1_19 {
public static void main(String args[]) {
for (int N = 0; N < 100; N++) {
//StdOut.println(N + " " + F(N));
StdOut.println(N + " " + F2(N));
}
}
public static long F(int N) {
if (N == 0) {
return 0;
}
if (N == 1) {
return 1;
}
return F(N - 1) + F(N - 2);
}
//int范围太小放不开大数据,关于[大数值](http://blog.csdn.net/vadonmo/article/details/78417971)
public static BigInteger[] array = new BigInteger[100];
public static BigInteger F2(int N) {
if (N == 0) {
array[0] = BigInteger.valueOf(0);
return BigInteger.valueOf(0);
}
if (N == 1) {
array[1] = BigInteger.valueOf(1);
return BigInteger.valueOf(1);
}
array[N] = array[N - 1].add(array[N - 2]);
return array[N];
}
}
20.
package com.vadonmo.exp.chapter;
import com.vadonmo.exp.api.StdOut;
/**
* 编写一个递归的静态方法计算In(N!)的值
* @author vadon
*
*/
public class Exp1_1_20 {
public static void main(String[] args) {
StdOut.print(Math.log10(factorial(10)));
//6.559763032876794
//思考很久怎么写自然对数算法,抱着试试的想法打了Math,一看有。。,
//这道题的目的应该是阶乘
}
/**
* 计算阶乘
* @param N
* @return
* @return double
*/
public static double factorial(int N) {
if (N == 0) {
return 0;
}
if (N == 1) {
return 1;
}
return N * factorial(N - 1);
}
}
21
package com.vadonmo.exp.chapter;
import com.vadonmo.exp.api.StdIn;
import com.vadonmo.exp.api.StdOut;
/**
* 编写一段程序,从标准输入按行读取数据,其中每行都包含一个名字和两个整数。
* 然后用printf()打印一张表格,每行的若干列数据包括名字、
* 两个整数和第一个整数除以第二个整数的结果, 精确到小数点后三位。
*
* @author vadon
*
*/
public class Exp1_1_21 {
public static void main(String[] args) {
System.out.printf("%-10s%-10s%-10s%-10s", "name", "numOne", "numTwo",
"result");
while (StdIn.hasNextLine()) {
String name = StdIn.readString();
int m = StdIn.readInt();
int n = StdIn.readInt();
StdOut.printf("%-10s%-10d%-10d%-10.3f", name, m, n, (m * 1.0) / n);
System.out.println();
}
// name numOne numTwo result wwd 22 34 vadon 45 67
// wwd 22 34 0.647
// vadon 45 67 0.672
}
}
22.
package com.vadonmo.exp.chapter;
/**
* 使用1.1.6.4节中的rank()递归方法重新实现BinarySearch并跟踪该方法的调用。每当该方法被调用时,
* 打印出它的参数lo和hi并安照递归深度缩进。
*
* @author vadon
*
*/
public class Exp1_1_22 {
public static void main(String[] args) {
int[] array = { 1, 3, 5, 7, 9 };
rank(3, array, 0);
}
public static int rank(int key, int[] a, int deep) {
return rank(key, a, 0, a.length - 1, deep);
}
private static int rank(int key, int[] a, int lo, int hi, int deep) {
for (int i = 0; i < deep; ++i)
System.out.print(" ");
System.out.println("lo: " + lo + " hi: " + hi);
deep++;
if (lo > hi) {
return -1;
}
int mid = lo + (hi - lo) / 2;
if (key < a[mid]) {
return rank(key, a, lo, mid - 1, deep);
} else if (key > a[mid]) {
return rank(key, a, mid + 1, hi, deep);
} else {
return mid;
}
}
// lo: 0 hi: 4
// lo: 0 hi: 1
// lo: 1 hi: 1
}
23.
package com.vadonmo.exp.chapter;
import java.util.Arrays;
import com.vadonmo.exp.api.In;
import com.vadonmo.exp.api.StdIn;
import com.vadonmo.exp.api.StdOut;
/**
* 为BinarySearch的测试用例添加一个参数:+打印出标准输入中不在白名单上的值;-打印出出标准输入中在白名单上的值。
*
* @author vadon
*
*/
public class Exp1_1_23 {
public static boolean BinaryLookup(int key, int[] arr) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = low + ((high - low) >> 1);
if (key < arr[mid])
high = mid - 1;
else if (key > arr[mid])
low = mid + 1;
else
return true;
}
return false;
}
public static void main(String[] args) {
// “ + ” --> 打印出标准输入中不在白名单上的值,
// “ - ” --> 打印出标准输入中在白名单上的值
char symbol = '-';
int[] whitelist = new In(args[0]).readAllInts();
Arrays.sort(whitelist);
while (!StdIn.isEmpty()) {
int key = StdIn.readInt();
boolean found = BinaryLookup(key, whitelist);
if ('+' == symbol && !found)
StdOut.println(key);
if ('-' == symbol && found)
StdOut.println(key);
}
}
}
24.
package com.vadonmo.exp.chapter;
import com.vadonmo.exp.api.StdOut;
/**
* 给出使用欧几里德算法计算105 和24 的最大公约数的过程中得到的一系列p 和q的值。
* 扩展该算法中的代码得到一个程序Euclid,从命令行接受两个参数,计算它们的最大公约数并打印出每次调用递归方法时的两个参数。
* 使用你的程序计算1 111 111 和1 234 567 的最大公约数。
*
* @author vadon
*
*/
public class Exp1_1_24 {
public static void main(String[] args) {
StdOut.print(Euclid(105, 24) + "\n");
StdOut.print(Euclid(1111111, 1234567));
// p=105,q=24
// p=24,q=9
// p=9,q=6
// p=6,q=3
// p=3,q=0
// 3
// p=1111111,q=1234567
// p=1234567,q=1111111
// p=1111111,q=123456
// p=123456,q=7
// p=7,q=4
// p=4,q=3
// p=3,q=1
// p=1,q=0
// 1
}
public static long Euclid(int p, int q) {
StdOut.printf("p=%d,q=%d\n", p, q);
if (q == 0)
return p;
int r = p % q;
return Euclid(q, r);
}
}
25.
package com.vadonmo.exp.chapter;
/**
* 使用数学归纳法证明欧几里德算法能够计算任意一对非负整数p 和q 的最大公约数。
* @author vadon
*
*/
public class Exp1_1_25 {
}
26.
package com.vadonmo.exp.chapter;
public class Exp1_1_26 {
// 将三个数字排序。假设a、b、c 和t 都是同一种原始数字类型的变量。证明以下代码能够将a、 b、c 按照升序排列:
// if (a > b) { t = a; a = b; b = t; } // 保证a为a、b两数的较小者
//
// if (a > c) { t = a; a = c; c = t; } // 保证a为a、b、c三数中的最小者
//
// if (b > c) { t = b; b = c; c = t; } // 保证b为比a大的b、c两数的较小者,从而必有c为三数中的最大者
}
27.
package com.vadonmo.exp.chapter;
import com.vadonmo.exp.api.StdOut;
/**
* 二项分布。估计用以下代码计算binomial(100, 50, 0.25) 将会产生的递归调用次数:
* public static double binomial(int N,int k, double p) {
if(N == 0 && k == 0)
return 1.0;
if(N < 0 || k < 0)
return 0.0;
return (1.0 - p) * binomial(N-1, k, p) + p * binomial(N-1, k-1, p);
}
* 将已经计算过的值保存在数组中并给出一个更好的实现。
* @author vadon
*
*/
public class Exp1_1_27 {
public static double[][] Arr = new double[100 + 1][50 + 1];
public static void main(String[] args) {
for (int i = 0; i < 101; ++i)
for (int j = 0; j < 50 + 1; ++j)
Arr[i][j] = -1.0;
StdOut.print(binomial(100, 50, 0.25));
//4.507310875086383E-8
}
public static double binomial(int N, int k, double p) {
if (N < 0 || k < 0) {
return 0.0;
} else if (N == 0 && k == 0 && Arr[N][k] == -1.0) {
Arr[N][k] = 1.0;
return 1.0;
} else if (Arr[N][k] == -1.0) {
Arr[N][k] = (1.0 - p) * binomial(N - 1, k, p) + p
* binomial(N - 1, k - 1, p);
}
return Arr[N][k];
}
}