HDU 1316 How Many Fibs?(大数+字符串数字比较)
发布时间:2020-12-14 02:41:06 所属栏目:大数据 来源:网络整理
导读:先用大数打表,, ##include "string"#include "iostream"#include "cstdio"#include "cmath"#include "algorithm"#define INF 0x3f3f3f3f#define INFL 0x3f3f3f3f3f3f3f3fLLusing namespace std;typedef long long LL;const int M=55;string add(string x,s
先用大数打表,, ##include "string" #include "iostream" #include "cstdio" #include "cmath" #include "algorithm" #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL using namespace std; typedef long long LL; const int M=55; string add(string x,string y) { string ans ; int lenx = x.length(); int leny = y.length(); if(lenx<leny) { for(int i = 1;i<=leny-lenx;i++) x = "0"+x; } else { for(int i = 1;i<=lenx-leny;i++) y = "0"+y; } lenx = x.length(); int cf = 0; int temp; for(int i = lenx-1;i>=0;i--) { temp = x[i] - '0' + y[i] - '0'+cf; cf = temp/10; temp%=10; ans = char('0'+temp)+ans; } if(cf!=0) ans = char(cf+'0')+ans; return ans; } int compare(string x,string y)// 字符串形式的数的比较大小 { int i,lenx = x.length(),leny = y.length(),leaf; if(x==y) return 0; if(x.length()>y.length()) return 1; if(x.length()<y.length()) return -1; if(x.length()==y.length()) { for(i = 0;i<lenx;i++) { if(x[i]==y[i]) continue; if(x[i]>y[i]) return 1; else return -1; } return 0; } return leaf; } int main() { int i,j,k,start,eend; string x,y,num[1005];; num[0] = "0"; num[1] = "1"; num[2] = "2"; for(int i = 3;i<=1000;i++) num[i] = add(num[i-1],num[i-2]); while(cin>>x>>y&&x+y="0") { if(y == "0") { printf("0"); continue; } start = eend = 0; for(i = 1;i<1000;i++) { if(compare(x,num[i])==0) { start = i; break; } else if(compare(num[i],x)==-1&&compare(num[i+1],x)==1) { start = i+1; break; } } for(i = 1;i<1000;i++) { if(compare(y,num[i])==0){ eend = i;break; } else if(compare(num[i],y)==-1&&compare(num[i+1],y)==1){ eend = i;break; } } if(x=="0") // 注意 x == 0 时的情况 start = 1; cout<<eend-start+1<<endl; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |