for(int i = 0; i list[i] = random.nextInt(100);
}
for (Integer integer : list) {
System.out.println(integer);
}
}
}
3.谁知道用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; } } 。
4.用java语言编写一个可以实现任意两个数相加的小程序public Test{
public static void main(String[] args){
System.out.println(Test.add(11,3));//测试整数相加
System.out.println(Test.add(1.7,3.5));//测试小数相加
}
public static int add(int a, int b){//整数相加
return a+b;
}
public static float add(float a, float b){//小数相加
return a+b;
}
}
5.java程序:要求两个数相除,除数不能为负数和0定义异常类,当除数import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Division {
double Division() throws NumberFormatException, IOException, ZeroException {
double dividend = 1, divisor = 1;
System.out.println("请输入被除数:");
- 船长评语怎么写
- 怀孕两个月见红 怀孕两个月见红了怎么办呢
- 腾飞字草书怎么写
- 两访两创怎么写
- 怎么写两个s
- 油票报销怎么写理由
- 一叶障目,不见泰山;两耳塞豆,不闻雷霆--出自哪里 不见泰山两豆塞耳上一句
- 代码词间距怎么写
- 一个月两次月经 一个月两次月经第二次血很黑
- 两个人开公司怎么写合同