• MOOC——多项式加法(5分)好难!


    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

    有问题

    这是我自己写的,也不知道哪里错了,怎么改都不对。

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner in = new Scanner(System.in);
    5. int[] mi = new int[101];
    6. int x, y, cnt = 0;
    7. while (true) {
    8. x = in.nextInt();
    9. y = in.nextInt();
    10. //一直不知道该怎么结束这个输入循环 后来才看到有人写到:读到两次指数为0时,跳出循环
    11. mi[x] = mi[x] + y;
    12. if (x == 0) cnt++;
    13. if (cnt == 2) break;
    14. }
    15. for (int i = 100; i > 0; i--) {
    16. if (mi[i] > 0) {
    17. if (i == 1)
    18. System.out.print(mi[i] + "x+");
    19. else
    20. System.out.print(mi[i] + "x" + i + "+");
    21. }
    22. }
    23. System.out.print(mi[0]);
    24. }
    25. }

     看到网上的开始按照人家的思路写,依旧错误!!!

    不会是输出的错误吧!!!emmmmm仔细研究之后还真是输出的错误。

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner in = new Scanner(System.in);
    5. int[] mi = new int[101];
    6. int x, y;
    7. int flag = 0;
    8. while (flag < 2) {
    9. x = in.nextInt();
    10. y = in.nextInt();
    11. if (x == 0) flag++;
    12. mi[x] = mi[x] + y;
    13. }
    14. for (int i = mi.length-1; i > 0; i--) {
    15. if (mi[i] > 0) {
    16. if (i == 1)
    17. System.out.print(mi[i] + "x+");
    18. else
    19. System.out.print(mi[i] + "x" + i + "+");
    20. }
    21. }
    22. System.out.print(mi[0]);
    23. }
    24. }

    正确答案

    1.若是只有常数项,,,

    别人写的,先凑活看看,我不会。

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. // TODO Auto-generated method stub
    5. Scanner in = new Scanner(System.in);
    6. int a, b;
    7. final int N = 101;
    8. int[] arr = new int[N];
    9. int flag = 0;
    10. for (int i = 0; i < arr.length; i++)
    11. arr[i] = 0;
    12. while (flag < 2) {
    13. a = in.nextInt();
    14. b = in.nextInt();
    15. if (a == 0)
    16. flag++;
    17. if(arr[a] != 0)
    18. arr[a] += b;
    19. else
    20. arr[a] = b;
    21. }
    22. int begin = 0;
    23. for(int i = N-1; i>=0;i--)
    24. {
    25. if(arr[i] != 0)
    26. {
    27. begin = i;
    28. break;
    29. }
    30. }
    31. if(begin == 0)
    32. {
    33. System.out.print(arr[0]);
    34. }
    35. else {
    36. for(int i = begin; i > -1; i--) {
    37. if(arr[i] == 0)
    38. continue;
    39. if(i == 0)
    40. {
    41. if(arr[0] > -1)
    42. {
    43. System.out.print("+"+arr[0]);
    44. }
    45. else
    46. {
    47. System.out.print(arr[0]);
    48. }
    49. }
    50. else if(i == begin)
    51. {
    52. if(arr[i] == 1)
    53. {
    54. System.out.print("x"+i);
    55. }
    56. else if(arr[i] == -1)
    57. {
    58. System.out.print("-x"+i);
    59. }
    60. else
    61. {
    62. System.out.print(arr[i]+"x"+i);
    63. }
    64. }
    65. else if(i == 1)
    66. {
    67. if(arr[i] == 1)
    68. {
    69. System.out.print("+x");
    70. }
    71. else if(arr[i] == -1)
    72. {
    73. System.out.print("-x");
    74. }
    75. else if(arr[i] > 1)
    76. {
    77. System.out.print("+"+arr[i]+"x");
    78. }
    79. else
    80. {
    81. System.out.print(arr[i]+"x");
    82. }
    83. }
    84. else if(i > 1)
    85. {
    86. if(arr[i] == 1)
    87. {
    88. System.out.print("+x"+i);
    89. }
    90. else if(arr[i] == -1)
    91. {
    92. System.out.print("-x"+i);
    93. }
    94. else if(arr[i] > 0)
    95. {
    96. System.out.print("+" + arr[i]+"x"+i);
    97. }
    98. else {
    99. System.out.print(arr[i] + "x" + i);
    100. }
    101. }
    102. }
    103. }
    104. }
    105. }

  • 相关阅读:
    Linux安装PHP(最新版)
    日期格式化
    SpringCloud微服务-SpringAMQP(RabbitMQ)
    定义函数(简单介绍)-def
    Druid LogFilter输出可执行的SQL
    4_Git
    小白也可以开发闲鱼自动化发布工具!!!
    Mybatis-动态 SQL详解
    JS判断类型(typeof+constructor+toString)
    GJB 5000B二级-II实施基础
  • 原文地址:https://blog.csdn.net/qq_45745322/article/details/128146365