您的位置:首页 > 其它

FZU - 2115 多项式积分

2013-10-08 19:28 375 查看
题目要求降幂输出。。。

#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
#include <cstdlib>
#include <cstdio>

using namespace std;
/*  define */
#define sf(a) scanf("%d",&a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
/*  define */
int gcd(int a,int b){
if(a<0)
a*=-1;
if(b<0)
b*=-1;
return b==0?a:gcd(b,a%b);
}
int a[100],b[100];
int main(){
int t;
sf(t);
while(t--){
int n;
sf(n);
rep(i,1,n)
sf(a[i]);
rep(i,1,n)
sf(b[i]);
rep(i,1,n)
rep(j,i+1,n){
if(b[i]<b[j]){
swap(b[i],b[j]);
swap(a[i],a[j]);
}
}
bool first=0;
rep(i,1,n){
if(a[i]==0) continue;
if(b[i]==0){
if(a[i]>0 && a[i]!=1){
if(!first)
printf("%dx",a[i]);
else
printf("+%dx",a[i]);
}
else if(a[i]<0 && a[i]!=-1)
printf("%dx",a[i]);
else if(a[i]==1){
if(!first)
printf("x");
else
printf("+x");
}
else if(a[i]==-1)
printf("-x");
first=1;
continue;
}
int f1,f2;
f1=a[i]/gcd(a[i],b[i]+1);
f2=(b[i]+1)/gcd(a[i],b[i]+1);

if(a[i]<0){
f1=-abs(f1);
f2=abs(f2);
}
else {
f1=abs(f1);
f2=abs(f2);
}
//
if(f2==1){
if(a[i]>0&&f1!=1){
if(!first)
printf("%dx^%d",f1,b[i]+1);
else
printf("+%dx^%d",f1,b[i]+1);
}
else if(a[i]<0&&f1!=-1)
printf("%dx^%d",f1,b[i]+1);
else if(f1==1){
if(!first)
printf("x^%d",b[i]+1);
else
printf("+x^%d",b[i]+1);
}
else if(f1==-1){
printf("-x^%d",b[i]+1);
}
first=1;
}
else {
if(a[i]>0){
if(!first)
printf("%d/%dx^%d",f1,f2,b[i]+1);
else
printf("+%d/%dx^%d",f1,f2,b[i]+1);
}
else if(a[i]<0)
printf("%d/%dx^%d",f1,f2,b[i]+1);
first=1;
}
}
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  FZU 模拟