50,523
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(1);
LinkedList<Integer> copy = new LinkedList<Integer>();
copy = (LinkedList<Integer>) list.clone();
copy.add(2);
System.out.println("list:"+list); //[1]
System.out.println("copy:"+copy); //[1,2]
System.out.println("---------------------");
LinkedList<Integer> list2 = new LinkedList<Integer>();
list2 = list;
list2.add(3);
System.out.println("list:"+list);//[1,3]
System.out.println("list2:"+list2);//[1,3]
}
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt(); //T组测试数据
for(int i = 0; i < T; i++){
LinkedList<int[]> list1 = new LinkedList<>();
LinkedList<int[]> list2 = new LinkedList<>();
int f = sc.nextInt(); //第一个多项式的长度
int s = sc.nextInt(); //第二个的
//给多项式赋值,分别是系数和指数
int ii = 0;
while(ii < f){
int[] po = new int[2];
po[0] = sc.nextInt();
po[1] = sc.nextInt();
list1.add(po);
ii++;
}
for(int lp = 0; lp < s ;lp++){
int[] pi = new int[2];
pi[0] = sc.nextInt();
pi[1] = sc.nextInt();
list2.add(pi);
}
System.out.println(list1.size()); //输出一下多项式的长度
System.out.println(list2.size());
//把list2复制出来一份,以后除了输出都对temp操作,不改变list2的值
LinkedList<int[]> temp = new LinkedList<>();
temp = (LinkedList<int[]>) list2.clone();
int q = sc.nextInt(); //一共几次操作,其中1代表输出,2多项式相加结果存到list1中,3相减
for(int iii = 0; iii < q; iii++){
int qq = sc.nextInt();
if(qq == 1) mylist.outPut(list1, list2);
else if(qq == 2) mylist.add(list1, temp);
else if(qq == 3) mylist.min(list1, temp);
}
}
}
}
class mylist{
//输出函数的实现
public static void outPut(LinkedList<int[]> list11, LinkedList<int[]> list22){
for(int i = 0;i < list11.size(); i++){
if(list11.get(i)[0] > 0&&i > 0&&list11.get(i)[0] != 0&&list11.get(i)[0] != 100 ) System.out.print("+");
if(list11.get(i)[0] != 0&&list11.get(i)[0] != 100 ) System.out.print(list11.get(i)[0]+"X^"+list11.get(i)[1]);
}
System.out.println();
for(int i = 0; i < list22.size(); i++){
System.out.print(list22.get(i)[0]+"X^"+list22.get(i)[1]);
if(i < list22.size()-1) System.out.print("+");
}
System.out.println();
}
//相加
@SuppressWarnings("unchecked")
public static LinkedList<int[]> add(LinkedList<int[]> list1, LinkedList<int[]> list2){
int[] xc = new int[2];
xc[0] = 100;
xc[1] = 100;
LinkedList<int[]> temp = (LinkedList<int[]>) list2.clone();
for(int i = 0; i < list1.size(); i++){
for(int j = 0; j < temp.size(); j++){
if(temp.get(j)[1] == list1.get(i)[1]) {
System.out.print("系数一样");
list1.get(i)[0] = list1.get(i)[0] + temp.get(j)[0];
temp.set(j, xc);
}
}
}
for(int i = 0; i < temp.size(); i++){
if(temp.get(i)[0] == 100) {
temp.remove(i);
i--;
}
}
list1.addAll(temp);
return temp;
}
//相减
@SuppressWarnings("unchecked")
public static LinkedList<int[]> min(LinkedList<int[]> list1, LinkedList<int[]> list2){
int[] xc = new int[2];
xc[0] = 100;
xc[1] = 100;
LinkedList<int[]> temp = (LinkedList<int[]>) list2.clone(); //这里我又复制了一份,所以最开始的list2理论上是不会改变的,无论怎么操作。
for(int i = 0; i < temp.size(); i++){
int[] x =new int[2];
x[0] = -temp.get(i)[0];
x[1] = temp.get(i)[1];
temp.set(i, x);
}
for(int i = 0; i < list1.size(); i++){
for(int j = 0; j < temp.size(); j++){
if(temp.get(j)[1] == list1.get(i)[1]){
list1.get(i)[0] = list1.get(i)[0] + temp.get(j)[0];
temp.set(j, xc);
}
}
}
for(int i = 0; i < temp.size(); i++){
if(temp.get(i)[1] == 100) {
temp.remove(i);
i--;
}
}
list1.addAll(temp);
return temp;
}
}
LinkedList<int[]> list = new LinkedList<>();
LinkedList<int[]> copyList = (LinkedList<int[]>) list.clone();
System.err.println(list == copyList);
// 附上源码
/**
* Returns a shallow copy of this {@code LinkedList}. (The elements
* themselves are not cloned.)
*
* @return a shallow copy of this {@code LinkedList} instance
*/
public Object clone() {
LinkedList<E> clone = superClone();
// Put clone into "virgin" state
clone.first = clone.last = null;
clone.size = 0;
clone.modCount = 0;
// Initialize clone with our elements
for (Node<E> x = first; x != null; x = x.next)
clone.add(x.item);
return clone;
}