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

《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<i;j++)
                System.out.printf("%5d",(int)(Math.pow(2,j)));
            for(int j=i;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<i;j++)
            {
                if(i%j==0)
                {
                    isPrime = false;
                    break;
                }
            }
            if(isPrime)
            {
                count++;
                System.out.printf("%-5d",i);
                if(count%8==0&&count!=lineBase)
                {
                    System.out.print("\n");
                    lineBase+=8;
                }
            }
        }
    }
}

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);
        }
    }
}
发布了75 篇原创文章 · 获赞 61 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/swy_swy_swy/article/details/104468514