130道根本OJ编程题之: 78~88
@
目录
78: BC87 统计成绩
统计成绩_牛客题霸_牛客网
- import java.util.Scanner;
- import java.util.Arrays;
- // 注意类名必须为 Main, 不要有任何 package xxx 信息
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- // 注意 hasNext 和 hasNextLine 的区别
- while (in.hasNextInt()) { // 注意 while 处理多个 case
- int n = in.nextInt(); // 科目数
- float[] arr = new float[n];
- float sum = 0;
- for (int i = 0; i < arr.length; i++) {
- float temp = in.nextFloat();
- sum += temp;
- arr[i] = temp;
- }
- Arrays.sort(arr); // 排序,默认是升序
- System.out.printf("%.2f %.2f %.2f", arr[arr.length - 1], arr[0], sum / n / 1.0);
- }
- }
- }
复制代码 79: BC89 密码验证
密码验证_牛客题霸_牛客网
- import java.util.Scanner;
- // 注意类名必须为 Main, 不要有任何 package xxx 信息
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- // 注意 hasNext 和 hasNextLine 的区别
- while (in.hasNext()) { // 注意 while 处理多个 case
- String password = in.next(); //
- String password2 = in.next();
-
- if(password == "" || password2 == "") { // 判断是否存在为空值
- System.out.println("different");
- } else {
- if(password.equals(password2)) {
- System.out.println("same");
- } else {
- System.out.println("different");
- }
- }
- }
- }
- }
复制代码 80: BC90 矩阵计算
矩阵计算_牛客题霸_牛客网
- import java.util.Scanner;
- // 注意类名必须为 Main, 不要有任何 package xxx 信息
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- // 注意 hasNext 和 hasNextLine 的区别
- int sum = 0;
- while (in.hasNextInt()) { // 注意 while 处理多个 case
- int n = in.nextInt();
- int m = in.nextInt();
- // int arr[][] = new int[n][m];
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- // arr[i][j] =
- int temp = in.nextInt();
- if (temp > 0) {
- sum += temp;
- }
- }
- }
- }
- System.out.println(sum);
- }
- }
复制代码 81: BC92 逆序输出
逆序输出_牛客题霸_牛客网
- import java.util.Scanner;
- // 注意类名必须为 Main, 不要有任何 package xxx 信息
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int[] arr = new int[10];
- // 注意 hasNext 和 hasNextLine 的区别
- while (in.hasNextInt()) { // 注意 while 处理多个 case
- for(int i = 0; i < 10; i++) {
- arr[i] = in.nextInt();
- }
- for(int i = arr.length-1; i >= 0;i--) {
- System.out.printf("%d ",arr[i]);
- }
- }
- }
- }
复制代码 82: BC93 统计数据正负个数
统计数据正负个数_牛客题霸_牛客网
- import java.util.Scanner;
- // 注意类名必须为 Main, 不要有任何 package xxx 信息
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int positive = 0; // 正数
- int negative = 0; // 负数
- int temp = 0;
- // 注意 hasNext 和 hasNextLine 的区别
- while (in.hasNextInt()) { // 注意 while 处理多个 case
- for (int i = 0; i < 10; i++) {
- temp = in.nextInt();
- if (temp >= 0) {
- positive++;
- } else {
- negative++;
- }
- }
- }
- System.out.println("positive:" + positive);
- System.out.println("negative:" + negative);
- }
- }
复制代码 83: BC94 N个数之和
N个数之和_牛客题霸_牛客网
- import java.util.Scanner;
- // 注意类名必须为 Main, 不要有任何 package xxx 信息
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int sum = 0;
- // 注意 hasNext 和 hasNextLine 的区别
- while (in.hasNextInt()) { // 注意 while 处理多个 case
- int n = in.nextInt();
- for(int i = 0; i < n; i++) {
- int tmp = in.nextInt(); // 读取 5 个输入
- sum += tmp;
- }
- }
- System.out.println(sum);
- }
- }
复制代码 84: BC95 最高分与最低分之差
最高分与最低分之差_牛客题霸_牛客网
- import java.util.Scanner;
- import java.util.Arrays;
- // 注意类名必须为 Main, 不要有任何 package xxx 信息
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- int[] arr = new int[n];
- for (int i = 0; i < n; i++) {
- arr[i] = in.nextInt();
- }
- Arrays.sort(arr); // 默认进行升序排序
-
- System.out.println(arr[arr.length - 1] - arr[0]);
- }
- }
复制代码 85: BC96 有序序列判断
有序序列判断_牛客题霸_牛客网
- import java.util.Scanner;
- // 注意类名必须为 Main, 不要有任何 package xxx 信息
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- int[] arr = new int[n];
- int flag1 = 0;
- int flag2 = 0;
-
- for(int i = 0; i < n; i++) {
- int tmp = in.nextInt();
- arr[i] = tmp;
- if(i > 0 ) { // i > 0 防止越界
- // 升序判断
- if(arr[i-1] < arr[i]) {
- flag1 = 1;
- } else if(arr[i-1] > arr[i]) { // 升序判断
- flag2 = 1;
- }
- }
- }
- if(flag1 + flag2 > 1) { // 判断是否为有序,大于1说明序列中存在无序内容
- System.out.println("unsorted");
- } else {
- System.out.println("sorted");
- }
- }
- }
复制代码 86: BC98 序列中删除指定数字
序列中删除指定数字_牛客题霸_牛客网
- import java.util.Scanner;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.*;
- // 注意类名必须为 Main, 不要有任何 package xxx 信息
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- Integer n = in.nextInt();
- List list = new ArrayList(n);
- for (int i = 0; i < n; i++) {
- int tmp = in.nextInt();
- list.add(tmp);
- }
- Integer num = in.nextInt();
- Iterator<Integer> iterator = list.iterator();
- while (iterator.hasNext()) {
- if (iterator.next() == num) {
- iterator.remove();
- }
- }
- for(int i = 0; i < list.size();i++) {
- System.out.print(list.get(i)+" ");
- }
- }
- }
复制代码 87: BC99 序列中整数去重
序列中整数去重_牛客题霸_牛客网
- import java.util.Scanner;
- import java.util.Set;
- import java.util.LinkedHashSet;
- // 注意类名必须为 Main, 不要有任何 package xxx 信息
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- Set<Integer> set = new LinkedHashSet<>();
- for (int i = 0; i < n; i++) {
- int tmp = in.nextInt();
- set.add(tmp);
- }
- for(int num : set) {
- System.out.print(num+" ");
- }
- }
- }
复制代码 88: BC100 有序序列合并
有序序列合并_牛客题霸_牛客网
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.LinkedList;
- // 注意类名必须为 Main, 不要有任何 package xxx 信息
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- int m = in.nextInt();
- List listN = new ArrayList(n);
- List listM = new ArrayList(m);
- for (int i = 0; i < n; i++) {
- int tmp = in.nextInt();
- listN.add(tmp);
- }
- for (int i = 0; i < m; i++) {
- int tmp = in.nextInt();
- listM.add(tmp);
- }
- listN.addAll(listM);
- Object[] array = listN.toArray();
- Arrays.sort(array);
- for (Object num : array) {
- System.out.print(num + " ");
- }
- }
- }
复制代码 最后:
“在这个最后的篇章中,我要表达我对每一位读者的感激之情。你们的关注和回复是我创作的动力源泉,我从你们身上吸取了无尽的灵感与勇气。我会将你们的鼓励留在心底,继续在其他的范畴奋斗。感谢你们,我们总会在某个时刻再次相遇。”
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |