码迷,mamicode.com
首页 > 其他好文 > 详细

PAT:1002. A+B for Polynomials (25) 部分错误

时间:2015-02-17 23:33:31      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//【warning】double 输入%lf,输出%f
struct arr
{
  int tag;
  double data;
}arr[1005];

int main()
{
  memset(arr,0,sizeof(arr));
  int t1,t2,tmp,maxI=0;          //maxI记录最多多少项
  double tdata;

  for(int I=0 ; I<2 ; ++I)        //输入第一个和第二个多项式
  {
    scanf("%d",&t1);          //多项式有t1项
    if(t1>maxI)
      maxI=t1;
    for(int i=0 ; i<t1 ; ++i)      //输入多项式
    {
      scanf("%d",&tmp);
      arr[tmp].tag=1;          //修改过,设置为有效
      scanf("%lf",&tdata);
      arr[tmp].data+=tdata;      //加上这项前系数
      if(arr[tmp].data==0)      //正负抵消,没有此项了,设为无效
        arr[tmp].tag=0;
    }
  }
  int len=0;                //len记录多项式项数
  for(int i=0 ; i<=maxI ; ++i)
    if(arr[i].tag==1)
      ++len;
  printf("%d",len);
  if(len!=0)
  {
    printf(" ");
    for(int i=maxI ; i>=0 ; --i)        //从后向前找tag为1的有效项
      if(arr[i].tag==1)
      {
        printf("%d %.1f",i,arr[i].data);
        if(i!=0)
          printf(" ");          //保证只有中间有空格
      }
  }
  printf("\n");
  //system("pause");
  return 0;
}

PAT:1002. A+B for Polynomials (25) 部分错误

标签:

原文地址:http://www.cnblogs.com/Evence/p/4295538.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!