PAT:1002. A+B for Polynomials (25)

import java.util.Scanner;
import java.util.Vector;
import java.util.Iterator;


class  Polynomials_1002
{
public static void main(String[] args) 
{
try{
int  e;
float coe;
Attr []tmp=new Attr[1];
Vector <Attr>poly=new Vector<Attr>();


System.out.println("please input number of nonzero terms:k||A");
Scanner in=new Scanner(System.in);
int a=in.nextInt();
while(a>0){
System.out.println("please input the coe");
coe=in.nextFloat();
System.out.println("please input the e");
e=in.nextInt();
poly.add(new Attr(coe,e));
a--;


}
System.out.println("please input number of nonzero terms:k||B");
a=in.nextInt();
while(a>0){
System.out.println("please input the coe");
coe=in.nextFloat();
System.out.println("please input the e");
e=in.nextInt();


if(cmp(poly,new Attr(coe,e),tmp)){


int loc=poly.indexOf(tmp[0]);
System.out.printf("the a' loc %d",loc);
poly.get(loc).coe+=coe;
}
else{
poly.add(new Attr(coe,e));
}
a--; 
}
show(poly);       
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}






}




public static boolean cmp(Vector <Attr>v,Attr a,Attr []target){
Iterator<Attr> it = v.iterator();
while (it.hasNext()){
Attr tmp= (Attr)it.next();
if(tmp.e==a.e){
target[0]=tmp;
return true;
}
}
return false;
}


public static void show(Vector <Attr>v){
Iterator<Attr> it = v.iterator();
System.out.printf("the poly :%d ",v.size());
while (it.hasNext()){
Attr a=(Attr)it.next();
System.out.printf("%d ",a.e);
System.out.printf("%9.2f ",a.coe);
}
}
}






class Attr
{
public float coe=0;
public int e=0;
public  Attr(float coe,int e){
this.coe=coe;
this.e=e;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值