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

Re:C++代码,网上找到的测试例子都试过,但提交后仍然WA,哪位高人可以抽空帮忙指点一下,多谢

Posted by virusx1984 at 2014-01-19 01:06:43 on Problem 1001
In Reply To:C++代码,网上找到的测试例子都试过,但提交后仍然WA,哪位高人可以抽空帮忙指点一下,多谢 Posted by:ah_syh at 2013-11-18 14:08:36
> #include <stdio.h>
> #include <vector>
> #include <string>
> #include <string.h>
> #include <iostream>
> 
> 
> using namespace std;
> 
> const int MAX_BIT_NUMBER=256;
> 
> //Super interger class
> class SI {
> public:
> 	int point_pos;
> 	int length;
> 	unsigned char digitals[MAX_BIT_NUMBER];
> 
> 	SI& operator=(int i);
> 	SI& operator=(SI& tmp);
> 	SI& operator+(SI& tmp);
> 	SI& operator*(int i);
> 	SI& operator*(SI& tmp);
> 
> 	void print();
> 	void adjust();
> 
> public:
> 	SI();
> 	SI(int i);
> 	SI(char* str);
> 	SI(SI& tmp);
> 	virtual ~SI();
> };
> 
> SI::SI():point_pos(0),length(1) {
> 	// TODO Auto-generated constructor stub
> 	memset(digitals,0,MAX_BIT_NUMBER*sizeof(unsigned char));
> }
> 
> SI::SI(int i):point_pos(0),length(0) {
> 	memset(digitals,0,MAX_BIT_NUMBER*sizeof(unsigned char));
> 	while(i!=0)
> 	{
> 		digitals[length] = i%10;
> 		i /= 10;
> 		length++;
> 	}
> }
> 
> void SI::adjust()
> {
> 	//eliminate the latest 0 after point
> 	int i=0;
> 	for(i=0;i<point_pos;i++)
> 	{
> 		if(digitals[i]!=0)break;
> 	}
> 	if(i!=0)
> 	{
> 		for(int j=0;j<length;j++)
> 		{
> 			if(j<length-i)
> 				digitals[j]=digitals[j+i];
> 			else
> 				digitals[j]=0;
> 		}
> 		length -= (i);
> 		point_pos -= i;
> 	}
> 
> 	for(i=0;i<length;i++)
> 	{
> 		if(digitals[length-i-1]!=0)break;
> 	}
> 
> 	if(i!=0)
> 	{
> 		length -= i;
> 	}
> 	if(length==0)
> 		length = 1;
> }
> 
> SI::SI(char* str):point_pos(0),length(0) {
> 	memset(digitals,0,MAX_BIT_NUMBER*sizeof(unsigned char));
> 
> 	int new_length = strlen(str);
> 	length=0;
> 	for(int i=0;i<new_length;i++){
> 		if(str[new_length-i-1]=='.')
> 			point_pos = length;
> 		else
> 		{
> 			digitals[length]=str[new_length-i-1]-'0';
> 			length++;
> 		}
> 	}
> 	adjust();
> }
> 
> SI::SI(SI& tmp):point_pos(tmp.point_pos),length(tmp.length){
> 	// TODO Auto-generated constructor stub
> 	memcpy(digitals,tmp.digitals,MAX_BIT_NUMBER*sizeof(unsigned char));
> }
> 
> SI::~SI() {
> 	// TODO Auto-generated destructor stub
> }
> 
> SI& SI::operator=(int i) {
> 	// TODO Auto-generated destructor stub
> 	point_pos = 0;
> 	length = 0;
> 	memset(digitals,0,MAX_BIT_NUMBER*sizeof(unsigned char));
> 	while(i!=0)
> 	{
> 		digitals[length] = i%10;
> 		i /= 10;
> 		length++;
> 	}
> 	return *this;
> }
> 
> SI& SI::operator=(SI& tmp) {
> 	// TODO Auto-generated destructor stub
> 	point_pos = tmp.point_pos;
> 	length = tmp.length;
> 	memcpy(digitals,tmp.digitals,MAX_BIT_NUMBER*sizeof(unsigned char));
> 	return *this;
> }
> 
> SI& SI::operator+(SI& tmp) {
> 	// TODO Auto-generated destructor stub
> 	SI result(*this);
> 	//point_pos = tmp.point_pos;
> 
> 	int new_length = (length > tmp.length)?length:tmp.length;
> 	int jinwei=0;
> 	result.length = new_length;
> 	for(int i=0;i<(new_length);i++){
> 		result.digitals[i]=digitals[i]+tmp.digitals[i]+jinwei;
> 		jinwei = result.digitals[i] / 10;
> 		result.digitals[i] = result.digitals[i] % 10;
> 	}
> 
> 	if(jinwei!=0)
> 	{
> 		result.digitals[new_length]=jinwei;
> 		result.length = new_length+1;
> 	}
> 
> 	return result;
> }
> 
> SI& SI::operator*(int i) {
> 	SI result;
> 
> 	if(i==0) return result;
> 	if(length==1&&digitals[0]==0) return result;
> 	if(i==10) {
> 		result.digitals[0]=0;
> 		for(int j=0;j<length;j++){
> 			result.digitals[j+1]=digitals[j];
> 		}
> 		result.length = length+1;
> 		return result;
> 	}
> 
> 	int jinwei=0;
> 	result.length = length;
> 	for(int j=0;j<length;j++){
> 		result.digitals[j]=digitals[j]*i+jinwei;
> 		jinwei = result.digitals[j] / 10;
> 		result.digitals[j] = result.digitals[j] % 10;
> 	}
> 
> 	if(jinwei!=0){
> 		result.digitals[result.length]=jinwei;
> 		result.length = length+1;
> 	}
> 
> 	return result;
> }
> 
> SI& SI::operator*(SI& tmp) {
> 	// TODO Auto-generated destructor stub
> 	SI result;
> 	SI temp_val1(*this);
> 	SI temp_val2;
> 
> 	for(int i=0;i<tmp.length;i++){
> 		int j=tmp.digitals[i];
> 		temp_val2 = temp_val1*j;
> 		result = result+temp_val2;
> 		temp_val1=temp_val1*10;
> 	}
> 	result.point_pos = point_pos + tmp.point_pos;
> 	//point_pos = tmp.point_pos;
> 
> 	result.adjust();
> 
> 	return result;
> }
> 
> void SI::print(){
> 	if(length<=point_pos)
> 	{
> 		printf(".");
> 
> 		for(int i=0;i<point_pos-length;i++){
> 			printf("0");
> 		}
> 		for(int i=0;i<length;i++){
> 			printf("%d",digitals[length-i-1]);
> 		}
> 		printf("\n");
> 
> 		return;
> 	}
> 	int i;
> 
> 	for(i=0;i<length-point_pos;i++){
> 		printf("%d",digitals[length-i-1]);
> 	}
> 	if(point_pos!=0)
> 	{
> 		printf(".");
> 		for(;i<length;i++){
> 			printf("%d",digitals[length-i-1]);
> 		}
> 	}
> 	printf("\n");
> 	return;
> }
> 
> void cal_exponent(char* s1,int d){
> 	if(d==0)
> 	{
> 		printf("1\n");
> 		return;
> 	}
> 
> 	SI result(1);
> 	SI tmp_val(s1);
> 
> 	while(d!=0)
> 	{
> 		if(d&0x1)
> 			result = result*tmp_val;
> 		tmp_val = tmp_val*tmp_val;
> 		d=d>>1;
> 	}
> 	result.print();
> }
> 
> int main(void)
> {
> 	char s1[128];
> 	int d;
> 
> 
> 	while(scanf("%s%d",s1,&d)!=EOF)
> 	{
> 		s1[6] = '\0';
> 		cal_exponent(s1,d);
> 	}
> 
> 	return 0;
> }

第一,首先的邏輯都錯了, 0.0100 的 3次方,你居然算出 .000001060900
第二,你的main函數裏面的那個while循環寫得有問題,我之前也是這樣,看到你的源碼發現 cal_exponent裏面有print語句,也就是說輸入兩個數據就馬上計算然後打印,這是不對的。要把一組數據全部計算完後再一起打印,這樣才正確。可以參考一下我用C通過的代碼,鏈接如下:http://poj.org/showmessage?message_id=341901

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