@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#的名词, 哈哈哈不管了继续说
- 用javafx.util.Pair做键值对, 一路写下来很顺畅. 开心一提交, 判题系统不支持这个包.
- 问了得知单个键值对可以用Entry存储, 但是这是个接口, 于是第二个版本出来了, 自己实现了这个接口, 修改几下代码, 一测试, 通过. 信心满满一提交, arrayList.sort(c)直接运行错误, 不知道是哪里的问题.
- 自己猜测是不是泛型不好用? 于是直接把泛型去掉, 装两个int属性的 类. 代码轻轻一改, 提交. 还是有问题.
- 那既然这样, 就是内置srot方法有问题咯, 很显然这个时候已经深深陷入这个问题出不来了, 那你sort有问题我就给你写一个sort咯, 好第四个错误代码版本出来. 重写compare, 自己构造一个sort, 提交, 报一个新错误我的分割方法里有空串, 转成int异常
看起来sort这一关过去了但是
hhh心好累
然后偷偷看了组长的c++代码,模仿了一下:每次读一行之后, 以这个行作为输入源, 每次读入一个int, 显然这个思路用java比c++好实现, 好吧那就nextInt()咯
这个代码一题叫, 诶Wrong Answer
噢我的天, WA了, 那个高兴....
第一个wa:
=================/input.outRight:(6,3)(1,2)-----------------Your:(6,3) (1,2)=================
哦原来是多输出一个空格, 然后看看代码, 诶我最后输出额米有多余空格啊, 往前翻啊翻,
哦, 输出原多项式的时候多输了一个, 好吧, 去掉, 提交
第二个wa
=================/input.outRight:3,4)(6,3)(1,2)-----------------Your:0,6)(3,4)(6,3)(1,2)=================
这...什么可能!!! 我明明判断0就不输出啊
啊啊啊仔细看看代码
哦
又是输出原多项式的时候忘记了
神经病啊
你多项式写个有意思!!!
还让我给你判断
完了我直接给你打印出来你还不肯
好吧神经病鬼神经病, 代码还是要交的
加个if
提交
ac
通过的代码, 虽然难看的自己都不好意思, 但是终究还是要贴上来的. 毕竟
许久没有对自己智商绝望到绝望的地步了
package com.company;import java.util.*;class element{int key;int value;public element(int k, int v){this.key = k;this.value = v;}public int getKey() {return this.key;}public int getValue() {return this.value;}public int setValue(int value) {this.value = value;return value;}}public class Main {static Comparator<element> c = new Comparator<element>() {@Overridepublic int compare(element o1, element o2) {return o2.getValue()-o1.getValue();}};public static void mysort(LinkedList<element> a, Comparator c){int N = a.size();for (int i=1; i<N; i++)for(int j=i;j>0 && less(c,a.get(j),a.get(j-1)); j--)exch(a,j,j-1);}private static boolean less(Comparator c, element v, element w){return c.compare(v,w) < 0;}private static void exch(LinkedList a, int i, int j){Object t = a.get(i);a.set(i,a.get(j));a.set(j,t);}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);LinkedList<element> la = new LinkedList<>();LinkedList<element> lb = new LinkedList<>();int Index = 1;while (scanner.hasNext()){String line = scanner.nextLine();Scanner stringin = new Scanner(line);System.out.println("Case "+(Index++)+":");while (stringin.hasNext()){int d = stringin.nextInt();int m = stringin.nextInt();if (m>=0){la.add(new element(d,m));continue;}mysort(la,c);for (element anArrayList : la) {if(anArrayList.getKey() != 0)System.out.print("(" + anArrayList.getKey() + "," + anArrayList.getValue() + ")");}System.out.println();lb.addAll(la);la.clear();}mysort(lb,c);int i=1;boolean isZero = true;while(i<lb.size()){if(Objects.equals(lb.get(i - 1).getValue(), lb.get(i).getValue())){int sum = lb.get(i-1).getKey()+lb.get(i).getKey();lb.set(i-1,new element(sum,lb.get(i-1).getValue()));lb.remove(i);}else{i++;}}for(i=0;i<lb.size();i++){if(lb.get(i).getKey() != 0){isZero = false;System.out.print("("+lb.get(i).getKey()+","+lb.get(i).getValue()+")");}}if (isZero)System.out.println("0");elseSystem.out.println();la.clear();lb.clear();}}}
这是我实现的一个版本, 但是判题系统不给用javafx.util.Pair
package com.company;import java.math.BigDecimal;import java.math.BigInteger;import javafx.util.Pair;import java.util.*;public class Main {static Comparator<Pair<Integer,Integer>> c = new Comparator<Pair<Integer, Integer>>() {@Overridepublic int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) {return o2.getValue()-o1.getValue();}};public static void main(String[] args) {Scanner scanner = new Scanner(System.in);ArrayList<Pair<Integer,Integer>> arrayList = new ArrayList<>();ArrayList<Pair<Integer,Integer>> arrayListSum = new ArrayList<>();int Index = 1;while (scanner.hasNext()){String [] first = scanner.nextLine().split(" ");System.out.println("Case "+(Index++)+":");for(int x = 0; x<first.length;x+=2){int d = Integer.parseInt(first[x]);int m = Integer.parseInt(first[x+1]);if (m>=0){arrayList.add(new Pair<>(d,m));continue;}arrayList.sort(c);for (Pair<Integer, Integer> anArrayList : arrayList) {System.out.print("(" + anArrayList.getKey() + "," + anArrayList.getValue() + ") ");}System.out.println();arrayListSum.addAll(arrayList);arrayList.clear();}arrayList.clear();arrayList.addAll(arrayListSum);arrayList.sort(c);int i=1;boolean isZero = true;while(i<arrayList.size()){if(Objects.equals(arrayList.get(i-1).getValue(), arrayList.get(i).getValue())){int sum = arrayList.get(i-1).getKey()+arrayList.get(i).getKey();arrayList.set(i-1,new Pair<>(sum,arrayList.get(i-1).getValue()));arrayList.remove(i);}else{i++;}}for(i=0;i<arrayList.size();i++){if(arrayList.get(i).getKey() != 0){isZero = false;System.out.print("("+arrayList.get(i).getKey()+","+arrayList.get(i).getValue()+")");}}if (isZero==true)System.out.println("0");elseSystem.out.println();}}}
第二次写成这个鬼样子, 我觉得已经超出了题目的意思了.
虽然还是没有过////
说什么 arrayList.sort(c) 有问题.... 本地可以跑.....
package com.company;import java.util.*;import java.util.Map.Entry;import java.util.ArrayList;class myEntry<K,V> implements Entry<K,V>{K key;V value;public myEntry(K k, V v){this.key = k;this.value = v;}@Overridepublic K getKey() {return this.key;}@Overridepublic V getValue() {return this.value;}@Overridepublic V setValue(V value) {this.value = value;return value;}}public class Main {static Comparator<myEntry<Integer,Integer>> c = (o1, o2) -> o2.getValue()-o1.getValue();public static void main(String[] args) {Scanner scanner = new Scanner(System.in);ArrayList<myEntry<Integer,Integer>> arrayList = new ArrayList<>();ArrayList<myEntry<Integer,Integer>> arrayListSum = new ArrayList<>();int Index = 1;while (scanner.hasNext()){String [] first = scanner.nextLine().split("[\\p{Blank}]");System.out.println("Case "+(Index++)+":");for(int x = 0; x<first.length;x+=2){int d = Integer.parseInt(first[x]);int m = Integer.parseInt(first[x+1]);if (m>=0){arrayList.add(new myEntry<>(d,m));continue;}arrayList.sort(c);for (myEntry<Integer, Integer> anArrayList : arrayList) {System.out.print("(" + anArrayList.getKey() + "," + anArrayList.getValue() + ") ");}System.out.println();arrayListSum.addAll(arrayList);arrayList.clear();}arrayList.clear();arrayList.addAll(arrayListSum);arrayList.sort(c);int i=1;boolean isZero = true;while(i<arrayList.size()){if(Objects.equals(arrayList.get(i - 1).getValue(), arrayList.get(i).getValue())){int sum = arrayList.get(i-1).getKey()+arrayList.get(i).getKey();arrayList.set(i-1,new myEntry<>(sum,arrayList.get(i-1).getValue()));arrayList.remove(i);}else{i++;}}for(i=0;i<arrayList.size();i++){if(arrayList.get(i).getKey() != 0){isZero = false;System.out.print("("+arrayList.get(i).getKey()+","+arrayList.get(i).getValue()+")");}}if (isZero)System.out.println("0");elseSystem.out.println();}}}
换汤不换药再来一波
package com.company;import java.util.*;class element{int key;int value;public element(int k, int v){this.key = k;this.value = v;}public int getKey() {return this.key;}public int getValue() {return this.value;}public int setValue(int value) {this.value = value;return value;}}public class Main {static Comparator<element> c = new Comparator<element>() {@Overridepublic int compare(element o1, element o2) {return o2.getValue()-o1.getValue();}};public static void main(String[] args) {Scanner scanner = new Scanner(System.in);List<element> la = new LinkedList<>();List<element> lb = new LinkedList<>();int Index = 1;while (scanner.hasNext()){String [] first = scanner.nextLine().split("[\\p{Blank}]");System.out.println("Case "+(Index++)+":");for(int x = 0; x<first.length;x+=2){int d = Integer.parseInt(first[x]);int m = Integer.parseInt(first[x+1]);if (m>=0){la.add(new element(d,m));continue;}la.sort(c);for (element anArrayList : la) {System.out.print("(" + anArrayList.getKey() + "," + anArrayList.getValue() + ") ");}System.out.println();lb.addAll(la);la.clear();}lb.sort(c);int i=1;boolean isZero = true;while(i<lb.size()){if(Objects.equals(lb.get(i - 1).getValue(), lb.get(i).getValue())){int sum = lb.get(i-1).getKey()+lb.get(i).getKey();lb.set(i-1,new element(sum,lb.get(i-1).getValue()));lb.remove(i);}else{i++;}}for(i=0;i<lb.size();i++){if(lb.get(i).getKey() != 0){isZero = false;System.out.print("("+lb.get(i).getKey()+","+lb.get(i).getValue()+")");}}if (isZero)System.out.println("0");elseSystem.out.println();}}}
手动重写sort方法
顺便贴一下错误信息:
Main.java:47: error: cannot find symbolla.sort(c);^symbol: method sort(Comparator<element>)location: variable la of type List<element>Main.java:55: error: cannot find symbollb.sort(c);^symbol: method sort(Comparator<element>)location: variable lb of type List<element>2 errors
编译通过的代码, 运行错误
Exception in thread "main" java.lang.NumberFormatException: For input string: ""at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)at java.lang.Integer.parseInt(Integer.java:504)at java.lang.Integer.parseInt(Integer.java:527)at Main.main(Main.java:55)
package com.company;import java.util.*;class element{int key;int value;public element(int k, int v){this.key = k;this.value = v;}public int getKey() {return this.key;}public int getValue() {return this.value;}public int setValue(int value) {this.value = value;return value;}}public class Main {static Comparator<element> c = new Comparator<element>() {@Overridepublic int compare(element o1, element o2) {return o2.getValue()-o1.getValue();}};public static void mysort(LinkedList<element> a, Comparator c){int N = a.size();for (int i=1; i<N; i++)for(int j=i;j>0 && less(c,a.get(j),a.get(j-1)); j--)exch(a,j,j-1);}private static boolean less(Comparator c, element v, element w){return c.compare(v,w) < 0;}private static void exch(LinkedList a, int i, int j){Object t = a.get(i);a.set(i,a.get(j));a.set(j,t);}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);LinkedList<element> la = new LinkedList<>();LinkedList<element> lb = new LinkedList<>();int Index = 1;while (scanner.hasNext()){String [] first = scanner.nextLine().trim().split("[\\p{Blank}]");System.out.println("Case "+(Index++)+":");for(int x = 0; x<first.length-1;x+=2){int d = Integer.parseInt(first[x]);int m = Integer.parseInt(first[x+1]);if (m>=0){la.add(new element(d,m));continue;}mysort(la,c);for (element anArrayList : la) {System.out.print("(" + anArrayList.getKey() + "," + anArrayList.getValue() + ") ");}System.out.println();lb.addAll(la);la.clear();}mysort(lb,c);int i=1;boolean isZero = true;while(i<lb.size()){if(Objects.equals(lb.get(i - 1).getValue(), lb.get(i).getValue())){int sum = lb.get(i-1).getKey()+lb.get(i).getKey();lb.set(i-1,new element(sum,lb.get(i-1).getValue()));lb.remove(i);}else{i++;}}for(i=0;i<lb.size();i++){if(lb.get(i).getKey() != 0){isZero = false;System.out.print("("+lb.get(i).getKey()+","+lb.get(i).getValue()+")");}}if (isZero)System.out.println("0");elseSystem.out.println();la.clear();lb.clear();}}}