解题思路:
我也醉了 这题的答案。。例如n=4的时候输入0 1 0 0 0 输出的是 +x^4,居然过了,还有就是全输入0的时候是没有输出的。不要纠结为啥不是+x^3,请问 1 前面所有的不符合的0 算多项式么?
第一个题解是不判断前面的0的,也就是会出现0 1 0 0 0,+x这样;
第二个题解是判断前面的0的,也就是输入0 1 0 0 0,输出为 x^4;
思路:
我是按照自己的思路写的
三个重要的判断(前,中,后)
首先,最前面的那一位,如果是 1 ,就输出 1;如果是 -1,就输出 -1;不是 -1 ,原样输出;
中间,如果是 ±1 ,只需输出符号;不是 ±1,原样输出;
末尾,只需判断正负加符号即可!
注意事项:
参考代码:
第一个题解:
#include<bits/stdc++.h> using namespace std; typedef long long ll; int d[1005],a[1005]; int main() { int n, d=0, i, j=0, k; cin >> n; for(i=0;i<n+1;i++) cin>>a[i]; k = n ; for(i = 0; i < n+1; i++) { if(a[i]==0) { k--;d++;continue; } // 0 跳过,k-1 if(i == 0 && a[i] == -1 ) //特殊 -1 { if(k!=1) { cout << "-x^" << k--;continue; } if(k==1) { cout << "-x" ;continue; } } if(i == 0 && a[i] == 1 ) //特殊 1 { if(k!=1) { cout << "x^" << k--;continue; } if(k==1) { cout << "x" ;continue; } } if(i == 0) //首位不是 1 的数 { if(k!=1) { cout << a[0] << "x^" << k--;continue;} if(k==1) { cout << a[0] << "x" ;continue;} } //中间的数 if(i > 0 && i < n && a[i] == 1) { if(k!=1) { cout << "+x^" << k--;continue; } if(k==1) { cout << "+x" ;continue; } } if(i > 0 && i < n && a[i] == -1) { if(k!=1) { cout << "-x^" << k--;continue; } if(k==1) { cout << "-x" ;continue; } } if(i > 0 && i < n ) { if(a[i] < -1) { if(k!=1) { cout << a[i] << "x^" << k--;continue; } if(k==1) { cout << a[i] << "x" ;continue; } } if(a[i] > 1) { if(k!=1) { cout << "+" << a[i] << "x^" << k--;continue; } if(k==1) { cout << "+" << a[i] << "x" ;continue; } } } //最后的数字 if(i == n && a[i] < 0) { cout << a[i] << endl; continue; } if(i == n && a[i] > 0) { cout << "+" << a[i] << endl; continue; } } return 0; }
第二个题解:
#include<bits/stdc++.h> using namespace std; typedef long long ll; int d[1005],a[1005]; int main() { int n, d=0, i, j=0, k; cin >> n; for(i=0;i<n+1;i++) cin>>a[i]; k = n ; for(j=0;j<n+1;j++) if(a[j]) break; for(i = j; i < n+1; i++) { if(a[i]==0) { k--;continue; } // 0 跳过,k-1 if(i == j && a[i] == -1 ) //特殊 -1 { if(k!=1) { cout << "-x^" << k--;continue; } if(k==1) { cout << "-x" ;continue; } } if(i == j && a[i] == 1 ) //特殊 1 { if(k!=1) { cout << "x^" << k--;continue; } if(k==1) { cout << "x" ;continue; } } if(i == j) //首位不是 1 的数 { if(k!=1) { cout << a[i] << "x^" << k--;continue;} if(k==1) { cout << a[i] << "x" ;continue;} } //中间的数 if(i > j && i < n && a[i] == 1) { if(k!=1) { cout << "+x^" << k--;continue; } if(k==1) { cout << "+x" ;continue; } } if(i > j && i < n && a[i] == -1) { if(k!=1) { cout << "-x^" << k--;continue; } if(k==1) { cout << "-x" ;continue; } } if(i > j && i < n ) { if(a[i] < -1) { if(k!=1) { cout << a[i] << "x^" << k--;continue; } if(k==1) { cout << a[i] << "x" ;continue; } } if(a[i] > 1) { if(k!=1) { cout << "+" << a[i] << "x^" << k--;continue; } if(k==1) { cout << "+" << a[i] << "x" ;continue; } } } //最后的数字 if(i == n && a[i] < 0) { cout << a[i] << endl; continue; } if(i == n && a[i] > 0) { cout << "+" << a[i] << endl; continue; } } return 0; }
0.0分
0 人评分
C二级辅导-公约公倍 (C语言代码)浏览:2123 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:580 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:644 |
P1000 (C语言代码)浏览:877 |
C语言程序设计教程(第三版)课后习题8.2 (C语言代码)浏览:1038 |
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:560 |
小O的数字 (C语言代码)浏览:1411 |
母牛的故事 (C语言代码)浏览:455 |
1063题 初学者,求帮忙看下,不知道哪错了浏览:232 |
C语言程序设计教程(第三版)课后习题8.2 (C++代码)浏览:661 |