BigNums 之 hdu 1316

//  [4/14/2014 Sjm]
/*
考虑好边界处理,即可 AC 。
(
 虽然题目所给数据值很大,但无需优化,亦可水过。。。
 下面附 Java 的 AC 代码,第一次用 Java 交代码,
 很久不用 Java,照着文档,调试很长时间。。。。。
 不得不感叹 Java 对于大数处理的便捷与高效,自己写的C++代码效率太低了。

*/

 
 
Accepted1316890MS276K1742 BC++
//(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;
}
Accepted1316203MS5680K884 BJava
// 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);
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值