-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1555.cc
49 lines (43 loc) · 1.12 KB
/
1555.cc
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
//Name: Polynomial Showdown
//Level: 2
//Category: 実装
//Note:
/*
* パターンは問題文で全て説明されているので,その通りに条件を分けていけばよい.
* 0を出力するパターンに注意.
*/
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
int main() {
string line;
while(getline(cin, line)) {
vector<int> coefs;
istringstream is(line);
while(!is.eof()) {
int c;
is >> c;
coefs.push_back(c);
}
int N = coefs.size();
bool first = true;
for(int i = 0; i < N; ++i) {
if(coefs[i] == 0) continue;
if(coefs[i] < 0) {
if(first) cout << "-";
else cout << " - ";
}
else if(!first) cout << " + ";
if(i == N-1 || abs(coefs[i]) != 1) cout << abs(coefs[i]);
if(i != N-1) cout << "x";
if(i < N-2) cout << "^" << (N-i-1);
first = false;
}
if(first) cout << "0";
cout << endl;
}
return 0;
}