您的位置:首页 > 其它

UVA392 模拟(多项式输出),水

2016-11-09 20:45 344 查看
1 题意,输入一个多项式,输出它。

2 分析。

注意模拟题思路和代码都要按部就班、步骤条理清晰,一个姿势过不了,如果觉得有更清晰的思路,就不要纠结过不了代码,而是换个姿势重新来一发。多想想题目边界、自己代码分类的边界、以及0和1等特殊情况。

3

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;

const int maxn=10;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int f[maxn];
memset(f,0,sizeof(f));
while(~scanf("%d",&f[8])){
///ini
for(int i=7;i>=0;i--){
scanf("%d",&f[i]);
}

///print
int st=0;
for(int j=8;j>=0;j--){
if(f[j]!=0){
if(st==0){
st=1;
if(j!=0){
///.base_number:(底数)
if(f[j]==1) cout<<"";
else if(f[j]==-1) cout<<"-";
else cout<<f[j];

///.index:(指数)
if(j==1) cout<<"x";
else cout<<"x^"<<j;
}
else{
cout<<f[j];
}
}
else if(st==1){
if(j!=0){
///.base_number:
if(f[j]==1) cout<<" + ";
else if(f[j]==-1) cout<<" - ";
else if(f[j]>1) cout<<" + "<<f[j];
else if(f[j]<-1) cout<<" - "<<-1*f[j];

///.index:
if(j==1) cout<<"x";
else cout<<"x^"<<j;
}
else{
if(f[j]>0) cout<<" + "<<f[j];
else if(f[j]<0) cout<<" - "<<-1*f[j];
}
}
}
}
//cout<<endl; presentation error

if(st==0) cout<<"0"<<endl;
else cout<<endl;

memset(f,0,sizeof(f));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: