一道pat 1010 一元多项式求导 有一个测试点答案错误但是想不出为什么

img


import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        String str="";
        while(scan.hasNext()){
            int c=scan.nextInt();
            int e=scan.nextInt();
            if(c==0&&e==0){
                System.out.print("0 0");
                break;
            }//零多项式直接打0 0 然后结束循环 因为后面不可能有东西了
            if(e!=1&&e!=0){
            str+=e*c+" "+(e-1)+" ";
            }
            if(e==1){
                str+=c+" "+0;//幂次等于1的时候无论后面常数项是不是0求导都是0,不用拼接
            }
        }
        System.out.print(str.trim());//防止没有一次项,去掉最后的空格
    }
}

img

第三个测试点过不去,想不出来为什么,看了一些其他的代码,感觉算法上差不多,具体哪里有问题?

ac代码
他要输出导数非零项的系数和指数


import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner scan=new Scanner(System.in);
        String str="";
        int f=0;
        while(scan.hasNext()){
            int c=scan.nextInt();
            int e=scan.nextInt();
            if(f==1){
                if(c==0||e==0){
                    
                }//零多项式直接打0 0 然后结束循环 因为后面不可能有东西了
                if(e!=1&&e!=0){
                    str+=e*c+" "+(e-1)+" ";
                }
                if(e==1){
                 str+=c+" "+0;//幂次等于1的时候无论后面常数项是不是0求导都是0,不用拼接
                }
            }
            if(f==0){
                f=1;
                if(c==0||e==0){
                    str+=0+" "+0;
                }
                else{
                    str+=e*c+" "+(e-1)+" ";
                }
            }
  
        }
        System.out.print(str.trim());//防止没有一次项,去掉最后的空格
    }
}

试一试你的
输入 3 4 6 1 0 0
输出 12 3 6 0

第11行改为以下代码看看?

str="0 0"