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

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

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

6.27

public class book {
    public static void main(String[] args)
    {
        int count=0;
        int base=0;
        int dick = 2;
        while(count<100)
        {
            if(isPrime(dick)&&!isPalindrome(dick)&&isPrime(reverse(dick)))
            {
                System.out.print(dick+" ");
                count++;
            }
            if(count%10==0&&count!=base) {
                System.out.print('\n');
                base+=10;
            }
            dick++;
        }
    }
    public static boolean isPrime(int n)
    {
        for(int i=2;i<=n/2;i++)
        {
            if(n%i==0)
                return false;
        }
        return true;
    }
    public static boolean isPalindrome(int n)
    {
        String ass = Integer.toString(n);
        for(int i=0;i<ass.length();i++)
        {
            if(ass.charAt(i)!=ass.charAt(ass.length()-1-i))
                return false;
        }
        return true;
    }
    public static int reverse(int n)
    {
        String ass = Integer.toString(n);
        String dick = "";
        for(int i=ass.length()-1;i>=0;i--)
            dick+=ass.charAt(i);
        return Integer.parseInt(dick);
    }
}

6.28

public class book {
    public static void main(String[] args)
    {
        System.out.println("p           2^p-1");
        System.out.println("-----------------");
        for(int i=2;i<=31;i++)
        {
            int jb = (int)Math.pow(2,i)-1;
            if(isPrime(jb))
                System.out.printf("%d\t\t%-2d\n",i,jb);
        }
    }
    public static boolean isPrime(int n)
    {
        for(int i=2;i<=n/2;i++)
        {
            if(n%i==0)
                return false;
        }
        return true;
    }
}

6.29

public class book {
    public static void main(String[] args)
    {
        for(int i=2;i<=997;i++)
        {
            if(isPrime(i)&&isPrime(i+2))
                System.out.println("("+i+", "+(i+2)+")");
        }
    }
    public static boolean isPrime(int n)
    {
        for(int i=2;i<=n/2;i++)
        {
            if(n%i==0)
                return false;
        }
        return true;
    }
}

6.30

public class book {
    public static void main(String[] args)
    {
        dice();
    }
    public static boolean dice()
    {
        int d1 = (int)(Math.random()*6)+1;
        int d2 = (int)(Math.random()*6)+1;
        int sum = d1+d2;
        if(sum==2||sum==3||sum==12) {
            System.out.printf("You rolled %d+%d = %d\nYou lose\n",d1,d2,sum);
            return false;
        }
        else if(sum==7||sum==11) {
            System.out.printf("You rolled %d+%d = %d\nYou win\n",d1,d2,sum);
            return true;
        }
        else
        {
            System.out.printf("You rolled %d+%d = %d\n",d1,d2,sum);
            System.out.println("point is "+sum);
            int old = sum;
            do{
                int j1 = (int)(Math.random()*6)+1;
                int j2 = (int)(Math.random()*6)+1;
                sum=j1+j2;
                System.out.printf("You rolled %d+%d = %d\n",j1,j2,sum);
                if(sum==7) {
                    System.out.println("You lose");
                    return false;
                }
                else if(sum!=old)
                    System.out.println("point is "+sum);
            }while(sum!=old);
            System.out.println("You win");
            return true;
        }
    }
}

6.31

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a credit card number: ");
        String ass = input.next();
        if(isValid(ass))
            System.out.println(ass+" is valid");
        else
            System.out.println(ass+" is invalid");
    }
    public static boolean isValid(String number)
    {
        if(number.length()<13||number.length()>16)
            return false;
        else
        {
            return (sumOfDoubleEvenPlace(number)+sumOfOddPlace(number))%10==0;
        }
    }
    public static int sumOfDoubleEvenPlace(String number)
    {
        int sum=0;
        for(int i=number.length()-2;i>=0;i-=2)
            sum+=getDigit(2*(number.charAt(i)-'0'));
        return sum;
    }
    public static int getDigit(int number)
    {
        if(number<10)
            return number;
        else
            return number/10+number%10;
    }
    public static int sumOfOddPlace(String number)
    {
        int sum=0;
        for(int i=number.length()-1;i>=0;i-=2)
            sum+=(int)(number.charAt(i)-'0');
        return sum;
    }
    public static boolean prefixMatched(String number)
    {
        char c1=number.charAt(0);
        char c2=number.charAt(1);
        return c1 == '4' || c1 == '5' || c1 == '6' || (c1 == '3') && (c2 == 7);
    }
}

6.32

public class book {
    public static void main(String[] args)
    {
        int winCount=0;
        for(int i=0;i<10000;i++)
        {
            if(dice())
                winCount++;
        }
        System.out.println("You won "+winCount+" times");
    }
    public static boolean dice()
    {
        int d1 = (int)(Math.random()*6)+1;
        int d2 = (int)(Math.random()*6)+1;
        int sum = d1+d2;
        if(sum==2||sum==3||sum==12) {
            return false;
        }
        else if(sum==7||sum==11) {
            return true;
        }
        else
        {
            int old = sum;
            do{
                int j1 = (int)(Math.random()*6)+1;
                int j2 = (int)(Math.random()*6)+1;
                sum=j1+j2;
                if(sum==7) {
                    return false;
                }
            }while(sum!=old);
            return true;
        }
    }
}

6.33

🐰🌶

6.34

见3.21

6.35

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the side: ");
        double ass = input.nextDouble();
        System.out.println("The area of the pentagon is "+area(ass));
    }
    public static double area(double side)
    {
        return 5*side*side/(4*Math.tan(3.141592654/5));
    }
}

6.36

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter thr number of sides: ");
        int n=input.nextInt();
        System.out.print("Enter the side: ");
        double ass = input.nextDouble();
        System.out.println("The area of the polygon is "+area(n,ass));
    }
    public static double area(int n,double side)
    {
        return n*side*side/(4*Math.tan(3.141592654/n));
    }
}

6.37

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number : ");
        int n=input.nextInt();
        System.out.print("Enter the width : ");
        int w = input.nextInt();
        System.out.println("The format of the number is "+format(n,w));
    }
    public static String format(int number,int width)
    {
        String ass = Integer.toString(number);
        int len = ass.length();
        if(len>=width)
            return ass;
        else
        {
            String dick="";
            for(int i=1;i<=width-len;i++)
                dick+='0';
            return dick+ass;
        }
    }
}

6.38

public class book {
    public static void main(String[] args)
    {
        for(int i=1;i<=100;i++)
        {
            System.out.print(getRandomUpperCaseLetter()+" ");
            if(i%10==0)
                System.out.print('\n');
        }
        for(int i=1;i<=100;i++)
        {
            System.out.print(getRandomDigitCharacter()+" ");
            if(i%10==0)
                System.out.print('\n');
        }
    }
    public static char getRandomCharacter(char ch1,char ch2)
    {
        return (char)(ch1+Math.random()*(ch2-ch1+1));
    }
    public static char getRandomUpperCaseLetter()
    {
        return getRandomCharacter('A','Z');
    }
    public static char getRandomDigitCharacter()
    {
        return getRandomCharacter('0','9');
    }
}

6.39

import java.util.Scanner;
public class book {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter three points for p0,p1,p2: ");
        double x0 = input.nextDouble();
        double y0 = input.nextDouble();
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        if(leftOfTheLine(x0,y0,x1,y1,x2,y2))
            System.out.println("On the left");
        else if(onTheLineSegment(x0,y0,x1,y1,x2,y2))
            System.out.println("On the line segment");
        else if(onTheSameLine(x0,y0,x1,y1,x2,y2))
            System.out.println("On the same line");
        else
            System.out.println("On the right");
    }
    public static boolean leftOfTheLine(double x0,double y0,
                                        double x1,double y1,double x2,double y2)
    {
        return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0)>0;
    }
    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;
    }
    public static boolean onTheLineSegment(double x0,double y0,
                                        double x1,double y1,double x2,double y2)
    {
        return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0)==0&&x2>=x0&&x2<=x1;
    }
}

第六章 完

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值