多项式积分。
注意化简和符号即可。
降幂!!!
View Code
1 /* 2 多项式积分 3 */ 4 #include<stdio.h> 5 #include<string.h> 6 #include<stdlib.h> 7 #include<algorithm> 8 #include<iostream> 9 #include<queue> 10 #include<vector> 11 #include<map> 12 #include<math.h> 13 typedef long long ll; 14 //typedef __int64 int64; 15 const int maxn = 10; 16 const int maxm = 1005; 17 const int inf = 0x7FFFFFFF; 18 const double pi = acos(-1.0); 19 const double eps = 1e-8; 20 using namespace std; 21 22 struct node{ 23 int x,y,num; 24 double t; 25 }a[ maxn ],b[ maxn ]; 26 27 int gcd( int a,int b ){ 28 int r; 29 while( b!=0 ){ 30 r = a%b; 31 a = b; 32 b= r ; 33 } 34 return a; 35 } 36 37 int cmp( node a,node b ){ 38 return a.t>b.t; 39 } 40 41 int main(){ 42 int ca; 43 scanf("%d",&ca); 44 while( ca-- ){ 45 int n; 46 scanf("%d",&n); 47 for( int i=1;i<=n;i++ ){ 48 scanf("%d",&a[ i ].x); 49 a[ i ].y = 1; 50 a[ i ].num = i; 51 } 52 for( int j=1;j<=n;j++ ){ 53 scanf("%d",&b[ j ].x); 54 b[ j ].y = 1; 55 b[ j ].num = j; 56 } 57 for( int i=1;i<=n;i++ ){ 58 a[ i ].x = a[ i ].x*b[ i ].y; 59 a[ i ].y = a[ i ].y*( b[ i ].x+b[ i ].y ); 60 b[ i ].x = b[ i ].x+b[ i ].y; 61 62 int tgcd = gcd( abs(a[i].x),abs(a[i].y) ); 63 a[ i ].x /= tgcd,a[ i ].y /= tgcd; 64 tgcd = gcd( abs(b[i].x),abs(b[i].y) ); 65 b[ i ].x /= tgcd,b[ i ].y /= tgcd; 66 67 b[ i ].t = 1.0*b[ i ].x/(b[ i ].y*1.0); 68 //printf("%d/%d ",a[i].x,a[i].y); 69 //printf("%d/%d ",b[i].x,b[i].y); 70 } 71 //printf("gcd(12,8)=%d\n",gcd(12,8)); 72 int flag = -1; 73 sort( b+1,b+1+n,cmp ); 74 for( int j=1;j<=n;j++ ){ 75 int i = b[ j ].num;//注意这里的映射关系。降幂输出!! 76 if( a[ i ].x*a[ i ].y!=0 && flag==-1 ){ 77 78 if( a[ i ].x*a[ i ].y>0 ) ; 79 else printf("-"); 80 81 flag++; 82 83 if( a[ i ].x==a[ i ].y||a[ i ].x==-a[ i ].y ) ; 84 else if( abs(a[i].x)%abs(a[i].y)==0 ) printf("%d",abs(a[ i ].x)/abs(a[ i ].y)); 85 else printf("%d/%d",abs(a[i].x),abs(a[i].y));//首项系数 86 87 if( b[ j ].x*b[ j ].y==0 ) continue; 88 if( b[ j ].x==b[ j ].y ) printf("x"); 89 else if( b[ j ].x==-b[ j ].y ) printf("x^-1"); 90 else if( abs(b[ j ].x)%abs(b[ j ].y)==0 ) printf("x^%d",b[ j ].x/b[ j ].y); 91 else { 92 if( b[ j ].x*b[ j ].y<0 ) printf("-"); 93 printf("x^%d/%d",abs(b[ j ].x),abs(b[ j ].y)); 94 }//首项指数 95 } 96 else if( a[ i ].x*a[ i ].y!=0 && flag!=-1 ){ 97 98 if( a[i].x*a[i].y>0 ) printf("+"); 99 else printf("-");//系数符号 100 101 if( a[ i ].x==a[ i ].y||a[ i ].x==-a[ i ].y ) ; 102 else if( abs(a[i].x)%abs(a[i].y)==0 ) printf("%d",abs(a[ i ].x)/abs(a[ i ].y)); 103 else printf("%d/%d",abs(a[i].x),abs(a[i].y));//系数 104 105 if( b[ j ].x*b[ j ].y==0 ) ; 106 if( b[ j ].x==b[ j ].y ) printf("x"); 107 else if( b[ j ].x==-b[ j ].y ) printf("x^-1"); 108 else if( abs(b[ j ].x)%abs(b[ j ].y)==0 ) printf("x^%d",b[ j ].x/b[ j ].y); 109 else { 110 if( b[ j ].x*b[ j ].y<0 ) printf("-"); 111 printf("x^%d/%d",abs(b[ j ].x),abs(b[ j ].y)); 112 }//指数 113 } 114 } 115 printf("\n"); 116 } 117 return 0; 118 }