2021.05.31上机题
题目1:
源代码:
package Practice;
import java.util.Scanner;
public class Ch13Q17 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first complex number: ");
double a = input.nextDouble();
double b = input.nextDouble();
Complex c1 = new Complex(a, b);
System.out.print("Enter the second complex number: ");
double c = input.nextDouble();
double d = input.nextDouble();
Complex c2 = new Complex(c, d);
System.out.println("(" + c1 + ")" + " + " + "(" + c2 + ")" + " = " + c1.add(c2));
System.out.println("(" + c1 + ")" + " - " + "(" + c2 + ")" + " = " + c1.subtract(c2));
System.out.println("(" + c1 + ")" + " * " + "(" + c2 + ")" + " = " + c1.multiply(c2));
System.out.println("(" + c1 + ")" + " / " + "(" + c2 + ")" + " = " + c1.divide(c2));
System.out.println("|" + c1 + "| = " + c1.abs());
Complex c3 = (Complex)c1.clone();
// System.out.println(c1 == c3);
System.out.println(c3.getRealPart());
System.out.println(c3.getImaginaryPart());
// System.out.println(c1.toString());
}
}
class Complex implements Cloneable {
private double a = 0, b = 0;
public Complex() {
}
Complex(double a, double b) {
this.a = a;
this.b = b;
}
public Complex(double a) {
this.a = a;
}
public double getA() {
return a;
}
public double getB() {
return b;
}
public Complex add(Complex secondComplex) {
double newA = a + secondComplex.getA();
double newB = b + secondComplex.getB();
return new Complex(newA, newB);
}
public Complex subtract(Complex secondComplex) {
double newA = a - secondComplex.getA();
double newB = b - secondComplex.getB();
return new Complex(newA, newB);
}
public Complex multiply(Complex secondComplex) {
double newA = a * secondComplex.getA() - b * secondComplex.getB();
double newB = b * secondComplex.getA() + a * secondComplex.getB();
return new Complex(newA, newB);
}
public Complex divide(Complex secondComplex) {
double newA = (a * secondComplex.getA() + b * secondComplex.getB())
/ (Math.pow(secondComplex.getA(), 2.0) + Math.pow(secondComplex.getB(),
2.0));
double newB = (b * secondComplex.getA() - a * secondComplex.getB())
/ (Math.pow(secondComplex.getA(), 2.0) + Math.pow(secondComplex.getB(),
2.0));
return new Complex(newA, newB);
}
public double abs() {
return Math.sqrt(a * a + b * b);
}
@Override
public String toString() {
if (b != 0)
return a + " + " + b + "i";
return a + "";
}
public double getRealPart() {
return a;
}
public double getImaginaryPart() {
return b;
}
@Override
public Object clone() {
try {
return super.clone();
}
catch (CloneNotSupportedException ex) {
return null;
}
}
}
效果图:
题目2、3:
题目2代码:
package Practice;
import java.math.*;
public class Exercise13_15 {
public static void main(String[] args) {
Rational r1 = new Rational(new BigInteger("14"), new BigInteger("37"));
Rational r2 = new Rational(new BigInteger("23"), new BigInteger("67"));
System.out.println(r1 + " + " + r2 + " = " + r1.add(r2));
System.out.println(r1 + " - " + r2 + " = " + r1.subtract(r2));
System.out.println(r1 + " * " + r2 + " = " + r1.multiply(r2));
System.out.println(r1 + " / " + r2 + " = " + r1.divide(r2));
System.out.println(r2 + " is " + r2.doubleValue());
Rational rational = new Rational(new BigInteger("0"), new BigInteger("1"));
for (int i = 2; i <= 100; i++) {
rational = rational.add(new Rational(new BigInteger(""+(i-1)), new BigInteger(""+i))); // 计算1/2 + 2/3 + 3/4 + ... + 99/100的和
}
System.out.println("Final result: " + rational);
System.out.println("Double value: " + rational.doubleValue());
}
/*
14/37 + 23/67 = 1789/2479
14/37 - 23/67 = 87/2479
14/37 * 23/67 = 322/2479
14/37 / 23/67 = 938/851
23/67 is 0.34328358208955223
Final result: 264414864639329557497913717698145082779489/2788815009188499086581352357412492142272
Double value: 94.81262248236038
*/
static class Rational extends Number implements Comparable<Rational> {
// Data fields for numerator and denominator
private BigInteger numerator = BigInteger.ZERO;
private BigInteger denominator = BigInteger.ONE;
/** 默认属性 */
public Rational() {
this(BigInteger.ZERO, BigInteger.ONE);
}
/** 指定分子和分母 */
public Rational(BigInteger numerator, BigInteger denominator) {
BigInteger gcd = gcd(numerator, denominator);
if (denominator.compareTo(BigInteger.ZERO) < 0)
this.numerator = numerator.multiply(new BigInteger("-1")).divide(gcd);
else
this.numerator = numerator.divide(gcd);
this.denominator = denominator.abs().divide(gcd);
}
/** 最大公约数 */
private static BigInteger gcd(BigInteger n, BigInteger d) {
BigInteger n1 = n.abs();
BigInteger n2 = d.abs();
BigInteger gcd = BigInteger.ONE;
for (BigInteger k = BigInteger.ONE;
k.compareTo(n1) <= 0 && k.compareTo(n2) <= 0;
k = k.add(BigInteger.ONE)) {
if (n1.remainder(k).equals(BigInteger.ZERO) &&
n2.remainder(k).equals(BigInteger.ZERO))
gcd = k;
}
return gcd;
}
// private static BigInteger gcd(BigInteger n, BigInteger d) {
// return d ? gcd(d, n % d) : n;
// }
/** 返回分子 */
public BigInteger getNumerator() {
return numerator;
}
/** 返回分母 */
public BigInteger getDenominator() {
return denominator;
}
public Rational add(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator()).add(
denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational subtract(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator()).subtract(
denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational multiply(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational divide(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.numerator);
return new Rational(n, d);
}
@Override
public String toString() {
if (denominator.equals(BigInteger.ONE))
return numerator + "";
else
return numerator + "/" + denominator;
}
@Override
public boolean equals(Object parm1) {
if ((this.subtract((Rational)(parm1))).getNumerator().equals(BigInteger.ONE))
return true;
else
return false;
}
@Override
public int hashCode() {
return new Double(this.doubleValue()).hashCode();
}
@Override
public int intValue() {
return (int)doubleValue();
}
@Override
public float floatValue() {
return (float)doubleValue();
}
@Override
public double doubleValue() {
return numerator.doubleValue() / denominator.doubleValue();
}
@Override
public long longValue() {
return (long)doubleValue();
}
@Override
public int compareTo(Rational o) {
if ((this.subtract((Rational)o)).getNumerator().compareTo(BigInteger.ZERO) > 0)
return 1;
else if ((this.subtract((Rational)o)).getNumerator().compareTo(BigInteger.ZERO) < 0)
return -1;
else
return 0;
}
}
}
题目3代码:
package Practice;
import java.util.HashMap;
public class Exercise_03{
public static int flag = 0;
public static HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
public static int ans[] = new int[5];
public static int coll[] = new int[1000001];
public static int idx = 0;
public static void main(String args[]) {
for(int i = 1; i <= 1000000; i ++ ){
coll[i] = collatzArg(i,i);
}
for(int k = 0; k < 5; k ++ ){
int sum=0;
for(int i = 1; i <= 1000000; i ++ ){
if(sum < coll[i]){
sum=collatzArg(i, i);
idx = i;
ans[k] = idx;
}
}
coll[idx] = 0;
System.out.println(idx + ":");
collatzArg1(idx, idx);
}
}
// (n,n) d变化直到1
public static int collatzArg(double d, int n) {
flag++;
if (d > 1.0) {
if (d % 2 == 0) {
collatzArg(d / 2, n);
} else {
collatzArg(d * 3 + 1, n);
}
}else{
hashMap.put(n, flag);
flag=0;
}
return hashMap.get(n);
}
public static void collatzArg1(double d, int n) {
flag++;
System.out.print(d + " ");
if (d > 1.0) {
if (d % 2 == 0) {
collatzArg1(d / 2, n);
// System.out.print(d/2 + " ");
} else {
collatzArg1(d * 3 + 1, n);
// System.out.print(d * 3 + 1 + " ");
}
}else{
System.out.println("");
hashMap.put(n, flag);
flag=0;
}
}
}
题目3效果图: