m0_63956557 2022-03-29 10:20 采纳率: 50%
浏览 70
已结题

链表实现两个整数的乘法算法(Java)

无从下手,三位数和三位数相乘是一个规律,三位数和两位数相乘就变了

  • 写回答

2条回答 默认 最新

  • CSDN专家-sinJack 2022-03-29 10:30
    关注
    import java.util.Scanner;
    public class Dolynomial {
        private Node first;
        private Node last;
        private int N;
    
        private class Node{
            int coef;
            int expo;
            Node next;
        }
    
        public boolean isEmpty() {
            return N == 0;
        }
    
        public int size() {
            return N;
        }
    
        public void enqueue(int coef, int expo) {
            Node oldlast = last;
            last = new Node();
            last.coef = coef;
            last.expo = expo;
            last.next = null;
            if(N == 0) {
                first = last;
            } else
                oldlast.next = last;
            N++;
        }
    
        //读入一个多项式
        public static Dolynomial read() {
            Dolynomial temp = new Dolynomial();
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入整数:");
            int coef = sc.nextInt();
            temp.enqueue(coef, 1);
            return temp;
        }
    
        public Dolynomial multi(Dolynomial D1, Dolynomial D2) {
            Dolynomial temp = new Dolynomial();
            Node node1 = D1.first;
            Node node2 = D2.first;
    
            int n1 = D1.size();
            int n2 = D2.size();
    
            while(n2-- > 0) {
                temp.enqueue(node1.coef * node2.coef, node1.expo + node2.expo);
                node2 = node2.next;
            }
    
            while(--n1 > 0) {
                Node node3 = temp.first;
                node1 = node1.next;
                node2 = D2.first;
                n2 = D2.size();
    
                while(n2 > 0) {
                    //System.out.println("这个也一样哦!");
                    int c = node1.coef * node2.coef;
                    int e = node1.expo + node2.expo;
                    while(node3.next != null && node3.next.expo > e)
                        node3 = node3.next;
                    if(node3.next != null && node3.next.expo == e) {
                        //指数相等,直接相加
                        node3.next.coef += c;
                        if(node3.next.coef == 0) {
                            node3 = node3.next.next;
                        }
                    } else {
                        //插入节点
                        Node p = new Node();
                        p.coef = c;
                        p.expo = e;
                        p.next = node3.next;
                        node3.next = p;
                        temp.N++;
                    }
    
                    n2--;
                    node2 = node2.next;
                }
            }
            return temp;
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            Dolynomial d1, d2, result1, result2;
            d1 = read();
            d2 = read();
            result2 = d1.multi(d1, d2);
            Node temp2 = result2.first;
            for (; temp2 != null; temp2 = temp2.next) {
                System.out.println("结果:"+temp2.coef);
            }
        }
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月7日
  • 已采纳回答 3月30日
  • 创建了问题 3月29日

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图