一,将一个正整数分解为其质因数的积。例如:输入90,输出90=2*3*3*5。
请不改动程序原有结构和代码,将程序补充完整,并得到正确运行结果。
输入:90
输出:90=2*3*3*5
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader=new Scanner(System.in);//定义读取数据对象reader
int n;
n=reader.nextInt(); //读取一个整数
Main pf=new Main();
pf.printPrimeFactor(n);
}
void printPrimeFactor(int n) { //求整数n的质因子,按按要求输出
System.out.print(n + "=");
int i = 2;
while (i <= n) {
if (n % i == 0) {
System.out.print(i);
n = n / i;
if (n != 1) {
System.out.print("*");
}
} else {
i++;
}
}
}
int isPrimeNumber(int n) {
if (n <= 1) {
return 0;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
}
二,实现分数的滅法运算、乘法运算、除法运算,并进行测试。输入要求:整数,
不考虑分母为0情况:输出要求:最简分数,分母不为1,分子不为0,分母不为0
输入:1412
输出:
3/4
-1/4
1/8
1/2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
int a,b,c,d;
a=reader.nextInt();b=reader.nextInt();
c=reader.nextInt();d=reader.nextInt();
Fraction f1=new Fraction(a,b);
Fraction f2=new Fraction(c,d);
Fraction f3=f1.add(f2);
f3.print();
Fraction f4=f1.sub(f2);
f4.print();
Fraction f5=f1.multi(f2);
f5.print();
Fraction f6=f1.divide(f2);
f6.print();
//此处补充程序
}
}class Fraction{
int numerator;
int denominator;
Fraction(){}
Fraction(int n,int d){
numerator=n;
denominator=d;
}
Fraction add(Fraction f2){
Fraction f3=new Fraction();
f3.numerator=numerator*f2.denominator+denominator*f2.numerator;
f3.denominator=denominator*f2.denominator;
return f3;
}
Fraction sub(Fraction f2){ //分数的减法运算
Fraction f4=new Fraction();
f4.numerator=numerator*f2.denominator-denominator*f2.numerator;
f4.denominator=denominator*f2.denominator;
return f4;
}
Fraction multi(Fraction f2){ //分数的乘法运算
Fraction f5=new Fraction();
f5.numerator=numerator*f2.numerator;
f5.denominator=denominator*f2.denominator;
return f5;
}
Fraction divide(Fraction f2){ //分数的除法运算
Fraction f6=new Fraction();
f6.numerator=numerator*f2.denominator;
f6.denominator=denominator*f2.numerator;
return f6;
}
void print(){
reduction();
//以下输出需要修改成符合题目要求的形式
if(numerator==0)System.out.println("0");
else if(denominator==1)System.out.println(numerator+"");
else
System.out.printf("%d/%d\n",numerator,denominator);
}
void reduction(){ //对分数进行约分,化成最简分数
//请补充完整。此处直接对numerator和denominator进行约分
//求分子分母的最大公约数,记为a
int a,x,y;
x=numerator;y=denominator;
if(x<0) x=-x;
if(y<0)
{
y=-y;denominator=-denominator;numerator=-numerator;
}
a=x%y;
while(a!=0)
{
x=y;y=a;a=x%y;
}
numerator=numerator/y;
denominator=denominator/y;
}
}
三,实现复数的加法、减法、乘法运算,并进行测试。
输入:
1 2
3 4
输出:
4+6i
-2-2i
-5+10i
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner reader = new Scanner(System.in);//定义读取数据对象reader
int r1, v1, r2, v2;
r1 = reader.nextInt(); //读取第一个复数的实部
v1 = reader.nextInt(); //读取第一个复数的虚部
r2 = reader.nextInt();
v2 = reader.nextInt();
Complex complex1 = new Complex(r1, v1);
Complex complex2 = new Complex(r2, v2);
complex1.add(complex2).print();
complex1.minus(complex2).print();
complex1.multiply(complex2).print();
}
}
class Complex {
int real;
int virtual;
Complex() {
}
Complex(int r, int v) {
real = r;
virtual = v;
}
Complex add(Complex f2) {
return new Complex(real + f2.real, virtual + f2.virtual);
}
Complex minus(Complex f2) {
return new Complex(real - f2.real, virtual - f2.virtual);
}
Complex multiply(Complex f2) {
return new Complex(real * f2.real - virtual * f2.virtual, virtual * f2.real + real * f2.virtual);
}
void print() {
//以下输出需要修改成符合题目要求的形式
if(real!=0) {
if(virtual >0) System.out.println(real + "+" + virtual + "i");
else if(virtual<0) System.out.println(real + "" + virtual + "i");
else System.out.println(real);
}
else if(real==0) {
if(virtual==0)System.out.println(0);
else System.out.println(virtual+"i");
}
}
}
四,参考上述两个程序,请你设计一个点类(Point),其属性包括横坐标和纵坐标,方法包括计算点所在象限getQuadrant、两点之间距离calDistance(小数部分四舍五入)。在PointTest类中对Point类的两个方法进行测试。
输入:
1 2
3 4
输出:1 1 3
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Point p1 = new Point(scanner.nextDouble(), scanner.nextDouble());
Point p2 = new Point(scanner.nextDouble(), scanner.nextDouble());
int quadrant1 = p1.getQuadrant();
int quadrant2 = p2.getQuadrant();
double distance = p1.calDistance(p2);
System.out.println(quadrant1 + " " + quadrant2 + " " + Math.round(distance));
}
}
class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public int getQuadrant() {
if (x > 0 && y > 0) {
return 1;
} else if (x < 0 && y > 0) {
return 2;
} else if (x < 0 && y < 0) {
return 3;
} else if (x > 0 && y < 0) {
return 4;
} else {
return 0;
}
}
public double calDistance(Point p) {
double dx = x - p.getX();
double dy = y - p.getY();
return Math.sqrt(dx * dx + dy * dy);
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}