7.5
package wwr;
//7.5
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] number = new int[12];
boolean[] vistor = new boolean[12];
int num = 0;
System.out.print("Enter ten numbers: ");
for(int i = 0; i < 10; i++){
number[i] = input.nextInt();
vistor[i] = true;
for(int j = 0; j < i; j++)
if(number[i] == number[j]){
vistor[i] = false;
num ++;
break;
}
}
System.out.println("The number of distinct number is " + (10 - num));
System.out.print("The distinct numbers are: ");
for(int i = 0; i < 10; i++) {
if(vistor[i])
System.out.print(number[i] + " ");
}
}
}
/*
1 2 3 2 1 6 3 4 5 2
*/
7.17
package wwr;
//7.17
import java.util.Scanner;
class Student {
private String name;
private int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public void sort(Student[] stu, int count) {
Student tmp;
for(int i = 0; i < count; i++) {
for(int j = 0; j < count - i - 1; j++) {
if(stu[j].getScore() < stu[j + 1].getScore()) {
tmp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = tmp;
}
}
}
}
public void print(Student[] stu, int count) {
for(int i = 0; i < count; i++) {
System.out.println(stu[i].getName() + " " + stu[i].getScore());
}
}
}
public class Demo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of students: ");
int count = input.nextInt();
Student[] st = new Student[count + 3];
Student s1 = new Student();
int num = 0;
while(num < count) {
System.out.println("Enter student" + (num + 1)
+ "'s name and score: ");
Scanner scan = new Scanner(System.in);
String strLine = scan.nextLine();
String[] strLineArr = strLine.split(" ");
Student s2 = new Student();
s2.setName(strLineArr[0]);
s2.setScore(Integer.parseInt(strLineArr[1]));
st[num] = s2;
num++;
}
s1.sort(st, count);
System.out.println("The descending order is: ");
s1.print(st, count);
}
}
7.18
【注】冒泡排序
package wwr;
//7.18
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double tmp;
double[] num = new double[12];
System.out.println("Enter 10 floating point numbers: ");
for(int i = 0; i < 10; i++)
num[i] = input.nextDouble();
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 9 - i; j++) {
if(num[j] < num[j + 1]) {
tmp = num[j];
num[j] = num[j + 1];
num[j + 1] = tmp;
}
}
}
for(int i = 0; i < 10; i++)
System.out.print(num[i] + " ");
}
}
/*
1.2 3.4 5.6 7.8 9.0 1.9 2.8 3.7 4.6 5.5
*/
7.19
package wwr;
//7.19
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of the list: ");
int count = input.nextInt();
System.out.print("Enter the list: ");
int[] list = new int[count];
for(int i = 0; i < count; i++) {
list[i] = input.nextInt();
}
if(isSorted(list, count))
System.out.print("The list is already sorted");
else
System.out.print("The list is not sorted");
}
public static boolean isSorted(int[] list, int count) {
for(int i = 1; i < count; i++)
if(list[i] < list[i - 1])
return false;
return true;
}
}
/*
8
10 1 5 16 61 9 11 1
10
1 1 3 4 4 5 7 9 11 21
*/
7.22
package wwr;
//7.22 参考百度百科QAQ
public class Demo {
private int[] column; //同栏是否有皇后,1表示有
private int[] rup; //右上至左下是否有皇后
private int[] lup; //左上至右下是否有皇后
private int[] queen; //解答
private int num; //解答编号
public Demo() {
column = new int[8+1];
rup = new int[(2*8)+1];
lup = new int[(2*8)+1];
for (int i = 1; i <= 8; i++)
column[i] = 0;
for (int i = 1; i <= (2*8); i++)
rup[i] = lup[i] = 0; //初始定义全部无皇后
queen = new int[8+1];
}
public void backtrack(int i) {
if (i > 8) {
showAnswer();
} else {
for (int j = 1; j <= 8; j++) {
if ((column[j] == 0) && (rup[i+j] == 0) && (lup[i-j+8] == 0)) {
//若无皇后
queen[i] = j; //设定为占用
column[j] = rup[i+j] = lup[i-j+8] = 1;
backtrack(i+1); //循环调用
column[j] = rup[i+j] = lup[i-j+8] = 0;
}
}
}
}
protected void showAnswer() {
num++;
System.out.println("\n解答" + num);
for (int y = 1; y <= 8; y++) {
for (int x = 1; x <= 8; x++) {
if(queen[y]==x) {
System.out.print("|Q");
}
else {
System.out.print("| ");
}
}
System.out.println('|');
}
}
public static void main(String[] args) {
Demo queen = new Demo();
queen.backtrack(1);
}
}
7.23
package wwr;
//7.23
public class Demo {
public static void main(String[] args) {
int ans = 0;
int[] lock = new int[102];
for(int i = 1; i <= 100; i ++)
lock[i] = 1;
//初始状态是1,表示关着
for(int i = 1; i <= 100; i ++)
for(int j = i; j <= 100; j += i)
lock[j] = 1 - lock[j];
for(int i = 1; i <= 100; i ++)
if(lock[i] == 0)
System.out.println(i);
}
}
7.31
package wwr;
//7.31
import java.util.Arrays;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of lis1: ");
int n1 = input.nextInt();
System.out.print("Enter the number of lis2: ");
int n2 = input.nextInt();;
int[] list = new int[n1 + n2];
System.out.print("Enter list1: ");
for(int i = 0; i < n1; i ++)
list[i] = input.nextInt();
System.out.print("Enter list2: ");
for(int i = n1; i < n1 + n2; i ++)
list[i] = input.nextInt();
Arrays.sort(list, 0, n1 + n2);
System.out.print("The merged list is ");
for(int i = 0; i < n1 + n2; i ++)
System.out.print(list[i] + " ");
}
}
/*
5
4
1 5 16 61 111
2 4 5 6
*/
7.35
package wwr;
//7.35
import java.util.Scanner;
public class Demo {
public static boolean check(char[] guess, int length) {
for(int i = 0; i < length; i++)
if(guess[i] == '*')
return true;
return false;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean flag1 = true;
while(flag1) {
String[] wordset = {"program", "SpongeBobSquarePants"};
String word = wordset[(int)(Math.random() * wordset.length)];
int length = word.length(), count = 0;
char[] guess = new char[length];
for(int i = 0; i < length; i ++)
guess[i] = '*';
while(check(guess, length)) {
System.out.print("(Guess)Enter a letter in word ");
for(int j = 0; j < length; j ++)
System.out.print(guess[j]);
System.out.print(" > ");
char c1 = input.next().charAt(0);
boolean flag2 = false;
for(int j = 0; j < length; j++) {
if(c1 == word.charAt(j)) {
flag2 = true;
if(guess[j] == '*')
guess[j] = word.charAt(j);
else
System.out.println(" " + c1 + " is already in the word");
}
}
if(!flag2) {
System.out.println(" " + c1 + " is not in the word");
count ++;
}
}
System.out.println("The word is " + word + ". You missed " + count + " time");
System.out.print("Do you want to guess another word? Enter y or n> ");
char c2 = input.next().charAt(0);
if(c2 == 'y')
flag1 = true;
else if(c2 == 'n') {
flag1 = false;
System.out.print("Thanks for using this program!");
}
}
}
}