重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java两个参数相除代码 java两数相除取整数部分

谁知道用java怎么写出两个多项式相除的程序啊。。求解啊。。速度啊。。

网上有用单链表来实现多项式的加减乘,你可以参考一下,代码如下

为安达等地区用户提供了全套网页设计制作服务,及安达网站建设行业解决方案。主营业务为成都网站制作、做网站、外贸营销网站建设、安达网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

public class Polynomial {

private Monomial first; // 首项

//添加单项式

public void append(Monomial monomial) {

if (monomial == null) {

// do nothing

} else if (first == null) {

first = monomial;

} else {

Monomial current = first;

while (current != null) {

// Examda提示:如果指数相同,则相加

if (current.index == monomial.index) {

current.coefficient += monomial.coefficient;

break;

} else if (current.next == null) { // 否则直接扔到最后

current.next = monomial;

break;

}

current = current.next;

}

}

}

public void append(double c, int i) {

append(new Monomial(c, i));

}

public String toString() {

StringBuffer sb = new StringBuffer();

Monomial current = first;

while (current.next != null) {

sb

.append("(" + current.coefficient + "x^" + current.index

+ ") + ");

current = current.next;

}

sb.append("(" + current.coefficient + "x^" + current.index + ")");

return sb.toString();

}

// 两个多项式相加

public Polynomial add(Polynomial p2) {

Polynomial result = new Polynomial();

Monomial current = this.first;

while (current != null) {

result.append(current.coefficient, current.index); // Examda提示:注意这里

current = current.next;

}

current = p2.first;

while (current != null) {

result.append(current.coefficient, current.index);

current = current.next;

}

return result;

}

// 两个多项式相减 this- p2

public Polynomial substract(Polynomial p2) {

Polynomial result = new Polynomial();

Monomial current = this.first;

while (current != null) {

result.append(current.coefficient, current.index); // 注意这里

current = current.next;

}

current = p2.first;

while (current != null) {

result.append(-current.coefficient, current.index);

current = current.next;

}

return result;

}

/**

* this * p2

*

* @return

*/

public Polynomial multiply(Polynomial p2) {

Polynomial result = new Polynomial();

Monomial c1 = this.first;

Monomial c2 = p2.first;

while (c1 != null) {

while (c2 != null) {

result.append(c1.coefficient * c2.coefficient, c1.index

+ c2.index);

c2 = c2.next;

}

c1 = c1.next;

c2 = p2.first;

}

return result;

}

public Polynomial divide(Polynomial p2) {

// todo 实现相除

return null;

}

public static void main(String[] args) {

Polynomial p1 = new Polynomial();

p1.append(2.2, 1);

p1.append(3.3, 2);

p1.append(4.111, 7);

System.out.println("p1: " + p1);

Polynomial p2 = new Polynomial();

p2.append(2.232, 5);

p2.append(3.444, 6);

p2.append(5.777, 1);

System.out.println("p2: " + p2);

Polynomial result = p1.add(p2);

System.out.println("加: " + result);

result = p1.substract(p2);

System.out.println("减: " + result);

result = p1.multiply(p2);

System.out.println("乘: " + result);

}

}

/**

* 单项式

*/

class Monomial {

double coefficient; // 系数

int index; // 指数

Monomial next; // 后继结点

public Monomial() {

}

public Monomial(double c, int i) {

this.coefficient = c;

this.index = i;

}

}

关于java加减除的代码问题

import java.util.Scanner;

public class P {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

P prm=new P();

int a,b;

System.out.println("请输入两个整数(以空格分隔):");

a=sc.nextInt();

b=sc.nextInt();

System.out.println("两数相加后等于:"+prm.add(a,b));

System.out.println("两数相减后等于:"+prm.sub(a,b));

System.out.println("两数相除后等于:"+prm.divide(b,a));

sc.close();

}

int add(int a,int b) {

return a+b;

}

int sub(int a,int b) {

if(ab)

return a-b;

else

return b-a;

}

int divide(int a,int b) {

if(0==b)

System.out.println("错误");

if(0==a)

System.out.println("除数不能为0");

return b/a;

}

}

用java语言从键盘输入两个数,求这两个数相除的结果.处理所有可能的异常情况

import java.util.Scanner;

public class HelloJava {

public static void main(String[] args) {

int num1 = 0;

int num2 = 0;

Scanner scanner = new Scanner(System.in);

try {

System.out.println("请输入除数 按回车确认");

num1 = scanner.nextInt();

System.out.println("请输入被除数 按回车确认");

num2 = scanner.nextInt();

}catch (InputMismatchException e){

System.out.println("输入的类型不合法");

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

if(num2 == 0) {

System.out.println("num2 的值 不能是 0");

} else {

System.out.println(num1 + " / " + num2 + " = " + (num1 / num2));

}

}

}

怎么编写一个JAVA程序,从键盘输入任意两个整数进行相除操作并输出结果,使用2?

利用百Java应用程序编写要求输入两个整度数并计算两个数据之和具知体操作步骤道如下:内

import java.util.Scanner

public class Test {

public static void main(String[] args) {

System.out.print("请输容入第一个整数:")

Scanner s=new Scanner(System.in)

int a=s.nextInt()

System.out.print("请输入第二个整数:")

int b=s.nextInt()

System.out.println(a+b)

}

}


分享标题:java两个参数相除代码 java两数相除取整数部分
分享路径:http://cqcxhl.cn/article/dohddpi.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP