10.3
package wwr;
//10.3 未完QAQ
import java.util.Scanner;
class MyInteger {
//0、一个名为value的int型数据域,存储这个对象表示的int值
private int value;
//1、一个为指定的int值创建MyInteger对象的构造方法
MyInteger(int Value) {
this.value = Value;
}
//2、一个返回int值的get方法
public int getValue() {
return value;
}
//3、如果值分别为偶数、奇数或素数,那么isEven()、is0dd()和isPrime()方法都会返回true
public boolean isEven() {
return isEven(value);
}
public boolean isOdd() {
return isOdd(value);
}
public boolean isPrime() {
return isPrime(value);
}
//4、如果指定值分别为偶数、奇数或素数,那么相应的静态方法isEven(int)、isOdd(int)和isPrime(int)会返回true
public static boolean isEven(int value) { //偶
if(value % 2 == 0)
return true;
else
return false;
}
public static boolean isOdd(int value) { //奇
if(value % 2 != 0)
return true;
else
return false;
}
public static boolean isPrime(int value) { //素
for(int i = 2; i < value - 1; i++)
if(value % i == 0)
return false;
return true;
}
//5、如果指定值分别为偶数、奇数或素数,那么相应的静态方法isEven(MyInteger)、is0dd(MyInteger)和isPrime(MyInteger)会返回true
public static boolean isEven(MyInteger m) { //偶
if(m.getValue() % 2 == 0)
return true;
else
return false;
}
public static boolean isOdd(MyInteger m) { //奇
if(m.getValue() % 2 != 0)
return true;
else
return false;
}
public static boolean isPrime(MyInteger m) { //素
for(int i = 2; i < m.getValue() - 1; i++)
if(m.getValue() % i == 0)
return false;
return true;
}
//6、如果该对象的值与指定的值相等,那么equals(int)和 equals(MyInteger)方法返回true
public boolean equals(int value) {
if(this.value == value)
return true;
else
return false;
}
public boolean equals(MyInteger m) {
if(this.value == m.getValue())
return true;
else
return false;
}
//7、静态方法parseInt(char[])将数字字符构成的数组转换为一个int值
public static int parseInt(char[] c) {
int tmp = 1, num = 0;
for(int i = c.length; i >= 0; i--) {
num += c[i] * tmp;
tmp *= 10;
}
return num;
}
//8、静态方法parseInt(String)将一个字符串转换为一个int值
public static int parseInt(String s) {
int tmp = 1, num = 0;
for(int i = s.length(); i >= 0; i--) {
num += s.charAt(i) * tmp;
tmp *= 10;
}
return num;
}
}
public class Demo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter n1: ");
int n1 = input.nextInt();
//1、一个为指定的int值创建MyInteger对象的构造方法
MyInteger N1 = new MyInteger(n1);
//2、一个返回int值的get方法
System.out.println("n1 is " + N1.getValue());
//3、如果值分别为偶数、奇数或素数,那么isEven()、is0dd()和isPrime()方法都会返回true
System.out.println("Is n1 an even number?" + '\t' + N1.isEven());
System.out.println("Is n1 an odd number?" + '\t' + N1.isOdd());
System.out.println("Is n1 a prime?" + '\t' + N1.isPrime());
//4、如果指定值分别为偶数、奇数或素数,那么相应的静态方法isEven(int)、isOdd(int)和isPrime(int)会返回true
System.out.println("Is n1 an even number?" + '\t' + MyInteger.isEven(n1));
System.out.println("Is n1 an odd number?" + '\t' + MyInteger.isOdd(n1));
System.out.println("Is n1 a prime?" + '\t' + MyInteger.isPrime(n1));
//5、如果指定值分别为偶数、奇数或素数,那么相应的静态方法isEven(MyInteger)、is0dd(MyInteger)和isPrime(MyInteger)会返回true
System.out.println("Is n1 an even number?" + '\t' + MyInteger.isEven(N1));
System.out.println("Is n1 an odd number?" + '\t' + MyInteger.isOdd(N1));
System.out.println("Is n1 a prime?" + '\t' + MyInteger.isPrime(N1));
System.out.print("Enter n2: ");
int n2 = input.nextInt();
MyInteger N2 = new MyInteger(n2);
//6、如果该对象的值与指定的值相等,那么equals(int)和 equals(MyInteger)方法返回true
System.out.println("Is n1 equal to n2?" + '\t' + N1.equals(n2));
System.out.println("Is n1 equal to n2?" + '\t' + N1.equals(N2));
}
}
10.5
package wwr;
//10.5
import java.util.Scanner;
import java.util.Arrays;
class StackOfIntegers {
private int integer;
public StackOfIntegers(int integer) {
this.integer = integer;
}
private static int k = 0;
private static int l = 0;
public int[] GetPrime() {
int[] getPrime = new int[this.integer];
boolean isPrime = true;
for (int i = 2; i < this.integer; i++) {
for (int h = 2; h < i; h++) {
if (i % h == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
getPrime[k] = i;
k++;
} else isPrime = true;
}
return getPrime;
}
public int[] Prime() {
int[] get = GetPrime();
int[] prime1 = new int[k];
boolean judge = false;
while (this.integer != 0) {
for (int a = k - 1; a >= 0; a--) {
if ((int) (this.integer / get[a]) == (double) this.integer / get[a]) {
prime1[l] = get[a];
this.integer /= get[a];
l++;
}
if (integer == get[a] || integer == 1) {
prime1[l] = get[a];
judge = true;
break;
}
}
if (judge)
break;
}
int[] prime = new int[l + 1];
for (int i = 0; i < l + 1; i++){
prime[i] = prime1[i];
}
return prime;
}
}
public class Demo {
public static void main(String[] args) {
System.out.print("Please input a integer: ");
Scanner input = new Scanner(System.in);
int integer=input.nextInt();
StackOfIntegers stackOfIntegers = new StackOfIntegers(integer);
int[] a=stackOfIntegers.Prime();
System.out.print("All the prime factors of the "+ integer + " are :");
System.out.print(Arrays.toString(a));
input.close();
}
}
10.10
package wwr;
//10.10 参考https://www.nowcoder.com/questionTerminal/8a35140dd85a4cfeb954964d5ea98437
import javax.management.RuntimeErrorException;
class Queue{
private int[] element;
public static final int capacity = 8;
private int size = 0;
public Queue() {
element = new int[capacity];
}
public void enqueue(int v) {
if(size >= element.length) {
int[] temp = new int[element.length * 2];
System.arraycopy(element, 0, temp, 0, element.length);
element = temp;
}
element[size++] = v;
}
public int dequeue() {
if (empty()) {
throw new RuntimeException("error");
}
else {
int temp = element[0];
for (int i = 0; i < element.length - 1; i++) {
element[i] = element[i + 1];
}
size--;
return temp;
}
}
public boolean empty() {
return size == 0;
}
public int getSize() {
return size;
}
}
public class Demo {
public static void main(String[] args) {
Queue queue = new Queue();
for (int i = 1; i <= 20; i++) {
queue.enqueue(i);
}
System.out.println("队列实际长度:" + queue.getSize());
for (int i = 0; i < 20; i++) {
System.out.println("元素 "+ queue.dequeue() + "出队列");
}
}
}
10.13
package wwr;
//10.13 参考https://blog.youkuaiyun.com/IFHHH/article/details/105581205
import java.util.*;
class MyRectangle2D {
public MyRectangle2D() {
this.x = 0;
this.y = 0;
this.height = 1;
this.width = 1;
}
private double x;
private double y;
private double width;
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
private double height;
public MyRectangle2D(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public double getArea() {
return height * width;
}
public double getPerimeter() {
return (height + width) * 2;
}
public boolean contains(double x, double y) {
//长方形判断:点到中心点距离x < width / 2 并且 y < height / 2
return Math.abs(x - this.x) < width / 2 && Math.abs(y - this.y) < height / 2;
}
public boolean contains(MyRectangle2D r) {
//包含判断:俩中心点距离小于宽距离和高距离;
return Math.abs(this.x - r.getX()) <= Math.abs((this.width - r.width) / 2)
&& Math.abs(this.y - r.y ) <= Math.abs((this.height - r.height) / 2);
}
public boolean overLaps(MyRectangle2D r) {
//重叠判断:点距离大于宽之差,高之差,小于宽高之和
boolean overlaps1 = Math.abs(this.x - r.getX()) > Math.abs((this.width - r.width) / 2)
&& Math.abs(this.y - r.y )> Math.abs((this.height - r.height) / 2);
boolean overlaps2 = Math.abs(this.x - r.getX()) < Math.abs((this.width + r.width) / 2)
&& Math.abs(this.y - r.y )< Math.abs((this.height + r.height) / 2);
return overlaps1 && overlaps2;
}
}
public class Demo {
public static void main(String[] args) {
MyRectangle2D r1 = new MyRectangle2D(2, 2, 5.5, 4.9);
System.out.println("The r1's area is " + r1.getArea());
System.out.println("The r1's perimeter is " + r1.getPerimeter());
System.out.println("The point contain is " + r1.contains(3, 3));
System.out.println("The new myRectangle contain is " + r1.contains(new MyRectangle2D(4, 5, 10.5, 3.2)));
System.out.println("The new myRectangle overLaps is " + r1.overLaps(new MyRectangle2D(3, 5, 2.3, 5.4)));
}
}
10.18
package wwr;
//10.18 参考https://blog.youkuaiyun.com/weixin_34600909/article/details/114863569
import java.math.BigInteger;
public class Demo {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
BigInteger bigNum = new BigInteger(Long.MAX_VALUE + "");
bigNum = bigNum.add(BigInteger.ONE);
int count = 1;
while (count <= 5) {
if (isPrime(bigNum)) {
System.out.println(bigNum);
count++;
}
bigNum = bigNum.add(BigInteger.ONE);
}
System.out.println(bigNum.toString());
// long endTime = System.currentTimeMillis();
// System.out.println("Time spent is " + (endTime - startTime) + " milliseconds");
}
public static boolean isPrime(BigInteger num) {
return true;
}
}
10.19
package wwr;
//10.19 参考https://blog.youkuaiyun.com/lyf3010572059/article/details/89440158?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-3.vipsorttest&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-3.vipsorttest
import java.math.BigInteger;
public class Demo {
public static void Check(BigInteger result, int i) {
if(result.isProbablePrime(1))
System.out.println(i + "\t" + result);
}
public static void main(String[] args) {
BigInteger num1 = BigInteger.ONE;
System.out.println("p\t2^p-1");
System.out.println("");
for(int i = 0; i <= 100;i++) {
num1 = num1.multiply(new BigInteger("2"));
BigInteger result = num1.subtract(new BigInteger("1"));
Check(result, i);
}
}
}
10.25
package wwr;
//10.25 参考http://cache.baiducontent.com/c?m=CFVc7eamI2vqo9axvdxAsvYP5lalUczMQyUHu7f2REIhE28hhgkjoxBE-3l-UDVnbHXIkp4S4W3bag9N3AUJdEcL2m7G26drodbKBSZs0ao72eZz7iqYVPhufgWEn9-omLVXdiOb5iIHlt_HciAkX7HVyx736w1TH5lGKVN3KLC_SqYlJdW81wSTX9ME2UAjZ6fD3u_B8El6-H8RMAY3dpy0WyY5bnZRwnqUfsqn31Vk7YavEPZh8l57DxbRO5UlL3XmmvPf33hnaTexxFfml4cBa-XFItAKT4SINLmrYPnRBerdvRL00YyTmhUSVbC3iBJlPh0ushUpyiz14sZgP-7ZNvFjMwzV342hIly-zNu&p=8c74d63785cc43b508e294754e4e&newp=93769a47c88108f108e294755f5d92695d0fc20e3addd501298ffe0cc4241a1a1a3aecbf2c251b07d0c3766c00a54c56ebf436703d0034f1f689df08d2ecce7e31ca6e6a&s=9517fd0bf8faa655&user=baidu&fm=sc&query=string%C0%E0%D6%D0%B5%C4split%B7%BD%B7%A8%BB%E1%B7%B5%BB%D8%D2%BB%B8%F6%D7%D6%B7%FB%B4%AE%CA%FD%D7%E9%2C%B8%C3%CA%FD%D7%E9%CA%C7%D3%C9&qid=d847a699001948b5&p1=3
import java.util.Scanner;
import java.util.ArrayList;
import java.util.regex.*;
public class Demo {
public static String[] split(String s, String regex) {
ArrayList<String> matchList = new ArrayList<String>();
Pattern pat = Pattern.compile(regex);
Matcher mat = pat.matcher(s);
int index = 0;
while(mat.find()) {
String match = s.subSequence(index, mat.start()).toString();
matchList.add(match);
String match2 = s.subSequence(mat.start(),mat.end()).toString();
matchList.add(match2);
index = mat.end();
}
String match = s.subSequence(index,s.length()).toString();
matchList.add(match);
int resultSize = matchList.size();
while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
resultSize--;
String[] result = new String[resultSize];
return matchList.subList(0, resultSize).toArray(result);
}
public static void main(String[] args) {
String[] ans=split("ab#123#453","#");
System.out.println("ab#123#453");
for(int i=0;i<ans.length;i++) {
System.out.println(ans[i]);
}
String[] ans2=split("a?b?gf#e","[?#]");
System.out.println("a?b?gf#e");
for(int i=0;i<ans2.length;i++) {
System.out.println(ans2[i]);
}
}
}