NOJ1005 多项式加法
(代码通过测试,巨坑在于全是0的时候也要输出0)
#include
#include
#include
#define N 200
int main()
{
int ex;
int counttt = 2;
int exp = N;
int value = 0;
int List[2][N] = { 0 };
//input list
while (counttt-- > 0)
{
exp = N;
while (exp!=-1)
{
scanf("%d %d", &value, &exp);
if (exp == -1)
break;
List[counttt][exp] = value;
}
}
//fitst list
for (ex = N-1; ex >= 0; ex--)
{
if (ex == 0 && List[1][ex] == 0)
printf("%d", List[1][ex]);
if (List[1][ex] == 0)
continue;
if (List[1][ex] == 1)
{
if (ex == 1)
printf("X");
else if (ex == 0)
printf("1");
else
printf("X^%d", ex);
break;
}
if (List[1][ex] == -1)
{
if (ex == 1)
printf("-X");
else if (ex == 0)
printf("-1");
else
printf("-X^%d", ex);
break;
}
if (List[1][ex] > 1)
{
if (ex == 1)
printf("%dX",List[1][ex]);
else if (ex == 0)
printf("%d",List[1][ex]);
else
printf("%dX^%d",List[1][ex], ex);
break;
}
if (List[1][ex] < -1) { if (ex == 1) printf("%dX", List[1][ex]); else if (ex == 0) printf("%d", List[1][ex]); else printf("%dX^%d", List[1][ex], ex); break; } } for (ex--; ex >= 0; ex--)
{
if (List[1][ex] == 0)
continue;
if (List[1][ex] == 1)
{
if (ex == 1)
printf("+X");
else if (ex == 0)
printf("+1");
else
printf("+X^%d", ex);
}
if (List[1][ex] == -1)
{
if (ex == 1)
printf("-X");
else if (ex == 0)
printf("-1");
else
printf("-X^%d", ex);
}
if (List[1][ex] > 1)
{
if (ex == 1)
printf("+%dX", List[1][ex]);
else if (ex == 0)
printf("+%d", List[1][ex]);
else
printf("+%dX^%d", List[1][ex], ex);
}
if (List[1][ex] < -1) { if (ex == 1) printf("%dX", List[1][ex]); else if (ex == 0) printf("%d", List[1][ex]); else printf("%dX^%d", List[1][ex], ex); } } printf("\n"); //second list for (ex = N - 1; ex >= 0; ex--)
{
if (ex == 0 && List[0][ex] == 0)
printf("%d", List[0][ex]);
if (List[0][ex] == 0)
continue;
if (List[0][ex] == 1)
{
if (ex == 1)
printf("X");
else if (ex == 0)
printf("1");
else
printf("X^%d", ex);
break;
}
if (List[0][ex] == -1)
{
if (ex == 1)
printf("-X");
else if (ex == 0)
printf("-1");
else
printf("-X^%d", ex);
break;
}
if (List[0][ex] > 1)
{
if (ex == 1)
printf("%dX", List[0][ex]);
else if (ex == 0)
printf("%d", List[0][ex]);
else
printf("%dX^%d", List[0][ex], ex);
break;
}
if (List[0][ex] < -1) { if (ex == 1) printf("%dX", List[0][ex]); else if (ex == 0) printf("%d", List[0][ex]); else printf("%dX^%d", List[0][ex], ex); break; } } for (ex--; ex >= 0; ex--)
{
if (List[0][ex] == 0)
continue;
if (List[0][ex] == 1)
{
if (ex == 1)
printf("+X");
else if (ex == 0)
printf("+1");
else
printf("+X^%d", ex);
}
if (List[0][ex] == -1)
{
if (ex == 1)
printf("-X");
else if (ex == 0)
printf("-1");
else
printf("-X^%d", ex);
}
if (List[0][ex] > 1)
{
if (ex == 1)
printf("+%dX", List[0][ex]);
else if (ex == 0)
printf("+%d", List[0][ex]);
else
printf("+%dX^%d", List[0][ex], ex);
}
if (List[0][ex] < -1)
{
if (ex == 1)
printf("%dX", List[0][ex]);
else if (ex == 0)
printf("%d", List[0][ex]);
else
printf("%dX^%d", List[0][ex], ex);
}
}
printf("\n");
//Added list
for (ex = 0; ex < N; ex++) List[1][ex] += List[0][ex]; //print added list for (ex = N - 1; ex >= 0; ex--)
{
if (ex == 0 && List[1][ex] == 0)
printf("%d", List[1][ex]);
if (List[1][ex] == 0)
continue;
if (List[1][ex] == 1)
{
if (ex == 1)
printf("X");
else if (ex == 0)
printf("1");
else
printf("X^%d", ex);
break;
}
if (List[1][ex] == -1)
{
if (ex == 1)
printf("-X");
else if (ex == 0)
printf("-1");
else
printf("-X^%d", ex);
break;
}
if (List[1][ex] > 1)
{
if (ex == 1)
printf("%dX", List[1][ex]);
else if (ex == 0)
printf("%d", List[1][ex]);
else
printf("%dX^%d", List[1][ex], ex);
break;
}
if (List[1][ex] < -1) { if (ex == 1) printf("%dX", List[1][ex]); else if (ex == 0) printf("%d", List[1][ex]); else printf("%dX^%d", List[1][ex], ex); break; } } for (ex--; ex >= 0; ex--)
{
if (List[1][ex] == 0)
continue;
if (List[1][ex] == 1)
{
if (ex == 1)
printf("+X");
else if (ex == 0)
printf("+1");
else
printf("+X^%d", ex);
}
if (List[1][ex] == -1)
{
if (ex == 1)
printf("-X");
else if (ex == 0)
printf("-1");
else
printf("-X^%d", ex);
}
if (List[1][ex] > 1)
{
if (ex == 1)
printf("+%dX", List[1][ex]);
else if (ex == 0)
printf("+%d", List[1][ex]);
else
printf("+%dX^%d", List[1][ex], ex);
}
if (List[1][ex] < -1)
{
if (ex == 1)
printf("%dX", List[1][ex]);
else if (ex == 0)
printf("%d", List[1][ex]);
else
printf("%dX^%d", List[1][ex], ex);
}
}
printf("\n");
return 0;
}
}