博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《Java语言程序设计与数据结构》编程练习答案(第五章)(二)
阅读量:4169 次
发布时间:2019-05-26

本文共 6819 字,大约阅读时间需要 22 分钟。

《Java语言程序设计与数据结构》编程练习答案(第五章)(二)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition

5.14

import java.util.Scanner;public class book {
public static void main(String[] args) {
System.out.print("Enter two integers: "); Scanner input = new Scanner(System.in); int n1 = input.nextInt(); int n2 = input.nextInt(); int jb = Math.min(n1,n2); for(int i=jb;i>=1;i--) {
if(n1%i==0&&n2%i==0) {
System.out.println("The gcd is "+i); break; } } }}

5.15

public class book {
public static void main(String[] args) {
char ass = '!'; int count = 0; while(ass<='~') {
System.out.print(ass+" "); count++; ass++; if(count%10==0) System.out.print("\n"); } }}

5.16

import java.util.Scanner;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int ass = input.nextInt(); while(ass!=1) {
for(int i=2;i<=ass;i++) {
if(ass%i==0) {
ass=ass/i; System.out.print(i+" "); break; } } } }}

5.17

import java.util.Scanner;public class book {
public static void main(String[] args) {
System.out.print("Enter the number of lines: "); Scanner input = new Scanner(System.in); int lines = input.nextInt(); for(int i=1;i<=lines;i++) {
for(int j=1;j<=7-i;j++) System.out.print(" "); for(int j=i;j>1;j--) System.out.print(j+" "); for(int j=1;j<=i;j++) System.out.print(j+" "); System.out.print('\n'); } }}

5.18

public class book {
public static void main(String[] args) {
for(int i=1;i<=6;i++) {
for(int j=1;j<=i;j++) System.out.print(j+" "); System.out.print('\n'); } for(int i=1;i<=6;i++) {
for(int j=1;j<=7-i;j++) System.out.print(j+" "); System.out.print('\n'); } for(int i=1;i<=6;i++) {
for(int j=1;j<=6-i;j++) System.out.print(" "); for(int j=i;j>=1;j--) System.out.print(j+" "); System.out.print('\n'); } for(int i=1;i<=6;i++) {
for(int j=1;j<=i-1;j++) System.out.print(" "); for(int j=1;j<=7-i;j++) System.out.print(j+" "); System.out.print('\n'); } }}

5.19

public class book {
public static void main(String[] args) {
for(int i=0;i<8;i++) {
for(int j=1;j<=7-i;j++) System.out.print(" "); for(int j=0;j
=0;j--) System.out.printf("%5d",(int)(Math.pow(2,j))); System.out.print('\n'); } }}

5.20

public class book {
public static void main(String[] args) {
int lineBase = 0; int count = 0; for(int i = 2;i <= 1000;i++) {
boolean isPrime = true; for(int j = 2;j

5.21

import java.util.Scanner;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Loan Amount: "); double amount = input.nextDouble(); System.out.print("Number of Years: "); int years = input.nextInt(); System.out.println("Interest Rate Monthly Payment Total Payment"); for(int i = 0;i<25;i++) {
double rate = (0.125*i+5)/1200; double monthMoney = amount*rate/(1-1/Math.pow(1+rate,years*12)); System.out.printf("%-4.3f%% %-5.2f %-7.2f\n",0.125*i+5,monthMoney,monthMoney*12*years); } }}

5.22

import java.util.Scanner;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Loan Amount: "); double amount = input.nextDouble(); System.out.print("Number of Years: "); int years = input.nextInt(); System.out.print("Annual Interest Rate: "); double annualRate = input.nextDouble(); double monthRate = annualRate/1200; double monthPay = amount*monthRate/(1-1/(Math.pow(1+monthRate,12*years))); double total = monthPay*years*12; System.out.printf("Monthly Payment: %.2f\n",monthPay); System.out.printf("Total Payment: %.2f\n",total); System.out.println("Payment# Interest Principle Balance"); double balance = amount; for(int i=1;i<=years*12;i++) {
double interest = monthRate*balance; double principle = monthPay-interest; balance = balance-principle; System.out.printf("%d\t\t %.2f\t\t%.2f\t\t%.2f\n",i,interest,principle,balance); } }}

5.23

public class book {
public static void main(String[] args) {
double l2r = 0; double r2l = 0; double base1 = 1.0; double base2 =50000.0; for(int i=1;i<=50000;i++) {
l2r+=1/base1; base1+=1; } for(int i=1;i<=50000;i++) {
r2l+=1/base2; base2-=1; } System.out.println("From left to right: "+l2r); System.out.println("From right to left: "+r2l); }}

5.24

public class book {
public static void main(String[] args) {
double sum = 0; for(int i =1;i<=97;i+=2) {
sum+=1.0*i/(i+2); } System.out.println("The answer is "+sum); }}

5.25

public class book {
public static void main(String[] args) {
double item = 0.0; double sum = 0.0; for(int i=10000;i<=100000;i+=10000) {
item = 1.0; sum = 0.0; for(int j=1;j<=i;j++) {
sum+=1.0*item/(2*j-1); item*=(-1); } System.out.printf("i=%d, pi=%f\n",i,sum*4); } }}

5.26

public class book {
public static void main(String[] args) {
for(int i =10000;i<=100000;i+=10000) {
double item = 1.0; double e = 1.0; for(int j=1;j<=i;j++) {
item/=j; e+=item; } System.out.printf("i=%d, e=%f\n",i,e); } }}

转载地址:http://kuwai.baihongyu.com/

你可能感兴趣的文章
BIM+GIS应用的八大挑战
查看>>
.net实现.aspx页面自动加载.cs程序定义的变量并按照格式输出
查看>>
[Leetcode]最后一个单词的长度
查看>>
merges sort use c++
查看>>
插入排序用递归实现
查看>>
工作流审批平台-审批流程-指定审批部门
查看>>
商务智能-系统概述-数据图形方式
查看>>
软件项目管理系统-项目管理-模块定义-开发内容
查看>>
工作流审批平台-审批功能
查看>>
商务智能-基本方法-特征与角度
查看>>
软件项目管理系统-项目管理-模块定义-开发笔记
查看>>
工作流审批平台-业务申请-申请书一览
查看>>
商务智能-基本方法-数据钻取
查看>>
C++程序员技术需求规划(发展方向)
查看>>
如何判断变量在内存中如何放置的?低位在前还是高位在前
查看>>
c语言中通过指针将数值赋值到制定内存地址
查看>>
64位与32位linux c开发时默认字节对齐值
查看>>
malloc(malloc在32位编译系统中分配的地址会8字节对齐,64为编译系统中会8或者16字节对齐)
查看>>
初始化时共享内存的key值和信号量初始化的key值可以一样
查看>>
linux创建线程之pthread_create
查看>>