Java语言程序设计与数据结构(基础篇)课后练习题 第八章(二)

8.12

这个题不建议做,太繁琐,看懂就行。。。

import java.util.Scanner;

public class a {

public static void main(String[] args) {
		double[] rates= {0.10, 0.15, 0.25, 0.28, 0.33, 0.35};
		int[][] brackets= {
				{8350, 33950, 82250, 171550, 372950},//Single filer
				{16700, 67900, 137050, 208850, 372950},//Married jointly
				{8350, 33950, 68525, 104425, 186475},//Married separately
				{11950, 45500, 117450, 190200, 372950}//Head of a household
		};
		Scanner input = new Scanner(System.in);
		System.out.print("(0-single filer, 1-Married Joint or qualifying widow(er), "+ "2-Married Separately, 3-Head of a household)\n"+ "Enter the filong status:");
		int status=input.nextInt();
		System.out.print("Enter the taxable intcome: ");
	double taxableIncome=input.nextDouble();
	double tax=0;

			if(status==0) {
				if(taxableIncome<=8350)
					tax=brackets[0][0] * rates[0];
				else if(taxableIncome<=33950)
					tax=brackets[0][0] * rates[0]+
							(brackets[0][1] - brackets[0][0]) * rates[1];
				else if(taxableIncome<=82250)
					tax=brackets[0][0] * rates[0]+
							(brackets[0][1] - brackets[0][0]) * rates[1] +
							(brackets[0][2] - brackets[0][1]) * rates[2];
				else if(taxableIncome<=171550)
					tax=brackets[0][0] * rates[0]+
							(brackets[0][1] - brackets[0][0]) * rates[1] +
							(brackets[0][2] - brackets[0][1]) * rates[2] +
							(brackets[0][3] - brackets[0][2]) * rates[3]; 
				else if(taxableIncome<=372950)
					tax=brackets[1][0] * rates[0]+
							(brackets[1][1] - brackets[1][0]) * rates[1] +
							(brackets[1][2] - brackets[1][1]) * rates[2] +
							(brackets[1][3] - brackets[1][2]) * rates[3] +
							(brackets[1][4] - brackets[1][3]) * rates[4];
				else
					tax=brackets[0][0] * rates[0]+
							(brackets[0][1] - brackets[0][0]) * rates[1] +
							(brackets[0][2] - brackets[0][1]) * rates[2] +
							(brackets[0][3] - brackets[0][2]) * rates[3] +
							(brackets[0][4] - brackets[0][3]) * rates[4] +
							(taxableIncome - brackets[0][4]) * rates[5];   
			}else if(status==1) {
				if(taxableIncome<=16700)
					tax=brackets[1][0] * rates[0];
				else if(taxableIncome<=67900)
					tax=brackets[1][0] * rates[0]+
							(brackets[1][1] - brackets[1][0]) * rates[1];
				else if(taxableIncome<=137050)
					tax=brackets[1][0] * rates[0]+
							(brackets[1][1] - brackets[1][0]) * rates[1] +
							(brackets[1][2] - brackets[1][1]) * rates[2];
				else if(taxableIncome<=208850)
					tax=brackets[1][0] * rates[0]+
							(brackets[1][1] - brackets[1][0]) * rates[1] +
							(brackets[1][2] - brackets[1][1]) * rates[2] +
							(brackets[1][3] - brackets[1][2]) * rates[3];
				else if(taxableIncome<=372950)
					tax=brackets[1][0] * rates[0]+
							(brackets[1][1] - brackets[1][0]) * rates[1] +
							(brackets[1][2] - brackets[1][1]) * rates[2] +
							(brackets[1][3] - brackets[1][2]) * rates[3] +
							(brackets[1][4] - brackets[1][3]) * rates[4];
				else
					tax=brackets[1][0] * rates[0]+
							(brackets[1][1] - brackets[1][0]) * rates[1] +
							(brackets[1][2] - brackets[1][1]) * rates[2] +
							(brackets[1][3] - brackets[1][2]) * rates[3] +
							(brackets[1][4] - brackets[1][3]) * rates[4] +
							(taxableIncome - brackets[1][4]) * rates[5];  
			}else if(status==2) {
				if(taxableIncome<=8350)
					tax=brackets[2][0] * rates[0];
				else if(taxableIncome<=33950)
					tax=brackets[2][0] * rates[0]+
							(brackets[2][1] - brackets[2][0]) * rates[1];
				else if(taxableIncome<=68525)
					tax=brackets[2][0] * rates[0]+
							(brackets[2][1] - brackets[2][0]) * rates[1] +
							(brackets[2][2] - brackets[2][1]) * rates[2];
				else if(taxableIncome<=104425)
					tax=brackets[2][0] * rates[0]+
							(brackets[2][1] - brackets[2][0]) * rates[1] +
							(brackets[2][2] - brackets[2][1]) * rates[2] +
							(brackets[2][3] - brackets[2][2]) * rates[3];
				else if(taxableIncome<=186475)
					tax=brackets[2][0] * rates[0]+
							(brackets[2][1] - brackets[2][0]) * rates[1] +
							(brackets[2][2] - brackets[2][1]) * rates[2] +
							(brackets[2][3] - brackets[2][2]) * rates[3] +
							(brackets[2][4] - brackets[2][3]) * rates[4] ;
				else
					tax=brackets[2][0] * rates[0]+
							(brackets[2][1] - brackets[2][0]) * rates[1] +
							(brackets[2][2] - brackets[2][1]) * rates[2] +
							(brackets[2][3] - brackets[2][2]) * rates[3] +
							(brackets[2][4] - brackets[2][3]) * rates[4] +
							(taxableIncome - brackets[2][4]) * rates[5] ;
			}else if(status==3) {
				if(taxableIncome<=11950)
					tax=brackets[3][0] * rates[0];
				else if(taxableIncome<=45500)
					tax=11950*0.1+(taxableIncome-11950)*0.15;
				else if(taxableIncome<=117450)
					tax=11950*0.1+(45500-11950)*0.15+(taxableIncome-45500)*0.25;
				else if(taxableIncome<=190200)
					tax=11950*0.1+(45500-11950)*0.15+(117450-45500)*0.25+(taxableIncome-117450)*0.28;
				else if(taxableIncome<=372950)
					tax=11950*0.1+(45500-11950)*0.15+(117450-45500)*0.25+(190200-117450)*0.28+(taxableIncome-190200)*0.33;
				else
					tax=brackets[3][0] * rates[0]+
							(brackets[3][1] - brackets[3][0]) * rates[1] +
							(brackets[3][2] - brackets[3][1]) * rates[2] +
							(brackets[3][3] - brackets[3][2]) * rates[3] +
							(brackets[3][4] - brackets[3][3]) * rates[4] +
							(taxableIncome - brackets[3][4]) * rates[5];

			}else{
				System.out.println("Error: invalid status");
				System.exit(1);
			}
			
			System.out.println("Tax is "+(int)(tax * 100) / 100.0);	
}

}

8.13

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
    System.out.print("Enter the number of rows and columns of the array: ");
    Scanner input = new Scanner(System.in);
    int r, c;
    r = input.nextInt();
    c = input.nextInt();
    double[][] a = new double[r][c];
    System.out.print("Enter the array: \n");

    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[i].length; j++) {
            a[i][j] = input.nextDouble();
        }
    }

    int[] p = new int[2];
    p = locateLargest(a);
    System.out.print("The location of the largest element is at (" + p[0] + "," + p[1] + ")");
}

private static int[] locateLargest(double[][] a) {
    int tmpArry[]=new int[2];
    double tmp = 0;
    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[i].length; j++) {
            if(tmp<a[i][j]){
                tmp=a[i][j];
                tmpArry[0]=i;
                tmpArry[1]=j;
            }
        }
    }
    return tmpArry;
}

}

8.14

import java.util.Random;
import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
	System.out.print("Enter the size for the matrix: ");
	Scanner input = new Scanner(System.in);
	int row = input.nextInt();
	Random random = new Random();
	int[][] a = new int[row + 1][row + 1];
	
	for (int i = 1; i <= row; i++) {
		for (int j = 1; j <= row; j++) {
			a[i][j] = random.nextInt(2);
			System.out.print(a[i][j]);
		}
		System.out.println();
	}
	
	int sum1 = 0, sum2 = 0, sum3 = 0, sum4 = 0;

	for (int i = 1; i <= row; i++) {
		sum1 = 0;
		sum2 = 0;
		for (int j = 1; j <= row; j++) {
			sum1 += a[i][j];
			sum2 += a[j][i];
		}
		
		if (sum1 == 0) {
			System.out.println("All 0s on row " + i);
		}else if (sum1 == 4) {
			System.out.println("All 1s on row " + i);
		}
		
		if (sum2 == 0) {
			System.out.println("All 0s on column " + i );
		} else if (sum2 == 4) {
			System.out.println("All 1s on column" + i);
		}

	}

	for (int i = 1; i <= row; i++) {
		sum3 += a[i][i];
	}

	if (sum3 == 0) {
		System.out.println("All 0s on major diagonal");
	}else if (sum3 == 4) {
		System.out.println("All 1s on major diagonal");
	} else {
		System.out.println("No same numbers on the major diagonal");
	}
	for (int i = 1; i <= row; i++) {
		sum4 += a[i][row + 1 - i];
	}

	if (sum4 == 0) {
		System.out.println("All 0s on sub-diagonal");
	} else if (sum4 == 4) {
		System.out.println("All 1s on sub-diagonal");
	} else {
		System.out.println("No same numbers on the sub-diagonal");

	}

	input.close();

}

}

8.15

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
				System.out.print("Enter five points: ");
				Scanner input = new Scanner(System.in);
				double[][] points = new double[5][2];
				for(int i=0;i<5;i++)
					for(int j=0;j<2;j++)
						points[i][j]=input.nextDouble();
				if(onTheSameLine(points[0][0], points[0][1], points[1][0], points[1][1], points[2][0], points[2][1])
						&& onTheSameLine(points[0][0], points[0][1], points[1][0], points[1][1], points[3][0], points[3][1])
						&& onTheSameLine(points[0][0], points[0][1], points[1][0], points[1][1], points[4][0], points[4][1]))
					System.out.print("The five points are on the same line");
				else
					System.out.print("The five points are not on the same line");
	}
	public static boolean onTheSameLine(double x0,double y0,double x1,double y1,double x2,double y2){
	    return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0)==0;
}

}

8.16

import java.util.Scanner;
public class dibazhang {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter row and column: ");
int r = input.nextInt();
int c = input.nextInt();
int[][] m = new int[r][c];
	System.out.print("Enter the values: ");
for (int i = 0; i < r; i++)
	for (int j = 0; j < c; j++)
		m[i][j] = input.nextInt();
sort(m);
}
public static void sort(int[][] m) {
	int count = 0;
	while (count < m.length) {
    	for (int i = 0; i < m.length - 1; i++) {
        // 遍历m[i]数组中的值,判断m[i],m[i+1]是否交换位置
        for (int j = 0; j < m[i].length; j++) {
            if (m[i][j] > m[i + 1][j]) {
                int temp[];
                temp = m[i];
                m[i] = m[i + 1];
                m[i + 1] = temp;
                j = 0;
                break;
            }
            if (m[i][j] < m[i + 1][j]) {
                break;
            }
            if (m[i][j] == m[i + 1][j]) {
                continue;
            }
        }
    }
    count++;
}
// print
	System.out.print("sorted: ");
	for (int[] j : m) {
		System.out.print("(");
		for (int i = 0; i < j.length; i++) {
			System.out.printf(" %d ", j[i]);
		 }
	System.out.print(")   ");
}
// 4 2 1 7 4 5 1 2 1 1 4 1}

}

8.17

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int limit = input.nextInt();
        double[][] loans  =new double[n][n];
        double[] balances = new double[n];
        for(int i=0;i<n;i++){
            balances[i] = input.nextDouble();
            int ass = input.nextInt();
            for(int j=0;j<ass;j++){
                int borrower = input.nextInt();
                loans[i][borrower]=input.nextDouble();
            }
        }
        System.out.print("Unsafe banks are ");
        boolean[] printed = new boolean[n];
        for(int i=0;i<n;i++)
            printed[i]=false;
        while(true){
            boolean changed = false;
            for(int i=0;i<n;i++){
                double sum = balances[i];
                for(int j=0;j<n;j++)
                    sum+=loans[i][j];
                if(sum<limit){
                    if(!printed[i]) {
                        System.out.print(i + " ");
                        printed[i] = true;
                        changed=true;
                    }
                    for(int k=0;k<n;k++)
                        loans[k][i]=0;
                }
            }
            if(!changed)
                break;
        }
}

}

8.18

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
	int[][] m = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 }, { 9, 10 } };
	shuffle(m);
}
public static void shuffle(int m[][]) {
	 // 判断是否为空
	 if (m == null) {
    		return;
	 }
	 // 获取原数组信息 n*m 以及 length
	int N = m.length;
	int M = m[0].length;
	int len = N * M;
	// 生成一个和原来二维数组规格一样的数组
	int[][] copy = new int[N][M];
	// 生成随机数下标,存放到数组中	
	// 存放行坐标
	List indexI = new ArrayList();
	// 存放列坐标
	List indexJ = new ArrayList();
	Random random = new Random();
	while (indexI.size() != N) {
		int ran = random.nextInt(N);
		if (!indexI.contains(ran)) {
			indexI.add(ran);
		}
	}
	while (indexJ.size() != M) {
		int ran = random.nextInt(M);
		if (!indexJ.contains(ran)) {	
			indexJ.add(ran);
		}
	}
	// 使用原数组为新数组赋值
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			int p = (int) indexI.get(i);
			int q = (int) indexJ.get(j);
			copy[i][j] = m[p][q];
		}
	}
	// print
	m = copy;
	for (int i = 0; i < N; i++) {
		System.out.print("(");
		for (int j = 0; j < M; j++) {
			System.out.printf(" %d ", m[i][j]);
		}
		System.out.println(")");
	}
}

}

8.19

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		int rows;
		int columns;
		System.out.print("Enter the number of rows: ");
		rows = input.nextInt();
		System.out.print("Enter the number of columns: ");
		columns = input.nextInt();
		int[][] jesus = new int[rows][columns];
		System.out.println("Enter the matrix: ");
		for(int i=0;i<rows;i++)
			for(int j=0;j<columns;j++)
				jesus[i][j] = input.nextInt();
		System.out.println(isConsecutiveFour(jesus));
}
public static boolean isConsecutiveFour(int[][] values){
		int rows = values.length;
		int columns = values[0].length;
//			从行,列,对角,反对角入手
			for(int i=0;i<rows;i++)
				for(int j=0;j<=columns-4;j++)
					if(values[i][j]==values[i][j+1]&&values[i][j]==values[i][j+2]&&values[i][j]==values[i][j+3])
	                    return true;
			for(int i=0;i<columns;i++)
				for(int j=0;j<=rows-4;j++)
					if(values[j][i]==values[j+1][i]&&values[j][i]==values[j+2][i]&&values[j][i]==values[j+3][i])
	                    return true;
			for(int i=3;i<rows;i++)
	            for(int j=0;j<columns-3;j++)
	                if(values[i][j]==values[i-1][j+1]&&values[i][j]==values[i-2][j+2]&&values[i][j]==values[i-3][j+3])
	                    return true;
	        for(int i=0;i<rows-3;i++)
	            for(int j=0;j<columns-3;j++)
	                if(values[i][j]==values[i+1][j+1]&&values[i][j]==values[i+2][j+2]&&values[i][j]==values[i+3][j+3])
	                    return true;
	        return false;
}

}

8.20

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
			// TODO Auto-generated method stub
			Scanner input = new Scanner(System.in);
			String[] redYellows = {"red","yellow"};
			int ry = 0;
			int[] ptrs = {5,5,5,5,5,5};
			int[][] map = new int[6][7];
			while(true){
				printMap(map);
				if(isConsecutiveFour(map,1)){
					System.out.println("The red player won");
					break;
				}
				if(isConsecutiveFour(map, 2)){
					System.out.println("The yellow player won");
				}
				System.out.print("Drop a "+redYellows[ry%2]+" disk at column (0-6): ");
				int num = input.nextInt();
				map[ptrs[num]][num]=ry%2+1;
				ptrs[num]--;
				ry++;
			}
	}
	public static boolean isConsecutiveFour(int[][] values,int num){
			int rows = values.length;
			int columns = values[0].length;
	//			从行,列,对角,反对角入手
				for(int i=0;i<rows;i++)
					for(int j=0;j<=columns-4;j++)
						if(values[i][j]==values[i][j+1]&&values[i][j]==values[i][j+2]&&values[i][j]==values[i][j+3]&&values[i][j]==num)
		                    return true;
				for(int i=0;i<columns;i++)
					for(int j=0;j<=rows-4;j++)
						if(values[j][i]==values[j+1][i]&&values[j][i]==values[j+2][i]&&values[j][i]==values[j+3][i]&&values[j][i]==num)
		                    return true;
				for(int i=3;i<rows;i++)
		            for(int j=0;j<columns-3;j++)
		                if(values[i][j]==values[i-1][j+1]&&values[i][j]==values[i-2][j+2]&&values[i][j]==values[i-3][j+3]&&values[i][j]==num)
		                    return true;
		        for(int i=0;i<rows-3;i++)
		            for(int j=0;j<columns-3;j++)
		                if(values[i][j]==values[i+1][j+1]&&values[i][j]==values[i+2][j+2]&&values[i][j]==values[i+3][j+3]&&values[i][j]==num)
		                    return true;
		        return false;
}
public static void printMap(int[][] values){
	for(int i=0;i<6;i++){
        for(int j=0;j<7;j++){
            System.out.print("|");
            if(values[i][j]==0)
                System.out.print(" ");
            else if(values[i][j]==1)
                System.out.print("R");
            else if(values[i][j]==2)
                System.out.print("Y");
        }
        System.out.println("|");
    }
    System.out.println("---------------");
}

}

8.21

import java.util.Scanner;

public class dibazhang {

public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the number of cities: ");
		int nums = input.nextInt();
		System.out.print("Enter the coordinates of the cities: ");
		double[][] coor = new double[nums][2];
		for (int i = 0; i < nums; i++)
			for (int j = 0; j < 2; j++)
				coor[i][j] = input.nextDouble();
		double min = totalDistance(coor, 0);
		int index = 0;
		for (int i = 0; i < coor.length; i++) {
			double total = totalDistance(coor, i);
			if (min > total) {
				min = total;
				index = i;
			}
		}
		System.out.println("The central city is at (" + coor[index][0] + ", " + coor[index][1] + ")");
		System.out.printf("The total distance to all other cities is %4.2f", min);
}

public static double totalDistance(double[][] coor, int i) {
	double totalDistance = 0;
	for (int j = 0; j < coor.length; j++)
		totalDistance += distance(coor[i], coor[j]); // 二维数据是一位数组的数组!!!!
	return totalDistance;
}

public static double distance(double[] c1, double[] c2) {
	return Math.sqrt((c1[0] - c2[0]) * (c1[0] - c2[0]) + (c1[1] - c2[1]) * (c1[1] - c2[1]));
}

}

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值