uva 10106 Product
还是数组的使用问题,比424稍微多了些算法,不过经过一些优化,还是很容易就能写出简单易懂的代码的
//如果大家做过150! = ? 的话,下面的代码很容易就能搞明白了,SO easy!! #include<stdio.h> #include<string.h> void voluation(char st[],int len,int num[]) { int i; for(i = 0;i < len; i++) num[i] = st[len-i-1] - \'0\'; } int main() { char st1[1000],st2[1000]; while(gets(st1) != NULL) { gets(st2); int num[1000] = {0}, top = 0, root = 0, i, len1, j, len2, k, _num[1000] = {0}; len1 = strlen(st1); len2 = strlen(st2); if(st1[0] == \'0\' || st2[0] == \'0\') { printf("0\n"); continue; } //这里加上这个判断不想后面计算时循环次数太多而占用太多的时间,算是一些优化吧 if(len1 > len2) { top = len1; voluation(st1,len1,num); strcpy(st1,st2); } else { top = len2; voluation(st2,len2,num); } k = strlen(st1); for(i = 0;i < k; i++) { for(j = 0;j < top; j++) { _num[j+i] += num[j]*(st1[k-1-i]-\'0\'); } } top += k; for(i = 0;i < top; i++) { _num[i+1] += _num[i]/10; _num[i] %= 10; } while(_num[top] == 0) top--; for(i = top;i > -1; i--) printf("%d",_num[i]); printf("\n"); } return 0; }