您的位置:首页 > 其它

[大数] HDU 1250 - Hat's Fibonacci

2014-11-16 16:10 411 查看
直接按题意,用数组模拟大数相加即可。。注意进位情况。

一开始用int二维数组,MLE了。。然后改成char数组过了。。

后来想了一下,用int数组也可做,把int每位存储由10进制编程100000进制。然后就可以把数组大小减小五倍。

做法一:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <malloc.h>
#include <time.h>
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
int ch[7043][502];
int main()
{
//freopen("test0.in", "r", stdin);
//freopen("test01.out", "w", stdout);
//srand(time(NULL));
int N;
ch[0][500] = ch[1][500] = ch[2][500] = ch[3][500] = 1;
for(int i = 4; i <= 7042; i++)
{
for(int j = i-4; j < i; ++j)
{
for(int k = 500; k >= 0; k--)
{
ch[i][k] += ch[j][k];
if(ch[i][k] > 99999)
{
ch[i][k-1] += ch[i][k] / 100000;
ch[i][k] %= 100000;
}
}
}
}
while(~scanf("%d", &N))
{
int j;
for(j = 0; j <= 600; j++)
{
if(ch[N-1][j] != 0)
{
break;
}
}
printf("%d", ch[N-1][j++]);
while(j <= 500)
{
printf("%05d", ch[N-1][j++]);
}
puts("");
}
return 0;
}


做法二:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
char ch[7043][2009];
int main()
{
//freopen("test0.in", "r", stdin);
//freopen("test0.out", "w", stdout);
//srand(time(NULL));
int N;
ch[0][2008] = ch[1][2008] = ch[2][2008] = ch[3][2008] = 1;
for(int i = 4; i <= 7042; i++)
{
for(int j = i-4; j < i; ++j)
{
for(int k = 2008; k >= 0; k--)
{
ch[i][k] += ch[j][k];
if(ch[i][k] > 9)
{
ch[i][k-1] += ch[i][k] / 10;
ch[i][k] %= 10;
}

}
}
}
while(~scanf("%d", &N))
{
int j;
for(j = 0; j <= 2008; j++)
{
if(ch[N-1][j] != 0)
{
break;
}
}
while(j <= 2008)
{
printf("%d", (int)ch[N-1][j++]);
}
puts("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm 大数