一道有点复杂的模拟题
#include<cstdio> using namespace std; int a[102]; int main() { int n; scanf("%d", &n); for(int i = n; i >= 0; i--) scanf("%d", &a[i]); for(int i = n; i >= 0; i--) { if(!a[i]) continue; if(i > 1) { if(a[i] < 0 && a[i] != -1) printf("%dx^%d", a[i], i); else if(a[i] > 0 && a[i] != 1) { if(i != n) printf("+"); printf("%dx^%d", a[i], i); } else if(a[i] == 1) { if(i != n) printf("+"); printf("x^%d", i); } else if(a[i] == -1) printf("-x^%d", i); } else if(i == 1) { if(a[i] == 1 && i != n) printf("+x"); else if(a[i] == -1 || i == n) printf("-x"); else if((a[i] < 0 && a[i] != -1) || i == n) printf("%dx", a[i]); else if(a[i] > 0 && i != n && a[i] != 1) printf("+%dx", a[i]); } else if(!i) { if(a[i] < 0 || i == n) printf("%d", a[i]); else if(a[i] > 0 && i != n) printf("+%d", a[i]); else if(a[i] == 0 && n == 0) printf("0"); } } return 0; }
原文地址:https://www.cnblogs.com/njbw7782/p/10356217.html
时间: 2024-10-12 01:17:46