标签:
http://acm.hdu.edu.cn/showproblem.php?pid=1715
模板大数:
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <assert.h> 6 #include <ctype.h> 7 #include <map> 8 #include <string> 9 #include <set> 10 #include <bitset> 11 #include <utility> 12 #include <algorithm> 13 #include <vector> 14 #include <stack> 15 #include <queue> 16 #include <iostream> 17 #include <fstream> 18 #include <list> 19 using namespace std; 20 21 const int MAXL = 500; 22 struct BigNum 23 { 24 int num[MAXL]; 25 int len; 26 BigNum() 27 { 28 memset(num,0,sizeof(num)); 29 } 30 }; 31 32 //高精度加法 33 BigNum Add(BigNum &a, BigNum &b) 34 { 35 BigNum c; 36 int i, len; 37 len = (a.len > b.len) ? a.len : b.len; 38 memset(c.num, 0, sizeof(c.num)); 39 for(i = 0; i < len; i++) 40 { 41 c.num[i] += (a.num[i]+b.num[i]); 42 if(c.num[i] >= 10) 43 { 44 c.num[i+1]++; 45 c.num[i] -= 10; 46 } 47 } 48 if(c.num[len]) 49 len++; 50 c.len = len; 51 return c; 52 } 53 void print(BigNum &a) //输出大数 54 { 55 int i; 56 for(i = a.len-1; i >= 0; i--) 57 printf("%d", a.num[i]); 58 puts(""); 59 } 60 //将字符串转为大数存在BigNum结构体里面 61 BigNum ToNum(char *s) 62 { 63 int i, j; 64 BigNum a; 65 a.len = strlen(s); 66 for(i = 0, j = a.len-1; s[i] != ‘\0‘; i++, j--) 67 a.num[i] = s[j]-‘0‘; 68 return a; 69 } 70 71 void Init(BigNum &a, char *s, int &tag) //将字符串转化为大数 72 { 73 int i = 0, j = strlen(s); 74 if(s[0] == ‘-‘) 75 { 76 j--; 77 i++; 78 tag *= -1; 79 } 80 a.len = j; 81 for(; s[i] != ‘\0‘; i++, j--) 82 a.num[j-1] = s[i]-‘0‘; 83 } 84 BigNum p[1005]; 85 void deal() 86 { 87 int tag=1; 88 Init(p[1],"1",tag); 89 Init(p[2],"1",tag); 90 for(int i=3;i<=1000;i++) 91 { 92 p[i]=Add(p[i-1],p[i-2]); 93 } 94 } 95 int main(void) 96 { 97 //freopen("in.txt","r",stdin); 98 deal(); 99 int n; 100 scanf("%d",&n); 101 while(n--) 102 { 103 int t; 104 scanf("%d",&t); 105 print(p[t]); 106 } 107 return 0; 108 }
标签:
原文地址:http://www.cnblogs.com/xuesen1995/p/4555367.html