一开始练感觉总是会有很多奇奇怪怪问题呢
不就一个简单的模拟, 为什么老会出问题
第一次提交(40%)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int main()
{
int n;
cin >> n;
int h = -1;
for (int i = n; 0 <= i; i--)
{
cin >> a[i];
if (a[i] == 0 && i > 0)
continue;
if (a[i] && h == -1)
h = i;
if (i == h)
{
if (a[i] == -1)
cout << '-';
else if (a[i] == 1)
;
else
cout << a[i];
}
else
{
if (a[i] > 0)
cout << '+';
else if (a[i] < 0)
cout << '-';
if (i == 0 || abs(a[i]) != 1)
cout << abs(a[i]);
}
if (i > 0)
cout << "x^" << i;
}
return 0;
}
第二次提交(ac)
感觉这样写, 代码是简洁点哈
#include<bits/stdc++.h>
using namespace std;
const int N =1e5+10;
const int inf = INT_MAX ;
typedef pair <int, int > pii;
typedef priority_queue<int ,vector<int> ,greater<int>> small_heap;
#define int long long
int a[200];
int n , omit;
int32_t main()
{
cin >> n ;
for(int i = 1 ; i<=n + 1 ; i++) cin >> a[i];
for(int i = 1 ; i<=n + 1 ; i++){
if(i == 1 && a[i] == 1)
omit = 1;
else if(i == n + 1 && a[i] == 1) printf("+%d" , a[i]);
else if(i != 1 && a[i] == 1) printf("+");
else if (i == n + 1 && a[i] == -1) printf("")
}
return 0;
}
加油, 再接再厉