《Java语言程序设计(基础篇)》(第10版梁勇著)
第七章练习题答案
7.1
public class Exercise07_01 {
/** Main method */
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
// Get number of students
System.out.print("Enter number of students: ");
int numberOfStudents = Int();
double[] scores = new double[numberOfStudents]; // Array scores
double best = 0; // The best score
// Read scores and find the best score
System.out.print("Enter " + numberOfStudents + " scores: ");
for (int i = 0; i < scores.length; i++) {
scores[i] = Double();
if (scores[i] > best)
best = scores[i];
}
// Declare and initialize output string
char grade; // The grade
/
/ Assign and display grades
for (int i = 0; i < scores.length; i++) {
if (scores[i] >= best - 10)
grade = 'A';
else if (scores[i] >= best - 20)
grade = 'B';
else if (scores[i] >= best - 30)
grade = 'C';
else if (scores[i] >= best - 40)
grade = 'D';
else
grade = 'F';
System.out.println("Student " + i + " score is " +
scores[i] + " and grade is " + grade);
}
}
}
7.1附加
public class Exercise07_01Extra {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
boolean[] isCovered = new boolean[99]; // Default false
/
/ Read all numbers and mark corresponding element covered int number = Int();
while (number != 0) {
if (number >= 0 && number <= 99)
isCovered[number - 1] = true;
number = Int();
}
// Check if all covered
boolean allCovered = true; // Assume all covered
for (int i = 0; i < 99; i++)
if (!isCovered[i]) {
System.out.print((i + 1) + " ");
allCovered = false; // Find one number if not covered
}
// Display result
if (allCovered)
System.out.println("The tickets cover all numbers.");
else
System.out.println(" are the missing numbers.");
}
}
7.2
public class Exercise07_02 {
public static void main (String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
int[] num = new int[10];
for (int i = 0; i < 10; i++) {
// Read a number
System.out.print(
"Read a number: ");
num[i] = Int();
}
// Display the array
for (int i = 9; i >= 0; i--) {
System.out.println(num[i]);
}
}
}
7.2附加
public class Exercise07_02Extra {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the coordinates of six points: ");
double x[] = new double[6];
double y[] = new double[6];
for (int i = 0; i < x.length; i++) {
x[i] = Double();
y[i] = Double();
}
double total = 0;
for (int i = 1; i < x.length - 1; i++) {
total += getArea(x[0], y[0], x[i], y[i], x[i + 1], y[i + 1]);
java程序设计基础视频}
System.out.println("The total area is " + total);
}
public static double getArea(double x1, double y1, double x2, double y2, double x3, double y3) {
double s1 = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 -y2));
double s2 = Math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 -y3));
double s3 = Math.sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 -y2));
double s = (s1 + s2 + s3) / 2;
return Math.sqrt(s * (s - s1) * (s - s2) * (s - s3));
}
}
7.3
public class Exercise07_03 {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
int[] counts = new int[100];
System.out.print("Enter the integers between 1 and 100: ");
// Read all numbers
int number = Int(); // number read from a file
while (number != 0) {
if (number <= 100 && number >= 0)
counts[number - 1]++;
number = Int();
}
// Display result
for (int i = 0; i < 100; i++) {
if (counts[i] > 0)
System.out.println((i + 1) + " occurs " + counts[i]
+ ((counts[i] == 1) ? " time" : " times"));
}
}
}
7.3附加
public class Exercise07_03Extra {
public static void main(String[] args) {
int count = 0; // How many solutions are found?
// Queen positions
int[] queens = new int[8]; // queens are placed at (i, queens[i])
for (int i = 0; i < 8; i++)
queens[i] = -1; // -1 indicates that no queen is currently placed in the
// ith row
queens[0] = 0; // Initially, place a queen at (0, 0) in the 0th row // k - 1 indicates the number of queens placed so far
// We are looking for a position in the kth row to place a queen int k = 1;
while (k >= 0) {
// Find a position to place a queen in the kth row
int j = findPosition(k, queens);
if (j < 0) {
queens[k] = -1;
k--; // back track to the previous row
} else {
queens[k] = j;
if (k == 7) {
count++; // One more solution found
System.out.println("Solution " + count + ":");
printResult(queens);
} else {
k++;
}
}
}
System.out.println("How many solutions? " + count);
}
public static int findPosition(int k, int[] queens) {
int start = queens[k] == -1 ? 0 : queens[k] + 1;
for (int j = start; j < 8; j++) {
if (isValid(k, j, queens))
return j; // (k, j) is the place to put the queen now
}
return -1;
}
/
** Return true if you a queen can be placed at (k, j) */
public static boolean isValid(int k, int j, int queens[]) {
// See if (k, j) is a possible position
// Check jth column
for (int i = 0; i < k; i++)
if (queens[i] == j)
return false;
// Check major diagonal
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论