UVa 392 - Polynomial Showdown

Contents

  1. 1. Problem
  2. 2. Solution
  3. 3. Code

Problem

中文網址

Solution

注意第一個為負的和只有常數項的情況。

Code

UVa 392
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<cstdio>

int main()
{
int coe;
while (scanf("%d", &coe) != EOF)
{
bool flag = false;
if (coe)
{
flag = true;
if (coe > 1 || coe < -1)
printf("%d", coe);
if (coe == -1)
putchar('-');

printf("x^8");
}

for (int i = 7; i; i--)
{
scanf("%d", &coe);
if (coe)
{
if (coe && !flag)
{
flag = true;
if (coe > 1 || coe < -1)
printf("%d", coe);
if (coe == -1)
putchar('-');

if (i == 1)
putchar('x');
else
printf("x^%d", i);
}
else
{
if (coe)
{
if (coe < 0)
printf(" - ");
else
printf(" + ");

if (coe > 1 || coe < -1)
printf("%d", coe < 0 ? -coe : coe);
if (i == 1)
putchar('x');
else
printf("x^%d", i);
}
}
}
}

scanf("%d", &coe);
if (!flag)
printf("%d", coe);
else if (coe < 0)
printf(" - %d", -coe);
else if (coe)
printf(" + %d", coe);

putchar('\n');
}

return 0;
}