[关闭]
@laofang 2016-06-19T02:17:18.000000Z 字数 10585 阅读 1085

多项式加法

java


Description
给定两个多项式P(x)与Q(x),通过链表实现它们的加法。
Input
有若干组,每组有2个多项式。每组输入时先输入第1个多项式,再输入第2个多项式。每组用若干对整数表示,依序每两个相邻的整数表示多项式的一项,分别是它的系数和幂。当输入的幂为负数时,表示一个多项式的结束。一个多项式中各项的次序是随机的。
Output
对每组中两个多项式,先输出“Case #:”,其中“#”是测试数据序号。接着在下面三行上分别输出这两多项式,以及这两多项式的和。要求按降幂排列输出多项式的各项,每一项用(a,n)对的形式,其中a是系数,n是幂。相邻两对之间无空格。系数为零的项不输出。对零多项式,只输出0。
Sample Input
-1 2 3 -1 1 2 2 -1
-2 2 4 4 3 -1 2 2 6 1 5 -1
Sample Output
Case 1:
(-1,2)
(1,2)
0
Case 2:
(4,4)(-2,2)
(2,2)(6,1)
(4,4)(6,1)

图镇

此处输入图片的描述

一路艰辛一路坑(第一份代码是通过的, 其他都是不通过的):

感觉题目也没什么好讲的. 显然是一道水题. 虽然说用的是链表, 但是好久没有用过这个东西了, 自己首先想到的还是一个键值对数组或者集合之类的玩意.
假设系数用key来存, 指数用value
一开始想到的是map,但是因为题目要用key做运算, 用value做排序,之前没有接触过map,直接跳过. 现在回过头想, 似乎用map也可以,而且如果用key保存指数会不会更简单.
最后选择了用一个list<key,value>来维护, 于是就有了第一份错误代码, 也正式开始入坑.
可能是好久没有写这种东西了, 自己首先想到的直接就是<Key , Value>泛型

哈哈哈好像我用到了C#的名词, 哈哈哈不管了继续说

  1. 用javafx.util.Pair做键值对, 一路写下来很顺畅. 开心一提交, 判题系统不支持这个包.
  2. 问了得知单个键值对可以用Entry存储, 但是这是个接口, 于是第二个版本出来了, 自己实现了这个接口, 修改几下代码, 一测试, 通过. 信心满满一提交, arrayList.sort(c)直接运行错误, 不知道是哪里的问题.
  3. 自己猜测是不是泛型不好用? 于是直接把泛型去掉, 装两个int属性的 类. 代码轻轻一改, 提交. 还是有问题.
  4. 那既然这样, 就是内置srot方法有问题咯, 很显然这个时候已经深深陷入这个问题出不来了, 那你sort有问题我就给你写一个sort咯, 好第四个错误代码版本出来. 重写compare, 自己构造一个sort, 提交, 报一个新错误我的分割方法里有空串, 转成int异常
    看起来sort这一关过去了但是
    hhh心好累
    然后偷偷看了组长的c++代码,模仿了一下:每次读一行之后, 以这个行作为输入源, 每次读入一个int, 显然这个思路用java比c++好实现, 好吧那就nextInt()咯
    这个代码一题叫, 诶Wrong Answer
    噢我的天, WA了, 那个高兴....
    第一个wa:
  1. =================/input.out
  2. Right:
  3. (6,3)(1,2)
  4. -----------------
  5. Your:
  6. (6,3) (1,2)
  7. =================

哦原来是多输出一个空格, 然后看看代码, 诶我最后输出额米有多余空格啊, 往前翻啊翻,
哦, 输出原多项式的时候多输了一个, 好吧, 去掉, 提交
第二个wa

  1. =================/input.out
  2. Right:
  3. 3,4)(6,3)(1,2)
  4. -----------------
  5. Your:
  6. 0,6)(3,4)(6,3)(1,2)
  7. =================

这...什么可能!!! 我明明判断0就不输出啊
啊啊啊仔细看看代码

又是输出原多项式的时候忘记了
神经病啊
你多项式写个有意思!!!
还让我给你判断
完了我直接给你打印出来你还不肯

好吧神经病鬼神经病, 代码还是要交的
加个if
提交
ac


通过的代码, 虽然难看的自己都不好意思, 但是终究还是要贴上来的. 毕竟
许久没有对自己智商绝望到绝望的地步了

  1. package com.company;
  2. import java.util.*;
  3. class element{
  4. int key;
  5. int value;
  6. public element(int k, int v){
  7. this.key = k;
  8. this.value = v;
  9. }
  10. public int getKey() {
  11. return this.key;
  12. }
  13. public int getValue() {
  14. return this.value;
  15. }
  16. public int setValue(int value) {
  17. this.value = value;
  18. return value;
  19. }
  20. }
  21. public class Main {
  22. static Comparator<element> c = new Comparator<element>() {
  23. @Override
  24. public int compare(element o1, element o2) {
  25. return o2.getValue()-o1.getValue();
  26. }
  27. };
  28. public static void mysort(LinkedList<element> a, Comparator c){
  29. int N = a.size();
  30. for (int i=1; i<N; i++)
  31. for(int j=i;j>0 && less(c,a.get(j),a.get(j-1)); j--)
  32. exch(a,j,j-1);
  33. }
  34. private static boolean less(Comparator c, element v, element w){
  35. return c.compare(v,w) < 0;
  36. }
  37. private static void exch(LinkedList a, int i, int j){
  38. Object t = a.get(i);
  39. a.set(i,a.get(j));
  40. a.set(j,t);
  41. }
  42. public static void main(String[] args) {
  43. Scanner scanner = new Scanner(System.in);
  44. LinkedList<element> la = new LinkedList<>();
  45. LinkedList<element> lb = new LinkedList<>();
  46. int Index = 1;
  47. while (scanner.hasNext()){
  48. String line = scanner.nextLine();
  49. Scanner stringin = new Scanner(line);
  50. System.out.println("Case "+(Index++)+":");
  51. while (stringin.hasNext()){
  52. int d = stringin.nextInt();
  53. int m = stringin.nextInt();
  54. if (m>=0){
  55. la.add(new element(d,m));
  56. continue;
  57. }
  58. mysort(la,c);
  59. for (element anArrayList : la) {
  60. if(anArrayList.getKey() != 0)
  61. System.out.print("(" + anArrayList.getKey() + "," + anArrayList.getValue() + ")");
  62. }
  63. System.out.println();
  64. lb.addAll(la);
  65. la.clear();
  66. }
  67. mysort(lb,c);
  68. int i=1;
  69. boolean isZero = true;
  70. while(i<lb.size()){
  71. if(Objects.equals(lb.get(i - 1).getValue(), lb.get(i).getValue())){
  72. int sum = lb.get(i-1).getKey()+lb.get(i).getKey();
  73. lb.set(i-1,new element(sum,lb.get(i-1).getValue()));
  74. lb.remove(i);
  75. }
  76. else{
  77. i++;
  78. }
  79. }
  80. for(i=0;i<lb.size();i++){
  81. if(lb.get(i).getKey() != 0){
  82. isZero = false;
  83. System.out.print("("+lb.get(i).getKey()+","+lb.get(i).getValue()+")");
  84. }
  85. }
  86. if (isZero)
  87. System.out.println("0");
  88. else
  89. System.out.println();
  90. la.clear();
  91. lb.clear();
  92. }
  93. }
  94. }

这是我实现的一个版本, 但是判题系统不给用javafx.util.Pair

  1. package com.company;
  2. import java.math.BigDecimal;
  3. import java.math.BigInteger;
  4. import javafx.util.Pair;
  5. import java.util.*;
  6. public class Main {
  7. static Comparator<Pair<Integer,Integer>> c = new Comparator<Pair<Integer, Integer>>() {
  8. @Override
  9. public int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) {
  10. return o2.getValue()-o1.getValue();
  11. }
  12. };
  13. public static void main(String[] args) {
  14. Scanner scanner = new Scanner(System.in);
  15. ArrayList<Pair<Integer,Integer>> arrayList = new ArrayList<>();
  16. ArrayList<Pair<Integer,Integer>> arrayListSum = new ArrayList<>();
  17. int Index = 1;
  18. while (scanner.hasNext()){
  19. String [] first = scanner.nextLine().split(" ");
  20. System.out.println("Case "+(Index++)+":");
  21. for(int x = 0; x<first.length;x+=2){
  22. int d = Integer.parseInt(first[x]);
  23. int m = Integer.parseInt(first[x+1]);
  24. if (m>=0){
  25. arrayList.add(new Pair<>(d,m));
  26. continue;
  27. }
  28. arrayList.sort(c);
  29. for (Pair<Integer, Integer> anArrayList : arrayList) {
  30. System.out.print("(" + anArrayList.getKey() + "," + anArrayList.getValue() + ") ");
  31. }
  32. System.out.println();
  33. arrayListSum.addAll(arrayList);
  34. arrayList.clear();
  35. }
  36. arrayList.clear();
  37. arrayList.addAll(arrayListSum);
  38. arrayList.sort(c);
  39. int i=1;
  40. boolean isZero = true;
  41. while(i<arrayList.size()){
  42. if(Objects.equals(arrayList.get(i-1).getValue(), arrayList.get(i).getValue())){
  43. int sum = arrayList.get(i-1).getKey()+arrayList.get(i).getKey();
  44. arrayList.set(i-1,new Pair<>(sum,arrayList.get(i-1).getValue()));
  45. arrayList.remove(i);
  46. }
  47. else{
  48. i++;
  49. }
  50. }
  51. for(i=0;i<arrayList.size();i++){
  52. if(arrayList.get(i).getKey() != 0){
  53. isZero = false;
  54. System.out.print("("+arrayList.get(i).getKey()+","+arrayList.get(i).getValue()+")");
  55. }
  56. }
  57. if (isZero==true)
  58. System.out.println("0");
  59. else
  60. System.out.println();
  61. }
  62. }
  63. }

第二次写成这个鬼样子, 我觉得已经超出了题目的意思了.
虽然还是没有过////
说什么 arrayList.sort(c) 有问题.... 本地可以跑.....

  1. package com.company;
  2. import java.util.*;
  3. import java.util.Map.Entry;
  4. import java.util.ArrayList;
  5. class myEntry<K,V> implements Entry<K,V>{
  6. K key;
  7. V value;
  8. public myEntry(K k, V v){
  9. this.key = k;
  10. this.value = v;
  11. }
  12. @Override
  13. public K getKey() {
  14. return this.key;
  15. }
  16. @Override
  17. public V getValue() {
  18. return this.value;
  19. }
  20. @Override
  21. public V setValue(V value) {
  22. this.value = value;
  23. return value;
  24. }
  25. }
  26. public class Main {
  27. static Comparator<myEntry<Integer,Integer>> c = (o1, o2) -> o2.getValue()-o1.getValue();
  28. public static void main(String[] args) {
  29. Scanner scanner = new Scanner(System.in);
  30. ArrayList<myEntry<Integer,Integer>> arrayList = new ArrayList<>();
  31. ArrayList<myEntry<Integer,Integer>> arrayListSum = new ArrayList<>();
  32. int Index = 1;
  33. while (scanner.hasNext()){
  34. String [] first = scanner.nextLine().split("[\\p{Blank}]");
  35. System.out.println("Case "+(Index++)+":");
  36. for(int x = 0; x<first.length;x+=2){
  37. int d = Integer.parseInt(first[x]);
  38. int m = Integer.parseInt(first[x+1]);
  39. if (m>=0){
  40. arrayList.add(new myEntry<>(d,m));
  41. continue;
  42. }
  43. arrayList.sort(c);
  44. for (myEntry<Integer, Integer> anArrayList : arrayList) {
  45. System.out.print("(" + anArrayList.getKey() + "," + anArrayList.getValue() + ") ");
  46. }
  47. System.out.println();
  48. arrayListSum.addAll(arrayList);
  49. arrayList.clear();
  50. }
  51. arrayList.clear();
  52. arrayList.addAll(arrayListSum);
  53. arrayList.sort(c);
  54. int i=1;
  55. boolean isZero = true;
  56. while(i<arrayList.size()){
  57. if(Objects.equals(arrayList.get(i - 1).getValue(), arrayList.get(i).getValue())){
  58. int sum = arrayList.get(i-1).getKey()+arrayList.get(i).getKey();
  59. arrayList.set(i-1,new myEntry<>(sum,arrayList.get(i-1).getValue()));
  60. arrayList.remove(i);
  61. }
  62. else{
  63. i++;
  64. }
  65. }
  66. for(i=0;i<arrayList.size();i++){
  67. if(arrayList.get(i).getKey() != 0){
  68. isZero = false;
  69. System.out.print("("+arrayList.get(i).getKey()+","+arrayList.get(i).getValue()+")");
  70. }
  71. }
  72. if (isZero)
  73. System.out.println("0");
  74. else
  75. System.out.println();
  76. }
  77. }
  78. }

换汤不换药再来一波

  1. package com.company;
  2. import java.util.*;
  3. class element{
  4. int key;
  5. int value;
  6. public element(int k, int v){
  7. this.key = k;
  8. this.value = v;
  9. }
  10. public int getKey() {
  11. return this.key;
  12. }
  13. public int getValue() {
  14. return this.value;
  15. }
  16. public int setValue(int value) {
  17. this.value = value;
  18. return value;
  19. }
  20. }
  21. public class Main {
  22. static Comparator<element> c = new Comparator<element>() {
  23. @Override
  24. public int compare(element o1, element o2) {
  25. return o2.getValue()-o1.getValue();
  26. }
  27. };
  28. public static void main(String[] args) {
  29. Scanner scanner = new Scanner(System.in);
  30. List<element> la = new LinkedList<>();
  31. List<element> lb = new LinkedList<>();
  32. int Index = 1;
  33. while (scanner.hasNext()){
  34. String [] first = scanner.nextLine().split("[\\p{Blank}]");
  35. System.out.println("Case "+(Index++)+":");
  36. for(int x = 0; x<first.length;x+=2){
  37. int d = Integer.parseInt(first[x]);
  38. int m = Integer.parseInt(first[x+1]);
  39. if (m>=0){
  40. la.add(new element(d,m));
  41. continue;
  42. }
  43. la.sort(c);
  44. for (element anArrayList : la) {
  45. System.out.print("(" + anArrayList.getKey() + "," + anArrayList.getValue() + ") ");
  46. }
  47. System.out.println();
  48. lb.addAll(la);
  49. la.clear();
  50. }
  51. lb.sort(c);
  52. int i=1;
  53. boolean isZero = true;
  54. while(i<lb.size()){
  55. if(Objects.equals(lb.get(i - 1).getValue(), lb.get(i).getValue())){
  56. int sum = lb.get(i-1).getKey()+lb.get(i).getKey();
  57. lb.set(i-1,new element(sum,lb.get(i-1).getValue()));
  58. lb.remove(i);
  59. }
  60. else{
  61. i++;
  62. }
  63. }
  64. for(i=0;i<lb.size();i++){
  65. if(lb.get(i).getKey() != 0){
  66. isZero = false;
  67. System.out.print("("+lb.get(i).getKey()+","+lb.get(i).getValue()+")");
  68. }
  69. }
  70. if (isZero)
  71. System.out.println("0");
  72. else
  73. System.out.println();
  74. }
  75. }
  76. }

手动重写sort方法
顺便贴一下错误信息:

  1. Main.java:47: error: cannot find symbol
  2. la.sort(c);
  3. ^
  4. symbol: method sort(Comparator<element>)
  5. location: variable la of type List<element>
  6. Main.java:55: error: cannot find symbol
  7. lb.sort(c);
  8. ^
  9. symbol: method sort(Comparator<element>)
  10. location: variable lb of type List<element>
  11. 2 errors

编译通过的代码, 运行错误

  1. Exception in thread "main" java.lang.NumberFormatException: For input string: ""
  2. at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
  3. at java.lang.Integer.parseInt(Integer.java:504)
  4. at java.lang.Integer.parseInt(Integer.java:527)
  5. at Main.main(Main.java:55)
  1. package com.company;
  2. import java.util.*;
  3. class element{
  4. int key;
  5. int value;
  6. public element(int k, int v){
  7. this.key = k;
  8. this.value = v;
  9. }
  10. public int getKey() {
  11. return this.key;
  12. }
  13. public int getValue() {
  14. return this.value;
  15. }
  16. public int setValue(int value) {
  17. this.value = value;
  18. return value;
  19. }
  20. }
  21. public class Main {
  22. static Comparator<element> c = new Comparator<element>() {
  23. @Override
  24. public int compare(element o1, element o2) {
  25. return o2.getValue()-o1.getValue();
  26. }
  27. };
  28. public static void mysort(LinkedList<element> a, Comparator c){
  29. int N = a.size();
  30. for (int i=1; i<N; i++)
  31. for(int j=i;j>0 && less(c,a.get(j),a.get(j-1)); j--)
  32. exch(a,j,j-1);
  33. }
  34. private static boolean less(Comparator c, element v, element w){
  35. return c.compare(v,w) < 0;
  36. }
  37. private static void exch(LinkedList a, int i, int j){
  38. Object t = a.get(i);a.set(i,a.get(j));a.set(j,t);
  39. }
  40. public static void main(String[] args) {
  41. Scanner scanner = new Scanner(System.in);
  42. LinkedList<element> la = new LinkedList<>();
  43. LinkedList<element> lb = new LinkedList<>();
  44. int Index = 1;
  45. while (scanner.hasNext()){
  46. String [] first = scanner.nextLine().trim().split("[\\p{Blank}]");
  47. System.out.println("Case "+(Index++)+":");
  48. for(int x = 0; x<first.length-1;x+=2){
  49. int d = Integer.parseInt(first[x]);
  50. int m = Integer.parseInt(first[x+1]);
  51. if (m>=0){
  52. la.add(new element(d,m));
  53. continue;
  54. }
  55. mysort(la,c);
  56. for (element anArrayList : la) {
  57. System.out.print("(" + anArrayList.getKey() + "," + anArrayList.getValue() + ") ");
  58. }
  59. System.out.println();
  60. lb.addAll(la);
  61. la.clear();
  62. }
  63. mysort(lb,c);
  64. int i=1;
  65. boolean isZero = true;
  66. while(i<lb.size()){
  67. if(Objects.equals(lb.get(i - 1).getValue(), lb.get(i).getValue())){
  68. int sum = lb.get(i-1).getKey()+lb.get(i).getKey();
  69. lb.set(i-1,new element(sum,lb.get(i-1).getValue()));
  70. lb.remove(i);
  71. }
  72. else{
  73. i++;
  74. }
  75. }
  76. for(i=0;i<lb.size();i++){
  77. if(lb.get(i).getKey() != 0){
  78. isZero = false;
  79. System.out.print("("+lb.get(i).getKey()+","+lb.get(i).getValue()+")");
  80. }
  81. }
  82. if (isZero)
  83. System.out.println("0");
  84. else
  85. System.out.println();
  86. la.clear();
  87. lb.clear();
  88. }
  89. }
  90. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注