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

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

《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
=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; }}

第六章 完

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

你可能感兴趣的文章
python read( )函数
查看>>
python readline()函数
查看>>
python readlines()函数
查看>>
python writelines()函数
查看>>
python 文件读写5个实例
查看>>
python 文件读写项目实践
查看>>
python的 os 和 shutil 模块
查看>>
python 如何反转序列
查看>>
python str.join()
查看>>
python 内置函数 reversed()
查看>>
python sort()方法
查看>>
python sorted()函数
查看>>
python reverse()方法
查看>>
Python sort( ) sorted( ) reverse( ) reversed( ) 总结
查看>>
python 工厂函数
查看>>
python 序列类型可用的内建函数
查看>>
python 如何输出百分数(如23%)
查看>>
python 字符串内建函数
查看>>
python 回文字符串 回文数字
查看>>
python字符串内建函数str.index()和str.rindex()
查看>>