栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

01 牛刀小试【PAT B1010】一元多项式求导

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

01 牛刀小试【PAT B1010】一元多项式求导

1 题目

2 代码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class B1010 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        sc.close();

        str = str.trim();
        String[] s = str.split(" ");
        List coe = new ArrayList<>();
        List exp = new ArrayList<>();

        int x,y;
        for (int i = 0; i < s.length; i+=2) {
            x = Integer.parseInt(s[i + 1]);
            if(x > 0){
                y = Integer.parseInt(s[i]);
                y *=x;
                x--;
                coe.add(y);
                exp.add(x);
            }
        }

        if(coe.isEmpty()){  // 零多项式
            coe.add(0);
            exp.add(0);
        }

        for (int i = 0; i < coe.size(); i++) {
            if (i == coe.size() - 1){
                System.out.print(coe.get(i) + " ");
                System.out.print(exp.get(i));
            }else {
                System.out.print(coe.get(i) + " ");
                System.out.print(exp.get(i) + " ");
            }
        }
    }
}

3 要点

(1)String.trim()函数:去除字符串前后的空格。
(2)String.split()函数:按照限定的规则将字符串划分为数组,方法之一是使用正则表达式。(正则表达式的使用可以参照https://blog.csdn.net/weixin_33672109/article/details/91598356)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/736520.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号