SCAU学习笔记 - 面向对象程序设计课后习题
复活啦!这学期给先给各位带来Java的题解 后续的Bash可能也会有 这篇自己也是边学边敲的 算是共勉啦!
01-基础语法编程的知识点
摄氏温度值度转换成华氏温度值
java.util.Scanner是用来读入数据的,默认读入的是字符串
println输出的时候会自带一个回车,想输出两位小数的话就必须要转成字符串,占位规则和C语言没什么区别
变量的定义和计算赋值啥的都和C语言没啥区别 就不多说了
import java.util.*;
public class one_one {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double C = in.nextDouble();
double F = C * 9 / 5 + 32;
System.out.println(String.format("%.2f", F));
}
}
圆柱体的体积
和上一题没有太大区别
import java.util.*;
import java.lang.*;
public class one_two {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double r = in.nextDouble();
double h= in.nextDouble();
double V = Math.PI * r * r * h;
System.out.println(String.format("%.2f", V));
}
}
正整数的各位数字之和
nextLine可以直接读取一整行的内容作为字符串存进去
字符串的遍历,包括for循环的语法和取字符串长度都和C++没有差别
import java.util.*;
import java.lang.*;
public class one_three {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
int res=0;
for(int i=0;i<s.length();i++){
res+=s.charAt(i)-'0';
}
System.out.println(res);
}
}
三角形的面积
import java.util.*;
import java.lang.*;
public class one_four {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double x1=in.nextDouble();
double y1=in.nextDouble();
double x2=in.nextDouble();
double y2=in.nextDouble();
double x3=in.nextDouble();
double y3=in.nextDouble();
double s1=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
double s2=Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
double s3=Math.sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
double s=(s1+s2+s3)/2;
double area=Math.sqrt(s*(s-s1)*(s-s2)*(s-s3));
System.out.println(String.format("area=%.2f", area));
}
}
计算每个月第一天是星期几
开始写一些复杂的逻辑,我们先预处理每个月的天分,把闰年这样一个变量解决了,然后每个月累加天数计算即可
import java.util.*;
import java.lang.*;
public class One_five {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int year = in.nextInt();
int first = in.nextInt();
int[] days = {31,28,31,30,31,30,31,31,30,31,30,31};
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days[1] = 29;
}
int[] ans = new int[12];
ans[0] = first;
for (int i = 1; i < 12; i++) {
ans[i] = (ans[i-1] + days[i-1]) % 7;
}
for (int i = 0; i < 12; i++) {
System.out.print(ans[i]);
if (i != 11) {
System.out.print(" ");
}
}
}
}
正整数10进制形式转换16进制形式
直接调函数,常见的进制转换一般都有内置函数的
import java.util.*;
import java.lang.*;
public class One_six {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n < 0) {
System.out.println("不正确的输入");
return;
}
System.out.println(Integer.toHexString(n).toUpperCase());
}
}
计算自然常数 e 的值
这题要注意一下浮点数和整数的计算规则啥的,如果学过C语言了那就都不成问题吧
import java.util.*;
import java.lang.*;
public class ont_seven {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n < 0) {
System.out.println("不正确的输入");
return;
}
double e = 1;
double temp = 1;
for(int i = 1; i <= n; i++) {
temp *= i;
e += 1.0 / temp;
}
System.out.println(String.format("%.8f", e));
}
}
判断回文整数
正常判断回文串即可,注意有负数的话要直接输出错误,所以要取字符串的第一个字符,和C语言有些不同,需要调一个函数
import java.util.*;
import java.lang.*;
public class One_eight {
public static void main(String[] args) {
String input = System.console().readLine();
if(input.charAt(0) == '-') {
System.out.println("不正确的输入");
return;
}
int len = input.length();
boolean flag = true;
for (int i = 0; i < len / 2; i++) {
if (input.charAt(i) != input.charAt(len - i - 1)) {
flag = false;
break;
}
}
if (flag) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
储物柜问题
注意数组开桶的语法
import java.util.Scanner;
public class One_nine {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int m = in.nextInt();
boolean[] lockers = new boolean[m];
for (int i = 0; i < m; i++) {
lockers[i] = false;
}
for (int i = 1; i <= m; i++) {
for (int j = i - 1; j < m; j += i) {
lockers[j] = !lockers[j];
}
}
for (int i = 0; i < m; i++) {
if (lockers[i]) {
System.out.print(i + 1 + " ");
}
}
}
}
考试成绩分析
注意一下长度可变的数组是怎么开的
import java.util.*;
import java.lang.*;
public class One_ten {
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++) {
int m = in.nextInt();
arr[i] = new int[m];
for (int j = 0; j < m; j++) {
arr[i][j] = in.nextInt();
}
}
for (int i = 0; i < n; i++) {
int sum = 0;
int max = arr[i][0];
int min = arr[i][0];
for (int j = 0; j < arr[i].length; j++) {
sum += arr[i][j];
if (arr[i][j] > max) {
max = arr[i][j];
}
if (arr[i][j] < min) {
min = arr[i][j];
}
}
System.out.println(String.format("%.2f %.2f %.2f", (double) sum / arr[i].length, (double) max, (double) min));
}
}
}