FZU - 2115 多项式积分

来源:互联网 发布:c语言编译过程 编辑:程序博客网 时间:2024/05/17 23:32

题目要求降幂输出。。。

#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;}


原创粉丝点击