IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> 1002 A+B for Polynomials (25 point(s)) -> 正文阅读

[开发测试]1002 A+B for Polynomials (25 point(s))

1002 A+B for Polynomials (25 point(s))

This time, you are supposed to find A+B where A and B are two polynomials.

Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

每个输入文件包含一个测试用例。 每个案例占用2行,并且每行包含一个多项式的信息:

where K is the number of nonzero terms in the polynomial, N i N_iN

i(i=1,2,?,K) are the exponents and coefficients, respectively. It is given that …

Output Specification:
For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

Sample Input:
2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:
3 2 1.5 1 2.9 0 3.2

Java实现 代码如下:

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author Teletele
 * @desc pta1002题
 * @date 2021-07-07 22:04:46
 */
public class ABPolynomials {
    public static void main(String[] args) {
        //输入部分处理
        Scanner sc = new Scanner(System.in);
        int k1 = sc.nextInt();
        int [] p1_exp=new int[k1];
        double [] p1_coe=new double[k1];
        for(int i=0;i<k1;i++){
            p1_exp[i]=sc.nextInt();
            p1_coe[i]=sc.nextDouble();
        }
        int k2 = sc.nextInt();
        int[] p2_exp = new int[k2];
        double[] p2_coe = new double[k2];
        for (int i =0; i<k2;i++){
            p2_exp[i]=sc.nextInt();
            p2_coe[i]= sc.nextDouble();
        }
        sc.close();

        //多项式加法运算
        ArrayList<Integer> res_exp= new ArrayList<>();  //存储结果的指数信息
        ArrayList<Double>   res_coe = new ArrayList<>();  //存储结果的系数信息
        DecimalFormat changeFormat = new DecimalFormat("0.0");  //修改格式信息

        int i=k1-1,j=k2-1;  //定义两个指针分别指向指数最低的项
        while(i>=0&&j>=0){
            //三种情况该如何处理
            if(p1_exp[i]>p2_exp[j]){
                res_exp.add(p2_exp[j] );
                res_coe.add(p2_coe[j]);
                j--;
            }else  if(p1_exp[i]<p2_exp[j]){
                res_exp.add(p1_exp[i]);
                res_coe.add(p1_coe[i]);
                i--;
            }else{
                //指数相等时进行多项式计算
                if((p1_coe[i]+p2_coe[j])!=0){  //若不为0,则添加多项式
                    res_exp.add(p1_exp[i]);
                    res_coe.add(p1_coe[i]+p2_coe[j]);
                }
                i--;
                j--;
            }
        }
        //循环走完还有其他的项?
        while (i>=0){
            res_exp.add(p1_exp[i]);
            res_coe.add(p1_coe[i]);
            i--;
        }
        while (j>=0){
            res_exp.add(p2_exp[j] );
            res_coe.add(p2_coe[j]);
            j--;
        }

        //结果格式化输出,由于是逆序,这里逆序输出
        int k=res_coe.size();
        System.out.print(k);
        for (int l = k-1; l >=0 ; l--) {
            System.out.print(" "+res_exp.get(l));
            System.out.print(" "+changeFormat.format(res_coe.get(l)));
        }

    }

}

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-07-09 17:40:17  更:2021-07-09 17:40:43 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 22:57:33-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码