Skip to content

Commit 88b6c55

Browse files
committed
20190328
1 parent 303165d commit 88b6c55

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

多项式输出-简单模拟.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// 仔细逻辑的把问题想全面
2+
#include <iostream>
3+
using namespace std;
4+
const int maxn = 105;
5+
int a[maxn];
6+
int main()
7+
{
8+
int n;
9+
cin >> n;
10+
for (int i = 0; i < n + 1; i++)
11+
{
12+
cin >> a[i];
13+
}
14+
15+
for (int j = n; j > 0; j--)
16+
{
17+
if (j == n)
18+
{
19+
if (a[n - j] == 0)
20+
{
21+
continue;
22+
}
23+
if (a[n - j] == 1)
24+
{
25+
cout << "x^" << j;
26+
}
27+
else if (a[n - j] == -1)
28+
{
29+
cout << "-"
30+
<< "x^" << j;
31+
}
32+
else
33+
cout << a[n - j] << "x^" << j;
34+
}
35+
else if (j == 1)
36+
{
37+
if (a[n - j] == 1)
38+
{
39+
cout << "+" << "x";
40+
}
41+
else if (a[n - j] == -1)
42+
{
43+
cout << "-" << "x";
44+
}
45+
46+
else if (a[n - j] == 0)
47+
{
48+
continue;
49+
}
50+
51+
else if (a[n - j] > 0)
52+
{
53+
cout << "+" << a[n - j] << "x";
54+
}
55+
else
56+
{
57+
cout << a[n - j] << "x";
58+
}
59+
}
60+
61+
else if (a[n - j] == 1)
62+
{
63+
cout << "+"
64+
<< "x^" << j;
65+
}
66+
else if (a[n - j] == -1)
67+
{
68+
cout << "-"
69+
<< "x^" << j;
70+
}
71+
72+
else if (a[n - j] == 0)
73+
{
74+
continue;
75+
}
76+
77+
else if (a[n - j] > 0)
78+
{
79+
cout << "+" << a[n - j] << "x^" << j;
80+
}
81+
else
82+
{
83+
cout << a[n - j] << "x^" << j;
84+
}
85+
}
86+
// 最后常数项所有情况也要考虑好
87+
if (a[n] != 0)
88+
{
89+
90+
if (a[n] > 0)
91+
{
92+
cout << "+" << a[n];
93+
}
94+
else
95+
{
96+
cout << a[n];
97+
}
98+
}
99+
system("pause");
100+
return 0;
101+
}

0 commit comments

Comments
 (0)