cclianlian @ 2024-06-11 17:04:40
using namespace std;
vector<string>vec;
string s;
int e;//e是指数
void solve() {
int n; cin >> n;
for (int i = 0; i <= n; i++) {
cin >> s;
if (s == "0") {
continue;
}
if (i!=n&&s == "-1") {//-1和1在中间的特殊处理,删去系数的1
s.erase(s.begin() + 1);
}
else if (i != n && s == "1") {
s.clear();
}
e = n - i;
if (s[0] != '-' && !vec.empty()) {//连接中间的+
s.insert(s.begin(), '+');
}
if (e == 1) {
s += "x";
}
if (e>1) {
s += "x^";
s.insert(s.end(), e + '0');
}
vec.push_back(s);
}
if (vec.empty()) {
cout << "0";//防止全为0
}
for (int i = 0; i < vec.size(); i++) {
cout << vec[i];
}
}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0); std::cout.tie(0);
solve();
return 0;
}
by Moya_Rao @ 2024-07-04 11:59:07
看看我的吧
#include<bits/stdc++.h>
using namespace std;
int n,k[105],v;
int main(){
cin>>n;
for(int i=n;i>=0;i--)cin>>k[i];
for(int i=n;i>=0;i--){
if(k[i]!=0){
if(i!=n&&k[i]>0&&v)cout<<"+";
if(!v)v=1;
if(abs(k[i])>1||i==0)cout<<k[i];
if(k[i]==-1&&i>0)cout<<"-";
if(i>1)cout<<"x^"<<i;
if(i==1)cout<<"x";
}
}
return 0;
}
(放心看罢,这份代码已经 AC 了)