liyunbo @ 2024-08-14 22:35:38
一半绿一半红
#include<bits/stdc++.h>
using namespace std;
int n;
int a[1000005];
void input()
{
cin>>n;
for(int i=1;i<=n+1;i++)
{
cin>>a[i];
}
}
void output()
{
int x=n;
for(int i=1;i<=n;i++)
{
if(a[i]!=0)
{
if(a[i]<0)
{
if(abs(a[i])==1)
{
if(i==1)
{
cout<<"x^"<<x;
}
else
{
cout<<"-"<<"x^"<<x;
}
}
else
{
cout<<a[i]<<"x^"<<x;
}
}
else
{
if(abs(a[i])==1)
{
if(i==1)
{
cout<<"x^"<<x;
}
else
{
cout<<"+"<<"x^"<<x;
}
}
else
{
if(i==1)
{
cout<<abs(a[1])<<"x^"<<x;
}
else
{
cout<<"+"<<abs(a[i])<<"x^"<<x;
}
}
}
}
x--;
}
if(a[n+1]>0)
{
cout<<"+"<<a[n+1];
}
else if(a[n+1]<0)
{
cout<<a[n+1];
}
}
int main()
{
input();
output();
return 0;
}
by wuziling @ 2024-08-20 16:05:58
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string f(int d,const vector<int>&c) {
string r;
for (int i=0;i<=d;++i) {
int a=c[i];
int p=d-i;
if (a!=0) {
if(!r.empty()){
r+=(a>0)?"+":"";
}
if (p==0) {
r+=to_string(a);
} else if (p == 1) {
r+=(abs(a) == 1) ? (a > 0 ? "x" : "-x") : to_string(a) + "x";
} else {
r+=(abs(a) == 1) ? (a > 0 ? "x^" + to_string(p) : "-x^" + to_string(p)) : to_string(a) + "x^" + to_string(p);
}
}
}
return r.empty()?"0":r;
}
int main() {
int n;
cin>>n;
vector<int>c(n + 1);
for(int i=0;i<=n;++i) cin>>c[i];
cout<<f(n,c)<<endl;
return 0;
}
//把我的代码给你康康