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

哪位大师指点一下,我的程序哪里错了.代码有注释

Posted by whd0810 at 2008-04-15 13:06:51 on Problem 1702
//思路:把物体的重量转化为 3 进制,如果在 i 位上有 2 则在左边加一个重量为 pow(3,i-1)的砝码
	//最终左边的重量转化为只有0,1组成的3进制,右边需要放的砝码就是左边1所对应的权值
#include<stdio.h>
#include<math.h>
#include<string.h>

__int64 W;
__int64 weight[25];  //把 W 转化成3进制的各位数
int top;     // 转化3进制时的栈顶指针
bool flag;    //标记左边需不需要砝码
int right[25];  //在右边的砝码
int rCnt;       //右边需要放的砝码数
int left[25];  //在左边的砝码
int lCnt;   //左边需要放的砝码数
void init()
{
	memset(right,0,sizeof(right));
	memset(left,0,sizeof(left));
	memset(weight,0,sizeof(weight));
	rCnt=0;
	lCnt=0;
	flag=false;
	top=0;
	__int64 tempW;
	scanf("%I64d",&W);
	tempW=W;
	while(tempW!=0)   //转化为3进制
	{
		weight[++top]=(tempW%3); //转化为
		tempW/=3;
	}
}

void solve()
{
	int i;
	__int64 carry=0;
	for(i=1;i<=(top+2);i++)  
	{
		carry=weight[i]/3;   // 是否有进位
		weight[i]=weight[i]%3;  //取余
		weight[i+1]+=carry;  //高位加进位
		if(weight[i]==1)    //如果为 1 则左边不需要放砝码
		{
			right[++rCnt]=i;   // 右边放砝码
		}
		if(weight[i]==2)     //如果为 2 则放一个砝码在左边,使之与物体相加得 0 
		{
			flag=true;    //左边需要放砝码
			left[++lCnt]=i;  //放一个砝码在左边
			weight[i]=0;   
			weight[i+1]=(weight[i+1]+1);  //高位 加 1
		}
	}
}

void display()   //输出
{
	int i;
	if(!flag)
	{
		printf("empty ");
		printf("%I64d",(__int64)pow((float)3,right[1]-1));
		for(i=2;i<=rCnt;i++)
		{
			printf(",%I64d",(__int64)pow((float)3,right[i]-1));
		}
		printf("\n");
	}	
	else
	{
		printf("%I64d",(__int64)pow((float)3,left[1]-1));
		for(i=2;i<=lCnt;i++)
		{
			printf(",%I64d",(__int64)pow((float)3,left[i]-1));
		}
		printf("% I64d",(__int64)pow((float)3,right[1]-1));
		for(i=2;i<=rCnt;i++)
		{
			printf(",%I64d",(__int64)pow((float)3,right[i]-1));
		}
		printf("\n");
	}
}
int main(int argc, char* argv[])
{
	int T;
	scanf("%d",&T);
	
	while(T--)
	{
		init();
		solve();
		display();
	}
	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