不会飞的章鱼

熟能生巧,勤能补拙;念念不忘,必有回响。

LintCode Java教程 练习题答案

Java 基础语法:语法、变量与运算

2160 · 打印 “Hello Java”

1
2
3
4
5
6
7
8
9
public class Solution {
/* Your first java code
* print Hello Java to console
*/
public static void main(String[] args) {
// write your code here
System.out.print("Hello Java");
}
}

2157 · 打印 Welcome to LintCode!

1
2
3
4
5
6
public class Solution {
public static void main(String[] args) {
// write your code here
System.out.print("Welcome to LintCode!");
}
}

2289 · 字符的ASCII码(Java 版)

1
2
3
4
5
6
7
8
9
10
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// write your code here
Scanner scan = new Scanner(System.in);
char c= scan.next().charAt(0);
System.out.println(c+0);
}
}

2814.ASCII码对应的字符(Java 版)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.*;

public class Main {
public static void main(String[] args){
// write your code here
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
if(num<128 && num >=0){
System.out.println(num>0?(char)num:"error");
}else{
System.out.println("error");
}

sc.close();

}
}

2759 · Integer 的取值范围

1
2
3
4
5
6
7
public class Solution {

public boolean equivalence(Integer a, Integer b) {
// write your code here
return a.equals(b);
}
}

2611 · 判断两个数组是否相等

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.*;

public class Solution {
public boolean equalityOfArrays(int[] arr1, int arr2[]) {
// -- write your code here --
Arrays.sort(arr1);
Arrays.sort(arr2);
if(Arrays.equals(arr1,arr2))
return true;
else
return false;
}
}

2166 · 简单的加减乘除运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Solution {
public int addition(int a, int b) {
// write your code here
return a + b;
}

public int subtraction(int a, int b) {
// write your code here
return a - b;
}

public int multiplication(int a, int b) {
// write your code here
return a * b;
}

public float division(int a, int b) {
// write your code here
return (float)a / b;
}
}

2826 · 数字 2 的朋友

1
2
3
4
5
6
public class Solution {
public static boolean isFriend(int n) {
// write your code here
return Integer.bitCount(n) == 2;
}
}

2820 · 小数字的和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;

public class Main {
public static void main(String[] args){
// write your code here
Scanner scan=new Scanner(System.in);
int len=scan.nextInt();
int[] s=new int[len];
for(int i=0; i<len; i++){
s[i]=scan.nextInt();
}
//求最大
int max=Arrays.stream(s).max().orElse(0);
//过滤并求和
int total=Arrays.stream(s).filter(f -> f!=max).sum();
System.out.println(total);
}
}

2823 · 这是什么三角形?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.*;

public class Main {
public static void main(String[] args){
// write your code here
Scanner cin = new Scanner(System.in);

int a = cin.nextInt();
int b = cin.nextInt();
int c = 180 - a -b;

if (a > 0 && b > 0 && c > 0) {
if (a + b == 90) {
System.out.println("直角三角形");
}
else if (a + b < 90) { //c > 90
System.out.println("钝角三角形");
}
else {
System.out.println("锐角三角形");
}
} else {
System.out.println("无法组成三角形");
}
}
}

2515 · 唯一存在的数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.*;

public class Solution {
/**
* @param nums: Represents the incoming number
* @return: A int value representing whether the
* return is a palindrome or not
*/
public int uniqueNumber(int nums[]) {
// write your code here
int ans = 0;
for(int i = 0; i < nums.length; i++){
ans ^= nums[i];
}
return ans;
}
}

Java 控制语句:分支与循环

2411 · 打印每月含有的天数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.Scanner;


class Main {
public static void main(String[] args) {
// write your code here
// read data from console
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
int month = sc.nextInt();

// output the answer to the console according to the
// requirements of the question
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31 days");

break;

case 4:
case 6:
case 9:
case 11:
System.out.println("30 days");

break;

case 2:

if ((((year % 4) == 0) && ((year % 100) != 0)) ||
((year % 400) == 0)) {
System.out.println("29 days");
} else {
System.out.println("28 days");
}

break;
}
}
}

2323 · 命中率评级 (Java 版)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// write your code here
// read data from console
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
// output the answer to the console according to the
// requirements of the question
switch(n/10){
case 10:
System.out.println("A");
break;
case 9:
System.out.println("A");
break;
case 8:
System.out.println("B");
break;
case 7:
System.out.println("C");
break;
case 6:
System.out.println("D");
break;
default:
System.out.println("E");
}
}
}

2290 · 求最大公约数(Java 版)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class Main {
public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
// write your code here
// please print the greatest common divisor of a and b
int res = 1;
int minval = Math.min(a,b);
int maxVal = Math.max(a,b);

if(maxVal % minval == 0)
{
System.out.println(minval);
return;
}
while (a % 2 == 0 && b % 2 == 0)
{
res *= 2;
a /= 2;
b /= 2;
}
while (a % 3 == 0 && b % 3== 0)
{
res *= 3;
a /= 3;
b /= 3;
}
while (a%5 ==0 && b%5==0)
{
res *= 5;
a /= 5;
b /= 5;
}
System.out.println(res);
}
}

2813 · 求 1 — 100 的偶数和

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Main {
public static void main(String[] args) {
// write your code here
int res = 0;
int i = 0;
while(i <= 100)
{
res += i;
i += 2;
}
System.out.println("Even sum of 1 - 100: "+res);
}
}

2259 · 求和(Java 版)

1
2
3
4
5
6
7
8
9
10
11
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
// read data from console
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// output the answer to the console according to the requirements of the question
System.out.println((1+n)*n/2);
}
}

2822 · 截取下标为5的字符

1
2
3
4
5
6
7
8
9
10
11
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// write your code here
Scanner sc=new Scanner(System.in);//创建Scanner对象
String s=sc.next(); //创建输入一个字符串
char[] ch=s.toCharArray(); //将字符串转化为char型数组
System.out.print(ch[5]); //输出char型数组下标为5的数据
}
}

2825 · 判断字符串是否相等

1
2
3
4
5
6
7
8
9
10
11
12
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// write your code here
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String str2 = sc.nextLine();

System.out.println(str1.equals(str2));
}
}

2336 · 字符串中的字母大小写转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Solution {
/**
* @param str: Indicates the string passed in
* @return: means return the case-converted string
*/
public String alphabetConversion(String str) {
// write your code here
String s = "";
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c>='A'&&c<='Z') {
c += 32;
s += c;
}else if(c>='a'&&c<='z'){
c -= 32;
s += c;
}else if(c>='0'&&c<='9'){
s += c;
}
}
return s;
}
}

2408 · 字符串字符排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Arrays;
public class Solution {
/**
* @param str: Arbitrary non-empty string
* @return String in alphabetical ascending order
*/
public String handle(String str) {
// write your code here
char[] chars = str.toCharArray();
Arrays.sort(chars);
// 将数组转化成字符串
return String.valueOf(chars);
}
}

2828 · 拼接字符串并改为大写

1
2
3
4
5
6
7
8
9
10
public class Main {
public static void main(String[] args) {
String str1 = "I love ";
String str2 = "China";
// write your code here
str1 = str1.concat(str2);
str1 = str1.toUpperCase();
System.out.println(str1);
}
}

2329 · 求直角坐标系内两点间距离(Java 版)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// write your code here
// read data from console
Scanner sc = new Scanner(System.in);
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
// output the answer to the console according to the
// requirements of the question
double distance = Math.sqrt(((x1 - x2) * (x1 - x2)) +
((y1 - y2) * (y1 - y2)));
System.out.printf("%.2f\n", distance);
}
}

2283 · 圆型周长(Java 版)

1
2
3
4
5
6
7
8
public class Main {
public static void main(String[] args) {
int r = Integer.parseInt(args[0]);
// write your code here
// please print the perimeter of the circle
System.out.printf("%.2f",2*Math.PI*r);
}
}

2289 · 字符的ASCII码(Java 版)

1
2
3
4
5
6
7
8
9
10
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// write your code here
Scanner scan = new Scanner(System.in);
char c= scan.next().charAt(0);
System.out.println(c+0);
}
}

2837 · 复制指定元素

1
2
3
4
5
6
7
8
9
10
11
12
public class Main {
public static void main(String[] args) {
int[] arr={1,2,3,4,5,6,7,8,9};
// write your code here
int [] newArr = new int[5];

for(int i=0;i<5;i++){
newArr[i] = arr[i];
System.out.print(newArr[i]+" ");
}
}
}

2838 · 随机生成数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Random;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// write your code here
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Random r = new Random(n);
for(int i = 0;i<50;i++){
System.out.println(r.nextInt(n)+1);
}
}
}

2675 · 日期格式转换

1
2
3
4
5
6
7
8
9
10
11
12
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Solution {

public String dateConversion(String str) throws ParseException {
// -- write your code here --
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(str);
return new SimpleDateFormat("yyyy年M月d日").format(date);
}
}

2829 · 简单的手机号码验证

1
2
3
4
5
6
7
8
9
import java.util.regex.*;
public class Solution {
public static boolean isMatch(String s) {
// write your code here
Pattern p = Pattern.compile("^1[3-9]{1}[0-9]{9}+$");
Matcher m = p.matcher(s);
return m.matches();
}
}

2832 · 简单校验一下邮箱格式

1
2
3
4
5
6
7
8
9
10
import java.util.regex.*;
public class Solution {
public static boolean isMatch(String s) {
// write your code here
String regex = "^[^0-9]\\w*([.+-]\\w+)*@[a-z0-9]+(.com)+(\\.[\\.a-zA-Z0-9]+)*";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
return matcher.matches();
}
}

2840 · 简单的字符串匹配替换

1
2
3
4
5
6
7
import java.util.regex.*;
public class Solution {
public static String replaceString (String str, String replacement) {
// write your code here
return str.replaceAll("[\\]/\\|~!@#$%\\^&\\*\\(\\);:_\\+\\-\\[\\]]+", replacement);
}
}

Java 面向对象入门

2848 · 完善合适的类

1
2
3
4
5
6
7
8
9
10
class Solution{
//write your code here
protected int age;
protected String name;

protected void show() {
String str = "Name: " + this.name + ", age: " + String.valueOf(this.age);
System.out.println(str);
}
}

2851 · 动物园的新朋友

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Animal {
private String name,type;
private int age;
public void setAnimalMessage(String name, String type, int age) {
this.name = name;
this.type = type;
this.age = age;
}

public void printAnimalMessage() {
System.out.println(name+" is a "+type+" and is "+age+" years old this year.");
}
}

2852 · 补充 Student 类的属性和方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Student {
// write your code here
public static String SEX_FEMALE = "female";
public static String SEX_MALE = "male";
private String sex;
public String name;
public int age;
public void setSex(String sex){
this.sex = sex;
}
public String toString(){
return "name = "+name+", age = "+age+", sex = "+sex;

}
}

2853 · 普通单例模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class SingleClass {
public String toString() {
return "This is a Single Instance Class.";
}

// write your code here
//创建 SingleClass 的一个对象
private static SingleClass instance = new SingleClass();

//让构造函数为 private,这样该类就不会被实例化
private SingleClass(){}

//获取唯一可用的对象
public static SingleClass getInstance(){
return instance;
}
}

2842 · 无参构造函数的使用(一)

1
2
3
4
5
6
7
8
9
10
11
12
public class Student {
public String name;
public int age;
public double height;

// write your code here
public Student(){
this.name = "Tom";
this.age = 20;
this.height = 180.0;
}
}

2843 · 无参构造函数的使用(二)

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Student {
String name;

public Student(String name) {
this.name = name;
}
public String toString() {
return "name = " + name;
}
// write your code here
public Student() {
}
}

2850 · 有参构造函数的使用(一)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Student {
String name;
int age;
public String toString() {
return "name = " + name + ", age = " + age;
}
// write your code here
public Student(String name, int age){
this.name=name;
this.age=age;
}
public Student(String name){
this.name=name;

}
public Student( int age){
this.age=age;

}

public Student(){

}
}

2212 · 编写对象属性的赋值方法和打印方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public class Employee {
String name;
String occupation;
String telephone;
int age;
int salary;
// write your code here
public Employee() {

}

public Employee(String name,
String occupation,
String telephone,
int age,
int salary) {
this.name = name;
this.occupation = occupation;
this.telephone = telephone;
this.age = age;
this.salary = salary;
}

public void setName(String name) {
this.name = name;
}

public void setOccupation(String occupation) {
this.occupation = occupation;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}

public void setAge(int age) {
this.age = age;
}

public void setSalary(int salary) {
this.salary = salary;
}

@Override
public String toString() {
return "Name: "+ name + System.lineSeparator() +
"Age: "+ age + System.lineSeparator() +
"Telephone: " + telephone + System.lineSeparator() +
"Occupation: " + occupation + System.lineSeparator() +
"Salary: " + salary;
}

public void printInfo() {
System.out.println(toString());

}
}

2614 · 求长方形的周长和面积

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Rectangle {
// Write your code here
private int length;
private int width;
public Rectangle(int length,int width){
this.length=length;
this.width=width;
}

public void show(){
float zhouchang=(length+width)*2;
float mianji=length*width;
String s="周长为: "+zhouchang+",面积为: "+mianji;
System.out.print(s);
}
}

2244 · 打印工作内容

1

# Java 面向对象进阶

------ 本文结束------
如果本篇文章对你有帮助,可以给作者加个鸡腿~(*^__^*),感谢鼓励与支持!