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

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
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;

}

总结

就写到这了,也算是给这段时间的面试做一个总结,查漏补缺,祝自己好运吧,也希望正在求职或者打算跳槽的 程序员看到这个文章能有一点点帮助或收获,我就心满意足了。多思考,多问为什么。希望小伙伴们早点收到满意的offer! 越努力越幸运!

金九银十已经过了,就目前国内的面试模式来讲,在面试前积极的准备面试,复习整个 Java 知识体系将变得非常重要,可以很负责任的说一句,复习准备的是否充分,将直接影响你入职的成功率。但很多小伙伴却苦于没有合适的资料来回顾整个 Java 知识体系,或者有的小伙伴可能都不知道该从哪里开始复习。我偶然得到一份整理的资料,不论是从整个 Java 知识体系,还是从面试的角度来看,都是一份含技术量很高的资料。

三面蚂蚁核心金融部,Java开发岗(缓存+一致性哈希+分布式)

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
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;

}

总结

就写到这了,也算是给这段时间的面试做一个总结,查漏补缺,祝自己好运吧,也希望正在求职或者打算跳槽的 程序员看到这个文章能有一点点帮助或收获,我就心满意足了。多思考,多问为什么。希望小伙伴们早点收到满意的offer! 越努力越幸运!

金九银十已经过了,就目前国内的面试模式来讲,在面试前积极的准备面试,复习整个 Java 知识体系将变得非常重要,可以很负责任的说一句,复习准备的是否充分,将直接影响你入职的成功率。但很多小伙伴却苦于没有合适的资料来回顾整个 Java 知识体系,或者有的小伙伴可能都不知道该从哪里开始复习。我偶然得到一份整理的资料,不论是从整个 Java 知识体系,还是从面试的角度来看,都是一份含技术量很高的资料。

[外链图片转存中…(img-1hAmmbWj-1714693155892)]

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值