{长郡内考}·11.5-数字乘积

来源:互联网 发布:高收益网络理财平台 编辑:程序博客网 时间:2024/05/20 10:20
数字乘积
申明:鄙人排版很辛苦,给点鼓励...
题目见这个帖子中的“循环数组字符窜模拟(2)”
看完题目,我首先想到的是unsigned long long,然后我看到数据范围--n<2000,很好,一个高精度
再然后我就想到了高精度加法(我只做了这个关于高精度的题)
没办法,硬着头皮试一试吧:
#include<cstdio>#include<iostream>#include<cstring>using namespace std;int n,a[668],k,sum,x,y,z,o,len;char str[1000];void gjd(){for(int i = 0;i < y;i++){for(int j = 0;j < strlen(str);j++){z = str[j] - 48;z = z * a[i] + x;x = z / 10;z %= 10;str[j]=z+48;if(x&&(str[j+1]<'0'||str[j+1]>'9'))str[j+1] = '0'; }}}int main(){freopen("number.in","r",stdin);freopen("number.out","w",stdout);cin >> n;if(n==2){cout << "1\n1";return 0;}if(n==3){cout << "2\n1";return 0;}if((n - 2) % 3 == 0)a[0] = 2,k = 1,n -= 2,o = 1;else if((n - 4) % 3 == 0)a[0] = 2,a[1] = 2,k = 2,n -= 4,o = 2;sum = n/3;y = sum + o;for(;k < y;k++)a[k] = 3;str[0]='1';gjd();for(int i = strlen(str)-1;i >= 0;i--)cout << str[i];cout << endl << strlen(str);return 0;}
AC满分通过,经过测试,N=2005时仍然不会超时
注解,未来回来会加上


1 0