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,实在找不出问题,总得到wrong answer!

Posted by huowaa at 2012-02-26 06:24:54
刚开始做题,1001的Exponentiation,我写完实在找不出问题了,但提交总是wrong answer,请高手指点,非常感谢!

代码入下:=================================

#include <iostream>
#include <string.h>
using namespace std;

static const int COLS = 6;  //max r's colums
static const int MAX = 151; //6*25+1

int get_base(char r[], int *k){
	int length = strlen(r);
	int k_pos = -1;
	int base = 0;
	//a to i
	for(int i=0; i<length; i++){
		if(r[i] == '.'){
			k_pos = i;
			continue;
		}
		
		base = base * 10 + (r[i] - '0');
	}

	*k = k_pos > 0 ? (length-1-k_pos) : 0;
	return base;
}

//caculate power by big multiply
void power(int base, int n, int result[]){
	//init result
	for(int i=0; i<MAX; i++) result[i] = 0;

	//digital base
	int i = MAX - 1;
	int tmp = base;
	while(tmp != 0){
		result[i--] = tmp % 10;
		tmp /= 10;
	}

	//power caculation(attention, n-1 bug not n)
	for(int i=0; i<n-1; i++){
		int set = 0; //jinwei!
		//result[j] == 0也必须继续,因为可能还有进位
		for(int j=MAX-1; j>=0; j--){
			if(result[j] == 0 && set == 0) continue;
			result[j] = result[j] * base + set;
			//jinwei! use in next loop
			set = result[j] / 10;
			result[j] = result[j] % 10;
		}
	}
}

void print(int result[], int k){
	int start=0, end=0;
	int zeros = 0; //prefix zeros

	//start
	for(int i=0; i<MAX; i++){
		if(result[i] != 0){start = i; break;}
	}

	if(k == 0){
		end = MAX-1;
	}else{
		//end	
		for(int j=MAX-1; j>=0; j--){
			if(result[j] != 0){end = j; break;}
		}
	
		if(k > (MAX-1-start+1)) zeros = k - (MAX-1-start+1);
	}

	//print
	//cout << "start, end, zeros=" << start << ", "<< end << ", " << zeros << endl;
	if(k == 0){		//xxxx
		for(int i=start; i<=end; i++) cout << result[i];
	}else{
		if(zeros > 0){	//.000xxx
			cout << ".";
			for(int i=0; i<zeros; i++) cout << "0";
			for(int i=start; i<=end; i++) cout << result[i];
		}else{		//x.xxx
			for(int i=start; i<start+(MAX-1-start+1-k); i++) cout << result[i];
			cout << ".";
			for(int i=start+(MAX-1-start+1-k); i<=end; i++) cout << result[i];
		}
	}
	cout << endl;

}

void run(){
	char r[COLS];
	int n;
	int result[MAX];
	while(cin >> r >> n){
		int int_base;  //基数的int值
		int k;         //小数位数
		
		int_base = get_base(r, &k);

		power(int_base, n, result);
		
		print(result, k*n);
		
	}
}

int main(int argc, char *argv[]){
	run();
	return 0;
}

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