唯一的坑可能就是0多项式的时候的输出格式吧,是输出2个0,一开始没注意就写了一个0,死活没通过。
#include <iostream> #include <string> #include <cstring> using namespace std; int a1[10000] = {0}; int a2[10000] = {0}; int a[100]; int k = 0; int u = 0; int b = 0; int last; int flag = 1;//是不是零多项式 int main() { char ch; while(b<5000) { b++; cin >> a1[k++] >> a2[u++]; } k--;u--; for(int i =0; i<=k; i++) { if(a2[i] == 0) { a1[i] = 0; } else { a1[i] = a1[i] * a2[i]; a2[i]--; if(a1[i] != 0) flag = 0; if(a2[i] == 0) last = i; } } if(flag == 1) cout << "0 0";//注意注意,这里有坑!!! else for(int i = 0; i<=last; i++) { if(a1[i]!=0) { if(i == last) cout << a1[i] << " " <<a2[i]; else cout << a1[i] << " " << a2[i] << " "; } else continue; } return 0; }