【java】第7章 数组 测试题(作业部分) 超、凢脫俗 2022-11-17 13:48 190阅读 0赞 ### 文章目录 ### * * 7.5 * 7.17 * 7.18 * 7.19 * 7.22 * 7.23 * 7.31 * 7.35 ## 7.5 ## ![在这里插入图片描述][20210409174105821.png] 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 ## ![在这里插入图片描述][20210409175627622.png] 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 ## ![在这里插入图片描述][20210409182220898.png] 【注】冒泡排序 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 ## ![在这里插入图片描述][20210412103815807.png] 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 ## ![在这里插入图片描述][20210409182404832.png]![在这里插入图片描述][20210409182426609.png] 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 ## ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1ODc4Mjky_size_16_color_FFFFFF_t_70] 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 ## ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1ODc4Mjky_size_16_color_FFFFFF_t_70 1] 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 ## ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1ODc4Mjky_size_16_color_FFFFFF_t_70 2] 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!"); } } } } [20210409174105821.png]: /images/20221022/bbbee321de52421a90dbb95f10b94ab6.png [20210409175627622.png]: /images/20221022/7236c70daf37470ea6a0bdcb22e72c4d.png [20210409182220898.png]: /images/20221022/23491d7da2c54abfab22d86be75c45f4.png [20210412103815807.png]: /images/20221022/d1004d66d8fb4ccd879d2ac6ff33ca87.png [20210409182404832.png]: https://img-blog.csdnimg.cn/20210409182404832.png [20210409182426609.png]: /images/20221022/e4a81bece208434fbd7f2b2835675f82.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1ODc4Mjky_size_16_color_FFFFFF_t_70]: /images/20221022/7bd688e92a0d470d875b4e793c5635c2.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1ODc4Mjky_size_16_color_FFFFFF_t_70 1]: /images/20221022/b437b20b33b047198bb805cea71678af.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1ODc4Mjky_size_16_color_FFFFFF_t_70 2]: /images/20221022/003d7a1c003d477ca6c732ccb7552633.png
相关 【java】第7章 数组 测试题(作业部分) 文章目录 7.5 7.17 7.18 7.19 7.22 7.23 7.3 超、凢脫俗/ 2022年11月17日 13:48/ 0 赞/ 191 阅读
相关 【java】第6章 方法 测试题(作业部分) 文章目录 6.2 6.3 6.13 6.18 6.23 6.28 6.29 ╰+攻爆jí腚メ/ 2022年11月14日 13:29/ 0 赞/ 153 阅读
相关 【java】第4章 数学函数&字符串 测试题(作业部分) 文章目录 4.6 4.11 4.16 4.21 4.22 4.24 4.2 清疚/ 2022年11月10日 14:20/ 0 赞/ 166 阅读
相关 【java】第3章 选择 测试题(作业部分) 文章目录 3.15 3.22 3.27 3.28 3.29 3.15 ![在这里插入图片描述] 淩亂°似流年/ 2022年11月07日 14:56/ 0 赞/ 173 阅读
相关 【java】第1章 概述 测试题 文章目录 1.1 输出 1.2 for循环 1.3 1.4 变量转义字符混合输出 1.5 野性酷女/ 2022年11月07日 04:05/ 0 赞/ 254 阅读
相关 【java】第11章 继承和多态 测试题(作业部分) 文章目录 11.2 11.4 11.10 11.13 11.16 11.17 11. 旧城等待,/ 2022年10月21日 14:53/ 0 赞/ 179 阅读
相关 【java】第10章 面向对象思考 测试题(作业部分) 文章目录 10.3 10.5 10.10 10.13 10.18 10.19 爱被打了一巴掌/ 2022年10月21日 14:53/ 0 赞/ 178 阅读
相关 【java】第9章 对象和类 测试题(作业部分) 文章目录 9.1 9.4 9.5 9.9 9.12 9.13 9.1 ![在这里 落日映苍穹つ/ 2022年10月21日 14:53/ 0 赞/ 186 阅读
相关 第7章 数组实验 c程序实验报告 姓名:肖伟 实验地点:教学楼514教室 实验时间:4月30日 实验项目: 1、写一个函数,对用随机函数产生的10个整数按从小到大的 清疚/ 2021年11月26日 11:08/ 0 赞/ 456 阅读
还没有评论,来说两句吧...