第六次作业题解
文章目录
今天的题解非常抱歉用的是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);
}
}