// [4/14/2014 Sjm]
/*
考虑好边界处理,即可 AC 。
(
虽然题目所给数据值很大,但无需优化,亦可水过。。。
下面附 Java 的 AC 代码,第一次用 Java 交代码,
很久不用 Java,照着文档,调试很长时间。。。。。
不得不感叹 Java 对于大数处理的便捷与高效,自己写的C++代码效率太低了。
)
*/
Accepted | 1316 | 890MS | 276K | 1742
B | C++ |
//(1) C++ 代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
string str_1, str_2;
bool Cmp(string str1, string str2)
{
int len1 = str1.size(), len2 = str2.size();
if (len1 > len2) return true;
if (len1 < len2) return false;
for (int i = 0; i < len1; i++) {
if (str1[i] == str2[i]) continue;
if ((str1[i] - '0') >(str2[i] - '0')) return true;
else return false;
}
return true;
}
bool myJudge(string str)
{
if (Cmp(str, str_1) && Cmp(str_2, str)) return true;
else return false;
}
string myAdd(string str1, string str2)
{
string str = "";
int temp = 0, len1 = str1.size() - 1, len2 = str2.size() - 1;
while (len1 != -1 && len2 != -1) {
temp = (str1[len1--] - '0') + (str2[len2--] - '0') + temp;
str = char('0' + temp % 10) + str;
temp /= 10;
}
while (len1 != -1) {
temp = (str1[len1--] - '0') + temp;
str = char('0' + temp % 10) + str;
temp /= 10;
}
while (len2 != -1) {
temp = (str2[len2--] - '0') + temp;
str = char('0' + temp % 10) + str;
temp /= 10;
}
if (temp) str = char(temp + '0') + str;
return str;
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
while (cin >> str_1 >> str_2 && (str_1 != "0" || str_2 != "0"))
{
int ans = 0;
string str, str1 = "1", str2 = "2";
if (!Cmp(str_2, str1)) {
printf("%d\n", ans);
continue;
}
if (myJudge(str1)) ans++;
if (!Cmp(str_2, str2)) {
printf("%d\n", ans);
continue;
}
if (myJudge(str2)) ans++;
str = myAdd(str1, str2);
while (Cmp(str_2, str)){
if (myJudge(str))
ans++;
str1 = str2;
str2 = str;
str = myAdd(str1, str2);
}
printf("%d\n", ans);
}
return 0;
}
Accepted | 1316 | 203MS | 5680K | 884
B | Java |
// Java 代码
import java.util.*;
import java.math.*;
import java.io.*;
public class Main {
public static void main(String[] agrs) {
BigInteger[]fib = new BigInteger[500];
fib[1] = new BigInteger("1");
fib[2] = new BigInteger("2");
for (int i = 3; i<500; i++) {
fib[i] = fib[i - 2].add(fib[i - 1]);
// 用于判断 10^100 边界值
//String str = fib[i].toString();
//if (100 < str.length()) {
//System.out.println(i);
//}
}
Scanner input = new Scanner(new BufferedInputStream(System.in));
BigInteger mydata1, mydata2;
while (input.hasNextBigInteger()){
mydata1 = input.nextBigInteger();
mydata2 = input.nextBigInteger();
if (mydata1.compareTo(BigInteger.valueOf(0)) == 0 && mydata2.compareTo(BigInteger.valueOf(0)) == 0) {
break;
}
int pos = 1, ans = 0;
while (fib[pos].compareTo(mydata2) != 1) {
if ((fib[pos].compareTo(mydata1) != -1) && (mydata2.compareTo(fib[pos]) != -1)) {
ans++;
}
pos++;
}
System.out.println(ans);
}
}
}