7-1 多数组排序 (20 分)
3个整数数组进行整体排序,根据输入的三个数组的元素,输出排序后的结果(从大到小)
第1个数组的各个元素
第2个数组的长度
第2个数组的各个元素
第3个数组的长度
第3个数组的各个元素
3 79 80 61 3 88 66 77 2 23 90
讯享网
讯享网90 88 80 79 77 66 61 23
Accepted Code
import java.lang.*; import java.sql.Array; import java.util.*; public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); Integer[] arrays = new Integer[10000]; for (int i = 0; i < arrays.length; i++) {
arrays[i] = 0; } int num = 0; for(int i = 0; i < 3; ++i) {
int n = scan.nextInt(); for (int j = 0; j < n; ++j) {
arrays[num++] = scan.nextInt(); } } Comparator cmp = new MyComparator(); Arrays.sort(arrays, cmp); for (int i = 0; i < num; ++i) {
System.out.print(arrays[i]); if (i != num - 1) System.out.print(" "); } scan.close(); } } //不是extends Comparator class MyComparator implements Comparator<Integer>{
@Override public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1); } }
7-2 两队PK (20 分)
A、B两队进行比赛,每队各有多名队员及其分数(分数不重复),使用A、B队所有队员得分的TOP 3来判断两队输赢,在TOP 3中拥有更多人数的队获胜。写程序实现该过程。
A队每人得分
B队人数
B队每人得分
赢的队
输入样例:
讯享网5 22 33 44 55 11 4 12 32 42 52
输出样例:
55 52 44 A
Accepted Code
讯享网import java.lang.*; import java.util.*; public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); int num = 0; int aNum = scan.nextInt(); Integer[] arraysA = new Integer[aNum]; for(int i = 0; i < aNum; ++i) {
arraysA[i] = scan.nextInt(); } int bNum = scan.nextInt(); Integer[] arraysB = new Integer[bNum]; for(int i = 0; i < bNum; ++i) {
arraysB[i] = scan.nextInt(); } Comparator<Integer> cmp = new MyComparator(); Arrays.sort(arraysA, cmp); Arrays.sort(arraysB, cmp); Integer[] res = new Integer[3]; int aCmp = 0, bCmp = 0; for (int i = 0; i < 3; i++) {
if (arraysA[aCmp] > arraysB[bCmp]) {
res[i] = arraysA[aCmp++]; } else if (arraysA[aCmp] < arraysB[bCmp]) {
res[i] = arraysB[bCmp++]; } else {
i++; } } for (int i = 0; i < res.length; ++i) {
System.out.print(res[i]); if (i != res.length - 1) System.out.print(" "); } System.out.println(); if (aCmp > bCmp) {
System.out.print("A"); } else {
System.out.print("B"); } scan.close(); } } //不是extends Comparator class MyComparator implements Comparator<Integer>{
@Override public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1); } }
7-3 公司季度销售额以及年销售额统计 (20 分)
年底了,某公司要做销售统计,请输入各月份的销售额(单位:万元),请分别统计每季度的以及全年度的公司销售总额。 要求:
1.用一个四行3列数组记录销售额
2.在Main类中写一个方法统计并输出季度以及全年度的销售总额,并输出。方法声明定义为:
public static void showTotal(int [][]sale)
3.在main方法中顶一个二维数组并通过键盘输入各月份的销售总额,然后调用showTotal方法,输出统计结果。
22 33 44 33 44 66 77 45 64 43 34 23 45
讯享网1季度的销售额为:99 2季度的销售额为:143 3季度的销售额为:186 4季度的销售额为:100 全年销售额为:528
Accepted Code
import java.lang.*; import java.util.*; public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); int [][]sale = new int[4][3]; for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 3; j++) {
sale[i][j] = scan.nextInt(); } } showTotal(sale); scan.close(); } public static void showTotal(int [][]sale) {
int totalYear = 0; for (int i = 0; i < 4; ++i) {
int tmpTotal = 0; for (int j = 0; j < 3; j++) {
tmpTotal += sale[i][j]; } totalYear += tmpTotal; System.out.println(i + 1 + "季度的销售额为:" + tmpTotal); } System.out.println("全年销售额为:" + totalYear); } }
7-4 数组元素的删除 (20 分)
完成数组元素的移动功能:假设数组有n个元素,输入一个数x,把数组的第x个位置的元素删除了,后面的元素依次前进一个位置。 重复若干次这样的删除,得到最后的结果。
输入样例:
讯享网10 1 2 3 4 5 6 7 8 9 10 4 3 2 4 6
输出样例:
1 4 5 7 8 10
Accepted Code
讯享网import java.lang.*; import java.util.*; public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int[] arr = new int[n]; int length = arr.length; for (int i = 0; i < arr.length; ++i) {
arr[i] = scan.nextInt(); } int deleteNum = scan.nextInt(); for (int i = 0; i < deleteNum; ++i) {
delete(arr, scan.nextInt() - 1, length--); } for (int i = 0 ; i < length; i++) {
System.out.print(arr[i]); if (i != length - 1) System.out.print(" "); } scan.close(); } public static void delete(int[] arr, int add, int length) {
for (int i = add; i < length - 1; i++) {
arr[i] = arr[i + 1]; } } }
7-5 369寝室 (20 分)
369寝室是比较特殊的寝室,因为别的寝室都住了四个人,而369寝室只有三个人。也因为这个原因,寝室里的三位同学感情特别好。但是,毕业在即,三位小伙伴马上要分别。为了在未来的某个日子可以见面,三位小伙伴有了一个约定,假设在未来的某一年,三位小伙伴的年龄的末尾正好出现3、6、9三个数,那么他们会再次相聚。
现在问题来了,假设今年三位小伙伴的年龄分别是x,y,z,那么,他们三人最早几年后可以相聚呢?
输入样例:
25 22 28
输出样例:
讯享网1
Accepted Code
import java.lang.*; import java.util.*; public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in); int body1 = scan.nextInt(); int body2 = scan.nextInt(); int body3 = scan.nextInt(); if (!judge(body1, body2, body3)) {
System.out.println("so sad!"); } scan.close(); } public static boolean judge(int body1, int body2, int body3) {
for (int i = 1; i < 101; i++) {
body1++; body2++; body3++; if (body1 % 10 == 3) {
if (body2 % 10 == 6) {
if (body3 % 10 == 9) {
System.out.println(i); return true; } } else if (body2 % 10 == 9) {
if (body3 % 10 == 6) {
System.out.println(i); return true; } } } else if (body1 % 10 == 6) {
if (body2 % 10 == 3) {
if (body3 % 10 == 9) {
System.out.println(i); return true; } } else if (body2 % 10 == 9) {
if (body3 % 10 == 3) {
System.out.println(i); return true; } } } else if (body1 % 10 == 9) {
if (body2 % 10 == 3) {
if (body3 % 10 == 6) {
System.out.println(i); return true; } } else if (body2 % 10 == 6) {
if (body3 % 10 == 3) {
System.out.println(i); return true; } } } } return false; } }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/85401.html