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 Hacker_QC at 2010-02-28 09:52:07 on Problem 1001
In Reply To:基本思路+代码 Posted by:cyb88 at 2010-02-28 09:08:51
> 基本上就是大数乘。
> 我的思路:
> 1 首先判断是不是0次方,是就直接输出1.(后来没加也过了,证明测试数据没有0次方的)
> 2 测试有没有小数点 有的话去除后面无用的0 记录下小数有多少位,留作输出的时候点小数  点。然后去除前面的0和小数点,转化成大数乘。如果外加判断类似10.000的情况,也直接转为没有小数的形式
> 3 大数乘,n次
> 4 点小数点, 需要前面补0 不需要补0
> 
> 代码:
> #include <iostream>
> #include <cstring>
> using namespace std;
> 
> unsigned int a[100];
> unsigned int b[100];
> unsigned int result[100];
> 
> char arr[100];
> 
> int main()
> {
> 	int n,i,j,k,len,len_a,len_b,jinwei,p,xiaoshu;
> 	bool flag;
> 
> 	while(cin>>arr>>n)
> 	{
> 		if(n==0)
> 		{
> 			cout << "1" << endl;
> 			continue;
> 		}
> 		memset(a,0,sizeof(a));
> 		memset(b,0,sizeof(b));
> 		memset(result,0,sizeof(result));
> 		len = strlen(arr);
> 		flag = false;
> 		//首先检查小数点
> 		for(i=len-1,j=0; i>=0; i--)
> 		{
> 			if(arr[i] == '.')
> 			{
> 				flag = true;
> 				break;
> 			}
> 		}
> 		//预处理,去掉后面没用的0
> 		if(flag)
> 		{
> 			i=len-1;
> 			while(arr[i] == '0')
> 			{
> 				i--;
> 			}
> 			if(arr[i] == '.')
> 			{
> 				i--;
> 				flag = false;
> 			}
> 		}
> 
> 		//倒序输入,方便乘法
> 		for(; i>=0;i--)
> 		{
> 			if(arr[i] != '.')
> 			{
> 				a[j]=arr[i]-'0';
> 				b[j]=arr[i]-'0';
> 				j++;
> 			}
> 			else
> 			{
> 				xiaoshu=j;
> 			}
> 		}
> 		len_a=len_b=j;
> 
> 		i=0;
> 		while(flag)
> 		{
> 			if((arr[i]>'0'&&arr[i]<='9'))
> 			{
> 				i++;
> 				break;
> 			}
> 			else if(arr[i] == '.')
> 			{
> 				i++;
> 			}
> 			else
> 			{
> 				len_a--;
> 				len_b--;
> 				i++;
> 			}
> 		}
> 		
> 		//就算总的小数位:
> 		xiaoshu = xiaoshu*n;
> 
> 		while(--n)
> 		{
> 			//大数乘
> 			memset(result, 0, sizeof(result));
> 			for(i=0; i<len_a; i++)
> 			{
> 				for(j=0; j<len_b; j++)
> 				{
> 					result[i+j] += a[i]*b[j];
> 				}
> 			}
> 			//处理进位
> 			len_a = len_a+len_b;
> 			jinwei=0;
> 			for(i=0; i<len_a; i++)
> 			{
> 				result[i] += jinwei;
> 				jinwei = result[i]/10;
> 				result[i] %= 10;
> 			}
> 			if(result[len_a-1] == 0)
> 			{
> 				len_a--;
> 			}
> 			memset(a, 0, sizeof(a));
> 
> 			for(i=0; i<len_a; i++)
> 			{
> 				a[i] = result[i];
> 			}
> 		}
> 
> 		if(flag)
> 		{
> 			//这一步特别比较繁琐
> 			if(len_a > xiaoshu)
> 			{
> 				j = len_a-xiaoshu+1;
> 				for(i=0; i<=j&&a[i]==0; i++)
> 				{
> 					;
> 				}
> 				k=i;
> 				//不用输出小数点
> 				if(i==j)
> 				{
> 					for(i=len_a-1; i>=k; i--)
> 					{
> 						cout << a[i];
> 					}
> 				}
> 				//需要输出
> 				else
> 				{
> 					//先输出小数点前面的
> 					j = len_a-xiaoshu;
> 					i = len_a-1;
> 					while(j--)
> 					{
> 						cout << a[i--];
> 					}
> 					cout << ".";
> 					for(; i>=k; i--)
> 					{
> 						cout << a[i];
> 					}
> 				}
> 			}
> 			else if(len_a == xiaoshu)
> 			{
> 				//倒去后面的0
> 				j=0;
> 				while(a[j] == 0)
> 				{
> 					j++;
> 				}
> 				cout << ".";
> 				for(i=len_a-1; i>=j; i--)
> 				{
> 					cout << a[i];
> 				}
> 			}
> 			else
> 			{
> 				cout << ".";
> 				//计算前面要多输出几个0
> 				j = xiaoshu-len_a;
> 				for(i=0; i<j; i++)
> 				{
> 					cout << "0";
> 				}
> 				//倒去后面的0
> 				j=0;
> 				while(a[j] == 0)
> 				{
> 					j++;
> 				}
> 				for(i=len_a-1; i>=j; i--)
> 				{
> 					cout << a[i];
> 				}
> 			}
> 			cout << endl;
> 		}
> 		//没有小数点的情况
> 		else
> 		{
> 			for(i=len_a-1; i>=0; i--)
> 			{
> 				cout << a[i];
> 			}
> 			cout << endl;
> 		}
> 	}
> }

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