FOJ 1698 最大乘积

 

http://acm.fzu.edu.cn/problem.php?pid=1698

 

解题思路:高精度乘法

 

#include <stdio.h> #include <string.h> #define LL long struct Anode { int value[145]; int size; }a; struct Bnode { LL value[500]; int size; }b; int n; int i,j; void mul()//大数相乘 { int up; for (i=0;i<a.size;i++) { up = 0; for (j=0;j<b.size;j++) { b.value[j] = b.value[j]*a.value[i]+up; up = b.value[j]/1000000; b.value[j]%=1000000; } if (up>0) b.value[b.size++]+= up; } } void PrintResult()//输出乘积结果 { int d; for (i=b.size-1;i>=0;i--) { if(b.value[i]!=0) { for(j=100000;j>=10;) { d=b.value[i]/j; j/=10; if(d==0&&i<b.size-1) { printf("0"); } else { printf("%ld",b.value[i]); break; } } } else printf("000000"); } printf("/n"); } void Fenjie()//分解n { i=0; for (j=2;;j++) { if (n<=j) { a.value[i]=n; i++; a.size=i; break; } else { a.value[i] = j; i++; n-=j; } } if (a.value[a.size-1]<=a.value[a.size-2]) { i = a.size-2; while (a.value[a.size-1]--) { a.value[i]++; i--; if(i==-1) i = a.size-2; } a.size--; } } void PrintFenjie()//输出分解结果 { int i; for (i=0;i<a.size;i++) { printf("%d",a.value[i]); if(i!=a.size-1) printf(" "); } printf("/n"); } int main() { while (scanf("%d",&n)!=EOF) { memset(&a,0,sizeof(a)); memset(&b,0,sizeof(b)); b.value[0] = 1; b.size=1; if(n==3) printf("1 2/n2/n"); else if(n==4) printf("1 3/n3/n"); if(n>4) { Fenjie(); PrintFenjie(); mul(); PrintResult(); } } return 0; }

你可能感兴趣的:(FOJ 1698 最大乘积)