BigNums 之 hdu 1316

//  [4/14/2014 Sjm]
/*
考虑好边界处理,即可 AC 。
(
 虽然题目所给数据值很大,但无需优化,亦可水过。。。
 下面附 Java 的 AC 代码,第一次用 Java 交代码,
 很久不用 Java,照着文档,调试很长时间。。。。。
 不得不感叹 Java 对于大数处理的便捷与高效,自己写的C++代码效率太低了。
)
*/
Accepted 1316 890MS 276K 1742 B C++
 1 //(1) C++ 代码
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <string>
 7 #include <cstring>
 8 #include <algorithm>
 9 using namespace std;
10 string str_1, str_2;
11 
12 bool Cmp(string str1, string str2)
13 {
14     int len1 = str1.size(), len2 = str2.size();
15     if (len1 > len2) return true;
16     if (len1 < len2) return false;
17     for (int i = 0; i < len1; i++) {
18         if (str1[i] == str2[i]) continue;
19         if ((str1[i] - '0') >(str2[i] - '0')) return true;
20         else return false;
21     }
22     return true;
23 }
24 
25 bool myJudge(string str)
26 {
27     if (Cmp(str, str_1) && Cmp(str_2, str)) return true;
28     else return false;
29 }
30 
31 string myAdd(string str1, string str2)
32 {
33     string str = "";
34     int temp = 0, len1 = str1.size() - 1, len2 = str2.size() - 1;
35     while (len1 != -1 && len2 != -1) {
36         temp = (str1[len1--] - '0') + (str2[len2--] - '0') + temp;
37         str = char('0' + temp % 10) + str;
38         temp /= 10;
39     }
40     while (len1 != -1) {
41         temp = (str1[len1--] - '0') + temp;
42         str = char('0' + temp % 10) + str;
43         temp /= 10;
44     }
45     while (len2 != -1) {
46         temp = (str2[len2--] - '0') + temp;
47         str = char('0' + temp % 10) + str;
48         temp /= 10;
49     }
50     if (temp) str = char(temp + '0') + str;
51     return str;
52 }
53 
54 int main()
55 {
56     //freopen("input.txt", "r", stdin);
57     //freopen("output.txt", "w", stdout);
58     while (cin >> str_1 >> str_2 && (str_1 != "0" || str_2 != "0"))
59     { 
60         int ans = 0;
61         string str, str1 = "1", str2 = "2";
62         if (!Cmp(str_2, str1)) {
63             printf("%d\n", ans);
64             continue;
65         }
66         if (myJudge(str1)) ans++;
67         if (!Cmp(str_2, str2)) {
68             printf("%d\n", ans);
69             continue;
70         }
71         if (myJudge(str2)) ans++;
72         str = myAdd(str1, str2);
73         while (Cmp(str_2, str)){
74             if (myJudge(str))
75                 ans++;
76             str1 = str2;
77             str2 = str;
78             str = myAdd(str1, str2);
79         }
80         printf("%d\n", ans);
81     }
82     return 0;
83 }
Accepted 1316 203MS 5680K 884 B Java
 1 // Java 代码
 2 import java.util.*;
 3 import java.math.*;
 4 import java.io.*;
 5 
 6 public class Main {
 7     public static void main(String[] agrs) {
 8         BigInteger[]fib = new BigInteger[500];
 9         fib[1] = new BigInteger("1");
10         fib[2] = new BigInteger("2");
11         for (int i = 3; i<500; i++) {
12             fib[i] = fib[i - 2].add(fib[i - 1]);
13             
14             // 用于判断 10^100 边界值
15             //String str = fib[i].toString();
16             //if (100 < str.length()) {
17             //System.out.println(i);
18             //} 
19         }
20         Scanner input = new Scanner(new BufferedInputStream(System.in));
21         BigInteger mydata1, mydata2;
22         while (input.hasNextBigInteger()){
23             mydata1 = input.nextBigInteger();
24             mydata2 = input.nextBigInteger();
25             if (mydata1.compareTo(BigInteger.valueOf(0)) == 0 && mydata2.compareTo(BigInteger.valueOf(0)) == 0) {
26                 break;
27             }
28             int pos = 1, ans = 0;
29             while (fib[pos].compareTo(mydata2) != 1) {
30                 if ((fib[pos].compareTo(mydata1) != -1) && (mydata2.compareTo(fib[pos]) != -1)) {
31                     ans++;
32                 }
33                 pos++;
34             }
35             System.out.println(ans);
36         }
37     }
38 }
posted @ 2014-04-15 00:04  JmingS  阅读(124)  评论(0编辑  收藏  举报