zoukankan      html  css  js  c++  java
  • 第5周编程:零基础学Java

    1、多项式加法(5分)

    题目内容:

    一个多项式可以表达为x的各次幂与系数乘积的和,比如:

    2x6+3x5+12x3+6x+20

    现在,你的程序要读入两个多项式,然后输出这两个多项式的和,也就是把对应的幂上的系数相加然后输出。

    程序要处理的幂最大为100。

    输入格式:

    总共要输入两个多项式,每个多项式的输入格式如下:

    每行输入两个数字,第一个表示幂次,第二个表示该幂次的系数,所有的系数都是整数。第一行一定是最高幂,最后一行一定是0次幂。

    注意第一行和最后一行之间不一定按照幂次降低顺序排列;如果某个幂次的系数为0,就不出现在输入数据中了;0次幂的系数为0时还是会出现在输入数据中。

    输出格式:

    从最高幂开始依次降到0幂,如:

    2x6+3x5+12x3-6x+20

    注意其中的x是小写字母x,而且所有的符号之间都没有空格,如果某个幂的系数为0则不需要有那项。

    输入样例:

    6 2

    5 3

    3 12

    1 6

    0 20

    6 2

    5 3

    2 12

    1 6

    0 20

    输出样例:

    4x6+6x5+12x3+12x2+12x+40
    
    时间限制:500ms内存限制:32000kb
    
    import java.util.Scanner;  
    public class Main {
     
    public static void main(String args[]) {
        int N = 101;
        int A[]=new int[N];
        Scanner In=new Scanner(System.in);
        
        for ( int i = 0; i < N; i++) {
            A[i] = 0;
        }
            
        int endtag = 0;
        int n, a;
        while ( endtag < 2 ) {
            n = In.nextInt();  
            a = In.nextInt(); 
            
            if ( n == 0 ) {
                endtag ++;
            }
            
            if ( A[n] != 0 ) {
                A[n] += a;
            } else {
                A[n] = a;
            }
        }
        int maxpower = 0;
        for ( int i = N-1; i > 0; i-- ) {
            if (A[i] != 0 ) {
                maxpower = i;
                break;
            }
        }
        if (maxpower == 0) {
            System.out.print(A[0]); 
        } else {
            for ( int i = maxpower; i > -1; i--) {
                if ( i > 1) {           
                    if ( Math.abs(A[i]) == 1 ) {
                         System.out.print("x");
                        System.out.print(i);
                    } else if ( Math.abs(A[i]) > 1 ) {
                        System.out.print(Math.abs(A[i]));
                         System.out.print("x");
                        System.out.print(i);
                    }
                } else if (i == 1 ) {
                    if ( Math.abs(A[i]) == 1 ) {
                        System.out.print("x");
                    } else if ( Math.abs(A[i]) > 1) {
                        System.out.print(Math.abs(A[i]));
                         System.out.print("x");
                    }
                } else {
                    if ( A[i] != 0 ) {
                        System.out.print(Math.abs(A[i]));
                    }
                }
                if ( i > 0){
                    if ( A[i-1] > 0 ) {
                        System.out.print("+");
                    } else if ( A[i-1] < 0 ) {
                        System.out.print("-");     
                    }
                }
            }
        }
    }
    }
    
    欢迎查阅
  • 相关阅读:
    HDU 5791 Two (DP)
    POJ 1088 滑雪 (DPor记忆化搜索)
    LightOJ 1011
    POJ 1787 Charlie's Change (多重背包 带结果组成)
    HDU 5550 Game Rooms (ccpc2015 K)(dp)
    HDU 5542 The Battle of Chibi (ccpc 南阳 C)(DP 树状数组 离散化)
    HDU 5543 Pick The Sticks (01背包)
    HDU 5546 Ancient Go (ccpc2015南阳G)
    NB-IoT的DRX、eDRX、PSM三个模式 (转载,描述的简单易懂)
    MQTT 嵌入式端通讯协议解析(转)
  • 原文地址:https://www.cnblogs.com/gh110/p/11827712.html
Copyright © 2011-2022 走看看