下面是POJ上面的一道编程练习题,以及我写的答案,使用例程的输入还有自己的测试输入结果都是正确的,但是提交之后总是提示“Wrong Answer”,从昨晚改到现在一直不行,不知道到底错在哪里。。。。。。
===================下面是题目========================================
多项式加法 总时间限制:1000ms 内存限制:5000KB
描述:
我们经常遇到两多项式相加的情况,在这里,我们就需要用程序来模拟
实现把两个多项式相加到一起。首先,我们会有两个多项式,每个多项式是
独立的一行,每个多项式由系数、幂数这样的多个整数对来表示。
如多项式2x20- x17+ 5x9- 7x7+ 16x5+ 10x4 + 22x2- 15
对应的表达式为:2 20 -1 17 5 9 - 7 7 16 5 10 4 22 2 -15 0。
为了标记每行多项式的结束,在表达式
后面加上了一个幂数为负数的整数对。
后面加上了一个幂数为负数的整数对。
同时输入表达式的幂数大小顺序是随机的。
我们需要做的就是把所给的两个多项式加起来。
输入
输入包括多行。
第一行整数n,表示有多少组的多项式需要求和。(1下面为2n行整数,每一行都是一个多项式的表达式。表示n组需要相加的多项式。
每行长度小于100。
输出
输出包括n行,每行为1组多项式相加的结果。
在每一行的输出结果中,多项式的每一项用“[x y]”形式的字符串表示,x是该项的系数、y 是该项的幂数。要求按照每一项的幂从高到低排列,即先输出幂数高的项、再输出幂数低的项。
系数为零的项不要输出。
样例输入<div col-9"="">
- 2
- -1 17 2 20 5 9 -7 7 10 4 22 2 -15 0 16 5 0 -1
- 2 19 7 7 3 17 4 4 15 10 -10 5 13 2 -7 0 8 -8
- -1 17 2 23 22 2 6 8 -4 7 -18 0 1 5 21 4 0 -1
- 12 7 -7 5 3 17 23 4 15 10 -10 5 13 5 2 19 9 -7
样例输出
- [ 2 20 ] [ 2 19 ] [ 2 17 ] [ 15 10 ] [ 5 9 ] [ 6 5 ] [ 14 4 ] [ 35 2 ] [ -22 0 ]
- [ 2 23 ] [ 2 19 ] [ 2 17 ] [ 15 10 ] [ 6 8 ] [ 8 7 ] [ -3 5 ] [ 44 4 ] [ 22 2 ] [ -18 0 ]
提示 第一组样例数据的第二行末尾的8 -8,因为幂次-8为负数,所以这一行数据结束,8 -8不要参与计算。
===============================下面是我的程序,改了很多地方都不行=======================================
已经提交了33次了,全都不对,都不敢再提交了
。。。。。。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MAX_PERLINE 110
typedef struct tagPolynomiali
{
int coeff;
int power;
}Polynomial;
int _num;
Polynomial _array1[ MAX_PERLINE ];
Polynomial _array2[ MAX_PERLINE ];
Polynomial _array_result[ 100 ][ MAX_PERLINE*2 ];
/*====================================================================*/
void AddPolynomial(Polynomial a[], int n1, int n2);
void Output(int n);
void Sort(Polynomial *, int);
int main(void){
int i=0;
int cnt1=0;
int cnt2=0;
char s[ MAX_PERLINE ];
char *pch;
_num = atoi( fgets(s,MAX_PERLINE,stdin) ); // 要计算的多项式组个数
for( i=0; i<_num; i++ ){ // 依次计算每组多项式的和
fgets(s,MAX_PERLINE,stdin);
for( pch=strtok(s," "),cnt1=0; pch!=NULL; pch=strtok(NULL," \n"),cnt1++){
if ( cnt1%2 == 0 ){
_array1[ cnt1/2 ].coeff = atoi(pch);
}
else{
_array1[ cnt1/2 ].power = atoi(pch);
}
}
cnt1 = cnt1/2-1; // 最后一项不参与计算
fgets(s,MAX_PERLINE,stdin);
for( pch=strtok(s," "),cnt2=0; pch!=NULL; pch=strtok(NULL," \n"),cnt2++){
if ( cnt2%2 == 0 ){
_array2[ cnt2/2 ].coeff = atoi(pch);
}
else{
_array2[ cnt2/2 ].power = atoi(pch);
}
}
cnt2 = cnt2/2-1;
// 按幂数从大到小排序
Sort(_array1,cnt1);
Sort(_array2,cnt2);
// 计算并保存多项式和
AddPolynomial(_array_result[i],cnt1,cnt2);
}
Output(_num);
return 0;
}
//---------------------------------------------------------
// AddPolynomial()
//
//
void AddPolynomial(Polynomial result[], int n1, int n2){
int coeff; // 系数
int id1;
int id2;
int power; // 幂数
int num; // 多项式和的非零项个数
_array1[ n1 ].power = _array2[ n2 ].power = -1;
for (id1=id2=num=0; id1<n1 || id2<n2; ){
coeff = 0;
power = MAX(_array1[ id1 ].power, _array2[ id2 ].power);
while ( _array1[ id1 ].power == power && id1<n1 ){
coeff += _array1[ id1++ ].coeff;
}
while ( _array2[ id2 ].power == power && id2<n2 ){
coeff += _array2[ id2++ ].coeff;
}
if ( coeff != 0 ) {
result[ num ].coeff = coeff;
result[ num++ ].power = power;
}
}
result[ num ].power = -1; // 负数幂表示多项式和的结束
}
//------------------------------------------------------------
// Output()
//
//
void Output(int n){
int i,j;
for ( i=0; i<n; i++ ){
for ( j=0; _array_result[i][j].power>=0; j++ ){
printf("[ %d %d ] ",_array_result[i][j].coeff, _array_result[i][j].power);
}
printf("\n");
}
}
//----------------------------------------------------------
// Partition()
//
//
int Partition(Polynomial *ptr, int p, int q){
int div = ptr[p].power;
int x = p;
int i;
Polynomial tmp;
for ( i=p+1; i<=q; i++ ){
if ( ptr[i].power > div ){
tmp = ptr[x+1];
ptr[x+1] = ptr[i];
ptr[i] = tmp;
x++;
}
}
tmp = ptr[p];
ptr[p] = ptr[x];
ptr[x] = tmp;
return x;
}
//---------------------------------------------------------
// QuickSort()
//
//
void QuickSort(Polynomial *ptr, int p, int q){
int res;
if ( p >= q ) { return; }
res = Partition(ptr,p,q);
QuickSort(ptr,p,res-1);
QuickSort(ptr,res+1,q);
}
//---------------------------------------------------------
// Sort()
//
//
void Sort(Polynomial *a, int n){
QuickSort(a,0,n-1);
}
/*****************************************************************************************
END FILE
*****************************************************************************************/