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

测试了所有的数据,还是WA 用字符串实现的 有人能帮忙看看吗 附代码

Posted by Lucky1984 at 2011-08-07 20:08:22 on Problem 1001
#include "stdio.h"
#include "string.h"
#define MAX_FLOAT_SIZE 10
#define MAX_NUM_LEN 300

int ConvertFloatNum(char* szFloatNum)
{
	int i = 0;
	int iFloatNum = 0;
	char szDstFloastNum[MAX_FLOAT_SIZE + 1] = {NULL};
	int j = 0;
	int iLen = 0;

	iLen = strlen(szFloatNum);
	
	while (szFloatNum[i] != '\0')
	{
		if (szFloatNum[i] == '.')
		{
			iFloatNum =  iLen - 1 - i;
			i++;
			continue;
		}
		
		szDstFloastNum[j++] = szFloatNum[i++];
	}
	
	szDstFloastNum[j] = '\0';
	strncpy(szFloatNum, szDstFloastNum, j + 1);

	return iFloatNum;
}

void CalAddEachBit(char* szFloatNum1, char* szFloatNum2, char* szDstFloatNum)
{
	int i = 0;
	int j = 0;

	int iLen1 = 0;
	int iLen2 = 0;
	int iLen  = 0;
	char szBitFloatNum[MAX_NUM_LEN] = {NULL};

	memset(szBitFloatNum, '0', MAX_NUM_LEN);
	szBitFloatNum[MAX_NUM_LEN - 1] = '\0';

	iLen1 = strlen(szFloatNum1);
	iLen2 = strlen(szFloatNum2);

	iLen = iLen1 < iLen2 ? iLen1 : iLen2;

	for (i = 0; i < iLen; i++, j++)
	{
		if (j == 0)
		{ 
			szDstFloatNum[j] = ((szFloatNum1[i] - '0') + (szFloatNum2[i] - '0')) % 10 + '0';
			szBitFloatNum[j + 1] = (szFloatNum1[i] - '0') / 10 + '0';
		}
		else
		{
			szDstFloatNum[j] = ((szFloatNum1[i] - '0') + (szFloatNum2[i] - '0') + szBitFloatNum[j] - '0') % 10 + '0';
			szBitFloatNum[j + 1] = ((szFloatNum1[i] - '0') + (szFloatNum2[i] - '0')  + szBitFloatNum[j] - '0') / 10 + '0';
		}
	}
	
	if (iLen1 > iLen2)
	{
		for (; i < iLen1; i++, j++)
		{ 
			szDstFloatNum[j] = ((szFloatNum1[i] - '0') + szBitFloatNum[j] - '0') % 10 + '0';
			szBitFloatNum[j + 1] = ((szFloatNum1[i] - '0') + szBitFloatNum[j] - '0') / 10 + '0';
		}
	}
	else
	{
		for (; i < iLen2; i++, j++)
		{ 
			szDstFloatNum[j] = ((szFloatNum2[i] - '0') + szBitFloatNum[j] - '0') % 10 + '0';
			szBitFloatNum[j + 1] = ((szFloatNum2[i] - '0') + szBitFloatNum[j] - '0') / 10 + '0';
		}
	}

	if (szBitFloatNum[j] != '0')
	{
		szDstFloatNum[j] = szBitFloatNum[j];
		szDstFloatNum[j + 1] = '\0';
	}
	else
	{
		szDstFloatNum[j] = '\0';
	}

}

void CalAddEachNum(int iNum, char szFloatNumEachBit[][MAX_NUM_LEN], char* szDstFloatNumOne)
{
	int i = 0;
	char szDstFloatNum[MAX_NUM_LEN] = {NULL};
	
	memset(szDstFloatNum, '0', MAX_NUM_LEN);
	szDstFloatNum[MAX_NUM_LEN - 1] = '\0';

	strncpy(szDstFloatNumOne, szFloatNumEachBit[0], MAX_NUM_LEN);

	for (i = 1; i <= iNum; i ++)
	{ 
		CalAddEachBit(szDstFloatNumOne, szFloatNumEachBit[i], szDstFloatNum);
		strncpy(szDstFloatNumOne, szDstFloatNum, MAX_NUM_LEN);

	}

}


void CalMultiplyEachBit(int iBit, char* szFloatNum, char* szDstFloatNum)
{
	int i = 0;
	int j = 0;
	int iLen = 0;
	char szBitFloatNum[MAX_NUM_LEN] = {NULL};

	memset(szBitFloatNum, '0', sizeof(char) * MAX_NUM_LEN);
	szBitFloatNum[MAX_NUM_LEN - 1] = '\0';

	iLen = strlen(szFloatNum);

	for (i = iLen - 1; i >= 0; i --, j ++)
	{
		
		if (j == 0)
		{
			szDstFloatNum[j] = (szFloatNum[i] - '0') * iBit % 10 + '0';
			szBitFloatNum[j + 1] = (szFloatNum[i] - '0') * iBit / 10 + '0';
		}
		else
		{ 
			szDstFloatNum[j] = ((szFloatNum[i] - '0') * iBit + szBitFloatNum[j] - '0') % 10 + '0';
			szBitFloatNum[j + 1] = ((szFloatNum[i] - '0') * iBit + szBitFloatNum[j] - '0') / 10 + '0';
		}

	}

	if (szBitFloatNum[j] != '0')
	{
		szDstFloatNum[j] = szBitFloatNum[j];
		szDstFloatNum[j + 1] = '\0';
	}
	else
	{
		szDstFloatNum[j] = '\0';
	}
 	
}
void CalMultiplyEachNum(char* szFloatNum, char* szFloatNumMutiply, char* szDstFloatNumOne)
{
	int i = 0;
	char szFloatNumBit[MAX_FLOAT_SIZE - 1][MAX_NUM_LEN] = {NULL};
	char szProcFloatNumBit[MAX_NUM_LEN] = {NULL};
	int	iLen = 0;	
	int	iLen1 = 0;	

	for (i = 0; i < MAX_FLOAT_SIZE - 1; i++)
	{
		memset(szFloatNumBit[i], '0', MAX_NUM_LEN);
		szFloatNumBit[i][MAX_NUM_LEN - 1] = '\0';

	}

	memset((char*)szProcFloatNumBit, '0', MAX_NUM_LEN);
	szProcFloatNumBit[MAX_NUM_LEN - 1] = '\0';
	
	iLen1 = strlen(szFloatNum);
	
	for (i = 0; i < iLen1; i ++)
	{
		CalMultiplyEachBit(szFloatNum[iLen1 - 1 - i] - '0', szFloatNumMutiply, szFloatNumBit[i]);
		iLen = strlen(szFloatNumBit[i]);
		memset((char*)szProcFloatNumBit, '0', MAX_NUM_LEN - 1);
		strncpy((char*)&szProcFloatNumBit[i], (char*)&szFloatNumBit[i][0], MAX_NUM_LEN - i);
		strncpy((char*)szFloatNumBit[i], (char*)szProcFloatNumBit, MAX_NUM_LEN);
		szFloatNumBit[i][iLen + i] = '\0';
	}
	
	CalAddEachNum(iLen1 - 1, szFloatNumBit, szDstFloatNumOne);
	
}

void SwapFloatNum(char* szFloatNum)
{
	int i = 0;
	int j = 0;
	int iLen = 0;
	char szDstFloatNum[MAX_NUM_LEN] = {NULL};

	iLen = strlen(szFloatNum);

	for (i = 0; szFloatNum[i] != '\0'; i++)
	{
		szDstFloatNum[iLen - i - 1] = szFloatNum[i];
	}

	szDstFloatNum[iLen] = '\0';

	strncpy(szFloatNum, szDstFloatNum, MAX_NUM_LEN);
}

void GetAllResult(int iPoint, char* szFloatNum)
{
	int iLen = 0;
	int iDstLen = 0;
	int i = 0;
	int j = 0;
	char szDstFloatNum[MAX_NUM_LEN] = {NULL};
	int iIsFloat = 0;
	int iPointPara = 0;

	iLen = strlen(szFloatNum);

	for (i = 0; i < iLen; i ++)
	{
		if (szFloatNum[i] != '0')
		{
			break;
		}
	}

	strncpy(szDstFloatNum, &szFloatNum[i], iLen - i);
	szDstFloatNum[iLen - i] = '\0'; 

	iDstLen = strlen(szDstFloatNum);

	if (iDstLen > iPoint)
	{ 

		iPointPara = iDstLen - iPoint;

		strncpy(szFloatNum, szDstFloatNum, iPointPara);

		for (i = iPointPara; szDstFloatNum[i] != '\0'; i++)
		{
			if (szDstFloatNum[i] != '0')
			{
				iIsFloat = 1;
				break;
			}
		}

		if (iIsFloat == 1)
		{
			szFloatNum[iPointPara] = '.';

			for (i = iDstLen - 1; i >= iPointPara; i--)
			{
				if (szDstFloatNum[i] == '0')
				{ 
					iPoint--;
				}
				else
				{
					break;
				}
			}

			strncpy(&szFloatNum[iPointPara + 1], &szDstFloatNum[iPointPara], iPoint);
			
			szFloatNum[iPoint + iPointPara + 1] = '\0';
		}
		else
		{ 
			szFloatNum[iPointPara] = '\0';
		}

	}
	else 
	{
		iPointPara = iPoint - iDstLen;

		szFloatNum[0] = '.';
		if (iPoint > iDstLen)
		{
			memset(&szFloatNum[1], '0', iPointPara);

		}

		for (i = iDstLen - 1; i >= 0; i--)
		{
			if (szDstFloatNum[i] != '0')
			{
				break;
			}
			else
			{
				iDstLen--;				
			}
			
		}

		strncpy(&szFloatNum[iPointPara + 1], szDstFloatNum, iDstLen);

		szFloatNum[iPointPara + 1 + iDstLen] = '\0';

	}

}
void CalAllNum(int iNum, char* szSrcFloatNum, char* szDstFloatNum)
{
	int i = 0;
	char szDstNum[MAX_NUM_LEN] = {NULL}; 	

	memset((char*)szDstNum, '0', MAX_NUM_LEN);
	szDstNum[MAX_NUM_LEN] = '\0';

	strncpy(szDstFloatNum, szSrcFloatNum, MAX_NUM_LEN);
	
	for (i = 0; i < iNum - 1; i++)
	{
		CalMultiplyEachNum(szSrcFloatNum, szDstFloatNum, szDstNum);
		SwapFloatNum(szDstNum); 
		strncpy(szDstFloatNum, szDstNum, MAX_NUM_LEN);

	}
	
}

int IsEqualToZero(char* szFloatNum)
{
	int i = 0;
	int iIsNonZero = 0;

	for (i = 0; szFloatNum[i] != '\0'; i++)
	{
		if (szFloatNum[i] != '0')
		{
			iIsNonZero = 1;
			break;
		}
	}

	return iIsNonZero;
}

int main()
{
	char szFloatNum[MAX_FLOAT_SIZE + 1] = {NULL};
	int n = 0;
	int iGetPoint = 0; 
	char szDstFloatNum[MAX_NUM_LEN] = {NULL};
	int iRet = 0;

	while (scanf("%s %d", szFloatNum, &n) != EOF)
	{
		memset((char*)szDstFloatNum, '0', MAX_NUM_LEN);
		szDstFloatNum[MAX_NUM_LEN] = '\0'; 
		iGetPoint = ConvertFloatNum(szFloatNum);
		if (n == 0)
		{
			printf("1\n");
			continue;
		}
		else
		{
			CalAllNum(n, szFloatNum, szDstFloatNum);
		}
		
		iRet = IsEqualToZero(szDstFloatNum);
		if (!iRet)
		{ 
			printf("0\n");
		}
		else
		{
			GetAllResult(iGetPoint * n, szDstFloatNum);
			printf("%s\n", szDstFloatNum);
		}

	}
	return 1;
}

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