[大数] HDU 1250 - Hat's Fibonacci
2014-11-16 16:10
411 查看
直接按题意,用数组模拟大数相加即可。。注意进位情况。
一开始用int二维数组,MLE了。。然后改成char数组过了。。
后来想了一下,用int数组也可做,把int每位存储由10进制编程100000进制。然后就可以把数组大小减小五倍。
做法一:
做法二:
一开始用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; }
相关文章推荐
- hdu 1250 Hat's Fibonacci(java大数)
- HDU 1250 Hat's Fibonacci(Java大数相加)+讲解
- HDU 1250 - Hat's Fibonacci(大数斐波那契)
- hdu-1250-Hat's Fibonacci(大数问题)
- HDOJ/HDU 1250 Hat's Fibonacci(大数~斐波拉契)
- HDU 1250-Hat's Fibonacci(大数)
- HDU 1250 Hat's Fibonacci(大数相加)
- HDU 1250 Hat's Fibonacci (JAVA大数)
- HDU 1250 Hat's Fibonacci (Java大数,类似Fib数,有个坑!!)
- HDU1250~Hat's Fibonacci(大数加法)
- hdu 1250 Hat's Fibonacci (大数相加,水题)
- hdu 1250 Hat's Fibonacci (Java大数)
- HDU 1250 Hat's Fibonacci 大数加法 进位10000000
- hdu 1250 Hat's Fibonacci (大数相加)
- hdu 1250 Hat's Fibonacci(大数加法)
- HDOJ 1250 Hat's Fibonacci (大数的加法)
- 杭电1250 Hat's Fibonacci(大数+1000进制输出)
- HDU 1250 Hat's Fibonacci JAVA大数
- 【HDU】1250 - Hat's Fibonacci(java - BigDecimal)
- HDU 1250 Hat's Fibonacci