标签:memset include 斐波那契 大数 put eof sizeof names for
#include<bits/stdc++.h> using namespace std; struct bign{ int d[10000]; int len; bign(){ memset(d,0,sizeof(d)); len = 0; } }; //字符串转到大数中 bign change(char *s){ bign a; a.len = strlen(s); for(int i=0; i<a.len; i++){ a.d[i] = s[a.len - 1 - i] - ‘0‘; } return a; } //两个大数相加 bign add(bign v1, bign v2){ bign c; int carry = 0;//进位 for(int i=0; i<v1.len || i<v2.len; i++){ int temp = v1.d[i] + v2.d[i] + carry; c.d[c.len++] = temp % 10; carry = temp / 10; } if(carry != 0){ c.d[c.len++] = carry; } return c; } int main(){ int n; scanf("%d",&n); if(n == 0){ puts("0"); return 0; } if(n == 1){ puts("1"); return 0; } if(n == 2){ puts("2"); return 0; } char s1[10000]; char s2[10000]; char s3[10000]; strcpy(s1,"1"); strcpy(s2,"2"); int cnt; for(int i=3; i<=n; i++){ bign v1 = change(s1); bign v2 = change(s2); bign v3 = add(v1,v2); for(int i=v3.len - 1,cnt = 0; i>=0; i--,cnt++){ s3[cnt] = v3.d[i] + ‘0‘; } strcpy(s1,s2); strcpy(s2,s3); } puts(s2); return 0; }
标签:memset include 斐波那契 大数 put eof sizeof names for
原文地址:https://www.cnblogs.com/zhangqiling/p/12309755.html