第六次作业题解

第六次作业题解

文章目录

今天的题解非常抱歉用的是java,(因为我不怎么用c++,上次用还是在上次了),可能以后也还是用java写题解吧,这次的作业挺简单的,你们也可以了解一下java的语法。

第一题

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '1') {
                count++;
            }
        }
        if (count > s.length() / 2) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

第二题

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        String[] arr = str.split(",");//根据','分割字符串
        int count = 0,c2=0;
        double sum = 0;
        for (int i = 0; i < arr.length; i++) {
            int num = Integer.parseInt(arr[i]);
            if (num > 0) {
                count++;
            }else if(num<0)c2++;
            sum += num;
        }
        System.out.println(count + "," + c2);

        System.out.printf("%.2f",sum / arr.length);
    }
}

第三题

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int v = in.nextInt();
        int N = in.nextInt();
        String result = convertToBaseN(v, N);
        System.out.println(result);
    }

    public static String convertToBaseN(int v, int N) {
        StringBuilder sb = new StringBuilder();
        while (v > 0) {
            int remainder = v % N;
            sb.insert(0, remainder);
            v /= N;
        }
        return sb.toString();
    }
}

第四题

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m = in.nextInt();
        int n = in.nextInt();
        int[][] arr = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                arr[i][j] = in.nextInt();
            }
        }
        int[] sum = new int[n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (arr[i][j] % 2 != 0) {
                    sum[j] += arr[i][j];
                }
            }
        }
        for (int i = 0; i < n; i++) {
            System.out.print(sum[i] + " ");
        }
    }
}

第五题

java 复制代码
import java.util.Scanner;

public class Main {
    public static boolean isNarcissisticNumber(int n) {
        int digit1 = n / 100; // 百位数字
        int digit2 = (n / 10) % 10; // 十位数字
        int digit3 = n % 10; // 个位数字
        int sum = digit1 * digit1 * digit1 + digit2 * digit2 * digit2 + digit3 * digit3 * digit3;
        return (sum == n);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        if (n < 100 || n > 999) {
            System.out.println("请输入一个三位数");
        } else {
            if (isNarcissisticNumber(n)) {
                System.out.println(n + "是水仙花数");
            } else {
                System.out.println(n + "不是水仙花数");
            }
        }
    }
}

第六题

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        int lastDigitA = a % 10;
        int lastDigitB = b % 10;
        int product = lastDigitA * lastDigitB;
        System.out.println(product);
    }
}
相关推荐
SamDeepThinking9 小时前
高并发场景下,CompletableFuture与ForkJoinPool该如何取舍?
java·后端·面试
张不才12 小时前
CPU 100% 了怎么办?Java 性能排障的标准化操作
java·后端
shepherd11113 小时前
吞吐量提升 10 倍:高并发大批量数据处理任务的架构演进与性能调优
java·后端·架构
plainGeekDev16 小时前
单例模式 → object 声明
android·java·kotlin
用户2986985301417 小时前
Java 实现 Word 文档文本与图片提取的方法
java·后端
SimonKing18 小时前
铁子,IntelliJ IDEA 2026.1.3来了,升不升?
java·后端·程序员
咖啡八杯1 天前
GoF设计模式——策略模式
java·后端·spring·设计模式
用户128526116022 天前
我把祖传Java项目重构后,接口响应从3s砍到了200ms,只改了这几行代码
java
Linsk2 天前
组件 = 模板 + 业务逻辑
java·前端·vue.js