第六次作业题解

第六次作业题解

文章目录

今天的题解非常抱歉用的是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);
    }
}
相关推荐
前行的小黑炭9 分钟前
Android 协程的使用:结合一个环境噪音检查功能的例子来玩玩
android·java·kotlin
李少兄43 分钟前
解决IntelliJ IDEA 提交代码时无复选框问题
java·ide·intellij-idea
cyforkk1 小时前
Spring Boot @RestController 注解详解
java·spring boot·后端
叫我阿柒啊2 小时前
从Java全栈到前端框架:一次真实面试的深度复盘
java·spring boot·typescript·vue·database·testing·microservices
点云SLAM2 小时前
C++ 常见面试题汇总
java·开发语言·c++·算法·面试·内存管理
sniper_fandc2 小时前
IDEA修改系统缓存路径,防止C盘爆满
java·ide·intellij-idea
aristo_boyunv2 小时前
拦截器和过滤器(理论+实操)
java·数据仓库·hadoop·servlet
半夏陌离2 小时前
SQL 入门指南:排序与分页查询(ORDER BY 多字段排序、LIMIT 分页实战)
java·前端·数据库
CUIYD_19892 小时前
Eclipse 常用搜索功能汇总
java·ide·eclipse
野犬寒鸦3 小时前
力扣hot100:相交链表与反转链表详细思路讲解(160,206)
java·数据结构·后端·算法·leetcode