Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求一个1001的让下面的这个C++ code WA掉的数据...

Posted by X_ray at 2007-07-15 22:39:08
[code]#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

class BigFloat {
public:
	static const int max_bit = 250;
	int size;
	int fracLen;
	int exp;
	long long val;
	long long bit[max_bit];
public:
	BigFloat() {}
	BigFloat(char source[]) {
		memset(bit, 0, sizeof(bit));
		if (strchr(source, '.')) {
			for (char *rear = source + strlen(source) - 1; !*rear; --rear)
				;
			size_t len = strlen(source);
			fracLen = len - (strchr(source, '.') - source + 1);
			for (char *tmp = strchr(source, '.'); *tmp; ++tmp) {
				*tmp = *(tmp + 1);
			}
		} else
			fracLen = 0;
		val = atoi(source);
		bit[0] = val;
		size = 1;
	}

	void multiBy(int val);
	void expBy(int e);
	void print();
};

void BigFloat::multiBy(int val) {
	for (int i = 0; i < size; ++i)
		bit[i] *= val;
	for (int i = 0; i < size; ++i) {
		if (bit[i] > 9) {
			bit[i + 1] += bit[i] / 10;
			bit[i] %= 10;
		}
	}
	while (bit[size]) {
		bit[size + 1] += bit[size] / 10;
		bit[size] %= 10;
		++size;
	}
}

void BigFloat::expBy(int e) {
	exp = e;
	for (int i = 0; i < e - 1; ++i)
		multiBy(val);
}

void BigFloat::print() {
	if (size <= exp * fracLen) {
		printf(".");
		for (int i = 0; i < exp * fracLen - size; ++i)
			putchar('0');
		int end = 0;
		while (!bit[end])
			++end;
		for (int i = size - 1; i >= end; --i)
			printf("%d", bit[i]);
	} else {
		for (int i = size - 1; i >= exp * fracLen; --i)
			printf("%d", bit[i]);
		int end = 0;
		while (!bit[end] && end < exp * fracLen)
			++end;
		if (end < exp * fracLen) {
			putchar('.');
			for (int i = exp * fracLen - 1; i >= end; --i)
				printf("%d", bit[i]);
		}
	}
}

int main() {
	char source[8];
	int exp;
	while (scanf("%s%d", source, &exp) != EOF) {
		BigFloat bf(source);
		if (bf.val == 0) {
			printf("0\n");
			continue;
		}
		bf.expBy(exp);
		bf.print();
		putchar('\n');
	}
	return 0;
}
//另外, 下面是AC掉的Java code, 个人感觉上面那个C++的code不可能是格式的问题, 因为下面这个Java code AC掉了
import java.io.*;
import java.util.*;
import java.math.*;

public class Main {

	public static void main(String args[]) throws Exception {
		Scanner sin = new Scanner(System.in);
		while (sin.hasNext()) {
			BigDecimal bd = sin.nextBigDecimal();
			int n = sin.nextInt();
			String ans = bd.pow(n).stripTrailingZeros().toPlainString();
			if (ans.charAt(0) == '0')
				System.out.println(ans.substring(1));
			else
				System.out.println(ans);
		}
	}

}[/code]

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator