zoukankan      html  css  js  c++  java
  • pat甲级1002

    1002. A+B for Polynomials (25)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    This time, you are supposed to find A+B where A and B are two polynomials.

    Input

    Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 ... NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, ..., K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10,0 <= NK < ... < N2 < N1 <=1000.

    Output

    For each test case you should output the sum of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate to 1 decimal place.

    Sample Input
    2 1 2.4 0 3.2
    2 2 1.5 1 0.5
    
    Sample Output
    3 2 1.5 1 2.9 0 3.2

     1 #include<cstdio>
     2 struct node
     3 {
     4     int e;//指数
     5     double cof;//系数
     6 }a[1500],b[1500];
     7 double ans[2002]={0};
     8 int main()
     9 {
    10     int m,n,sum=0;
    11     scanf("%d",&m);
    12     for(int i=0;i<m;i++)
    13     {
    14         scanf("%d %lf",&a[i].e,&a[i].cof);
    15         ans[a[i].e]+=a[i].cof;
    16     }
    17     scanf("%d",&n);
    18     sum=m+n;
    19     for(int i=0;i<n;i++)
    20     {
    21         scanf("%d %lf",&b[i].e,&b[i].cof);
    22         ans[b[i].e]+=b[i].cof;
    23     }
    24     for(int i=0;i<m;i++)
    25     {
    26         for(int j=0;j<n;j++)
    27         {
    28             if(a[i].e==b[j].e)
    29                 sum--;
    30         }
    31     }
    32 
    33     printf("%d",sum);
    34     for(int i=2000;i>=0;i--)
    35     {
    36         if(ans[i]!=0)
    37             printf(" %d %.1lf",i,ans[i]);
    38     }
    39 
    40 
    41     return 0;
    42 }

    有四个测试点过不去,应该是最后计含有非零项多项式时候少考虑了一种情况——两个指数相同的项的系数和为0

    #include<cstdio>
    struct node
    {
    	int e;//指数
    	double cof;//系数
    }a[1500],b[1500];
    double ans[2002]={0};
    int main()
    {
    	int m,n,sum=0;
    	scanf("%d",&m);
    	for(int i=0;i<m;i++)
    	{
    		scanf("%d %lf",&a[i].e,&a[i].cof);
    		ans[a[i].e]+=a[i].cof;
    	}
    	scanf("%d",&n);
    	sum=m+n;
    	for(int i=0;i<n;i++)
    	{
    		scanf("%d %lf",&b[i].e,&b[i].cof);
    		ans[b[i].e]+=b[i].cof;
    	}
    	for(int i=0;i<m;i++)
    	{
    		for(int j=0;j<n;j++)
    		{
    			if(a[i].e==b[j].e)
    				sum--;
    			if(a[i].e==b[j].e&&a[i].cof+b[j].cof==0)
    				sum--;
    		}
    	}
    
    	printf("%d",sum);
    	for(int i=2000;i>=0;i--)
    	{
    		if(ans[i]!=0)
    			printf(" %d %.1lf",i,ans[i]);
    	}
    
    
    	return 0;
    }
    

     改动一段代码果然解决了问题

  • 相关阅读:
    Dilworth定理,链还是反链?
    pku 3259Wormholes
    hdu 2612 Find a way
    hdu 2614
    hdu 2544 最短路
    hdu 2553 N皇后问题
    hdu2717 Catch That Cow
    hdu 1874 畅通工程续
    jquery学习必备代码和技巧
    HTML5 WebApp part4:使用 Web Workers 来加速您的移动 Web 应用程序(上)
  • 原文地址:https://www.cnblogs.com/brucekun/p/6511922.html
Copyright © 2011-2022 走看看