Exercise two

MyInteger类

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.Scanner;

class MyInteger{
int value;
MyInteger(int e){
this.value = e;
}
public int getter() {
return this.value;
}
public boolean isEven() {
return (this.value % 2 == 0);
}
public boolean isOdd() {
return (this.value % 2 == 1);
}
public boolean isPrime() {
for(int i=2; i*i <= this.value; i++)
{
if(this.value % i == 0)
return false;
}
return true;
}
public static boolean isEven(int e) {
return (e % 2 == 0);
}
public static boolean isOdd(int e) {
return (e % 2 == 1);
}
public static boolean isPrime(int e) {
for(int i=2; i*i <= e; i++)
{
if(e % i == 0)
return false;
}
return true;
}
public static boolean isEven(MyInteger e) {
return (e.value % 2 == 0);
}
public static boolean isOdd(MyInteger e) {
return (e.value % 2 == 1);
}
public static boolean isPrime(MyInteger e) {
for(int i=2; i*i <= e.value; i++)
{
if(e.value % i == 0)
return false;
}
return true;
}
public boolean equals(int e) {
return this.value == e;
}
public boolean equals(MyInteger e) {
return this.value == e.value;
}
}

public class Main{
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
int a = x.nextInt();
int b = x.nextInt();
int c = x.nextInt();
MyInteger mi = new MyInteger(a);
MyInteger ni = new MyInteger(b);
System.out.println(mi.isEven());
System.out.println(mi.isOdd());
System.out.println(mi.isPrime());
System.out.println(mi.isPrime(c));
System.out.println(mi.isPrime(ni));
System.out.println(mi.equals(c));
System.out.println(mi.equals(ni));
x.close();
}
}

Circle2D 类

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
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.Scanner;

class Circle2D{
double x;
double y;
double radius;

Circle2D(){
this.x = 0;
this.y = 0;
this.radius = 1;
}

Circle2D(double xx, double yy, double rr){
this.x = xx;
this.y = yy;
this.radius = rr;
}

public double getterx() {
return this.x;
}
public double gettery() {
return this.y;
}
public double getterradius() {
return this.radius;
}

public double getArea() {
return Math.PI * this.radius * this.radius;
}

public double getPerimeter() {
return Math.PI * this.radius * 2;
}

public boolean contains(double xx, double yy) {
double tmp = (xx - this.x)*(xx - this.x) + (yy - this.y)*(yy - this.y);
return Math.sqrt(tmp) < this.radius;
}

public boolean contains(Circle2D e) {
return Math.abs(this.radius-e.radius) > (Math.sqrt((e.x - this.x)*(e.x - this.x) + (e.y - this.y)*(e.y - this.y)));
}

public boolean overlaps(Circle2D e) {
if(this.contains(e)) return false;
if(e.contains(this)) return false;
return Math.sqrt((e.x - this.x)*(e.x - this.x) + (e.y - this.y)*(e.y - this.y)) < (Math.abs(this.radius) + Math.abs(e.radius));
}
}



public class Main1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Circle2D op1 = new Circle2D(input.nextDouble(),input.nextDouble(),input.nextDouble());
Circle2D op2 = new Circle2D(input.nextDouble(),input.nextDouble(),input.nextDouble());
double x = input.nextDouble();
double y = input.nextDouble();
System.out.println("The circle's area is "+op1.getArea());
System.out.println("The circle's perimeter is "+op1.getPerimeter());
System.out.println("The circle overlaps with the specified circle: "+op1.overlaps(op2));
System.out.println("The circle contains the specified point: "+op1.contains(x, y));
System.out.println("The circle contains the specified circle: "+op1.contains(op2));
input.close();
}
}

Author与BOok类

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

class Author{
private String name;
private String email;
private char gender;

public Author(String name, String email, char gender)
{
this.name = name;
this.email = email;
this.gender = gender;
}

public String getName() {
return this.name;
}

public String getEmail() {
return this.email;
}

public char getgen() {
return this.gender;
}

public String toString() {
String gg;
if(this.gender == 'm')
gg = "m";
else
gg = "f";
return "Author[name = " + this.name + ", email = " + this.email + ", gender = " + gg + "]";
}
}

class Book{
private String name;
private Author author;
private double price;
private int qty;

Book(String name, Author author, double price, int qty){
this.name = name;
this.author = author;
this.price = price;
this.qty = qty;
}

Book(String name, Author author, double price){
this.name = name;
this.author = author;
this.price = price;
this.qty = 0;
}

public String getName() {
return this.name;
}
public Author getAuthor() {
return this.author;
}
public double getPrice() {
return this.price;
}
public void setPrice(double e) {
this.price = e;
}
public int getQty() {
return this.qty;
}
public void setQty(int e)
{
this.qty = e;
}
public String toString() {
String au = this.author.toString();
return "Book[name = "+this.getName()+", "+au+", price = "+this.getPrice()+", qty = "+this.getQty() + "]";
}
}


public class Main2 {
public static void main(String[] args){
Author ahTeck = new Author("Tan Ah Teck", "ahteck@nowhere.com", 'm');
System.out.println(ahTeck); // Author's toString()

Book dummyBook = new Book("Java for dummy", ahTeck, 19.95, 99); // Test Book's Constructor
System.out.println(dummyBook); // Test Book's toString()

// Test Getters and Setters
dummyBook.setPrice(29.95);
dummyBook.setQty(28);
System.out.println("name is: " + dummyBook.getName());
System.out.println("price is: " + dummyBook.getPrice());
System.out.println("qty is: " + dummyBook.getQty());
System.out.println("Author is: " + dummyBook.getAuthor()); // Author's toString()
System.out.println("Author's name is: " + dummyBook.getAuthor().getName());
System.out.println("Author's email is: " + dummyBook.getAuthor().getEmail());

// Use an anonymous instance of Author to construct a Book instance
Book anotherBook = new Book("more Java", new Author("Paul Tan", "paul@somewhere.com", 'm'), 29.95);
System.out.println(anotherBook); // toString()
}
}

股票

三种:

  1. 只能买卖一次
  2. 不限买卖次数
  3. 只能买两次
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.util.Scanner;

class StockSeller{
int[] price;
StockSeller(int[] price){
this.price = price;
}
public int MaxProfit1() {
if(price.length <= 1) return 0;
if(price.length == 2) return price[1] > price[0] ? price[1] - price[0] : 0;
int ret = 0;
int min = this.price[0];
for(int i=1; i<price.length; i++)
{
if(price[i] > min)
{
int cur = price[i] - min;
ret = ret > cur ? ret : cur;
}
else if(price[i] < min)
min = price[i];
}
return ret;
}
public int MaxProfit2() {
// int[] dpbuy = new int[this.price.length];
// int[] dpsell = new int[this.price.length];
//
// dpbuy[0] = -price[0];
// dpsell[0] = 0;
// for(int i=1; i<this.price.length; i++)
// {
// dpbuy[i] = Math.max(dpbuy[i-1], dpsell[i-1]-price[i]);
// dpsell[i] = Math.max(dpsell[i-1], dpbuy[i-1] + price[i]);
// }
// return dpsell[price.length-1];
int sum = 0;
for(int i=1;i<price.length;i++){
if(price[i]-price[i-1] > 0)
sum+= price[i]-price[i-1];
}
return sum;
}

public int MaxProfit3() {
if(price.length <= 1) return 0;
if(price.length == 2) return price[1] > price[0] ? price[1] - price[0] : 0;
int[] l = new int[price.length];
int[] r = new int[price.length];

int ret = 0;
int cur = 0;
int min = price[0];
for(int i=0; i<price.length; i++)
{
if(price[i] >= min)
{
cur = price[i] - min;
ret = ret > cur ? ret : cur;
}
else if(price[i] < min)
min = price[i];
l[i] = ret;
}

ret = 0;
int max = price[price.length-1];
for(int i=price.length-1; i>=0; i--)
{
if(price[i] <= max)
{
cur = max - price[i];
ret = ret > cur ? ret : cur;
}
else if(price[i] > max)
max = price[i];
r[i] = ret;
}
max = 0;
for(int i=0; i<price.length-1; i++)
{
cur = l[i] + r[i+1];
if(cur > max) max = cur;
}
if(max < r[0])
max = r[0];
return max;
}
}

public class Main3 {
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
while(x.hasNext()){
int m=x.nextInt();
int[] price = new int[m];
for(int i=0;i<m;i++)
price[i]=x.nextInt();
StockSeller stock_seller = new StockSeller(price);
System.out.println(stock_seller.MaxProfit1());
System.out.println(stock_seller.MaxProfit2());
System.out.println(stock_seller.MaxProfit3());
}
x.close();
}
}
Donate? comment?