c++学习笔记(20)ACM基础之北大1001

1.先贴一下题目要求

Description

Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. 
This problem requires that you write a program to compute the exact value of R n where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25.

Input

The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9.

Output

The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer.

Sample Input

95.123 12
0.4321 20
5.1234 15
6.7592  9
98.999 10
1.0100 12

Sample Output

548815620517731830194541.899025343415715973535967221869852721
.00000005148554641076956121994511276767154838481760200726351203835429763013462401
43992025569.928573701266488041146654993318703707511666295476720493953024
29448126.764121021618164430206909037173276672
90429072743629540498.107596019456651774561044010001
1.126825030131969720661201

Hint

If you don't know how to determine wheather encounted the end of input: 
s is a string and  n is an integer

2.这个问题要用字符串来存储基数,指数用整型。我开始思考的思路是:判断有没有 小数点‘.’, 如果是没有小数点,直接就是两个整数相乘的问题。如果有的话,先提取出来,然后最后在将小数点放回去,并且小数点后面的如果是0结尾要去掉,开头如果是0+'.'要将0不显示。

我的按照上述思路了编写代码:

#include <iostream>
#include "iomanip"
#include "math.h"
#include "cstring"
#include "string"
#include "stack"
#include "memory"
using namespace std;

int smallPoint(string &str1);
void pointdelete(string &str, int smallPoint);
void pointadd(string &str, int pointnum_right);
void deletezeros(string &str);
bool judgePreZeor(string &str, int pointnum_left);
void pointadd_pre(string &str, int pointnum_right);
/***********************************************************************/

void mulInteger_2(string &str1, string &str2)   //两个整数相乘
{
	int num1[200] = {0};
	int num2[200] = {0};
	int result[300] = {0};
	//auto_ptr<string> output(new string (str2));

	for(int i = 0; i < str1.size(); i++)
	{
		num1[i] = (int)(str1[str1.size() - 1 - i] - '0');
		
	}
	for(int i = 0; i < str2.size(); i++)
	{
		num2[i] = (int)(str2[str2.size() - 1 - i] - '0');	
	}
	
	for(int i = 0; i < str1.size(); i++)
	{
		for(int j = 0; j < str2.size(); j++)
		{
			result[i+j] += num1[i] * num2[j];
		}
	}

	for(int i = 0; i < 300; i++)
	{
		if(result[i] >= 10)
		{
			result[i+1] += result[i]/10;
			result[i] = result[i]%10;
		}
	}

	int i, j, count = 0;
	for( i = 300-1; i >= 0; i--)
		if(result[i]) break;
	char *output = new char[i+2];
	for(j = i; j >= 0; j--)
	{
		output[count] = (char)(result[j] + '0');
		count++;
	}
	output[count] = '\0';
	str2.assign(output);

}
/******************************************************************/
int main()
{
	string str;
	int integer;
	while(cin >> str >> integer)
	{
		int time = integer - 1;
		int pointnum_right = smallPoint(str);
		int pointnum_left = str.size() + 1 - pointnum_right;
		if(!pointnum_right)   //无小数点
		{
			
			string output_pointless = str;
			while(time--)
				mulInteger_2(str, output_pointless);
			cout << output_pointless << endl;
		}
		else            //有小数点 
		{
			if(judgePreZeor(str, pointnum_left))   //判断小数点前是否全部为0   //不全部为0
			{
				pointdelete(str, pointnum_left);
				string output_point = str;
				while(time--)
					mulInteger_2(str, output_point);
				pointnum_left = output_point.size() - ((pointnum_right - 1) * integer) + 1;  //(pointnum_right - 1) * integer 为右侧小数点的总个数   例如???.?? 5-2+1  
				pointadd(output_point, pointnum_left);
				if(output_point[0] == '0'|| output_point[output_point.size() - 1] == '0')
				deletezeros(output_point);
				cout << output_point;
			}
			else   //全部为0
			{
				pointdelete(str, pointnum_left);
				string output_point = str;
				while(time--)
					mulInteger_2(str, output_point);
				pointnum_right = ((pointnum_right - 1) * integer) + 1;
				pointadd_pre(output_point, pointnum_right);
				deletezeros(output_point);
				cout << output_point;
			}
		}
	}	
	cin.get();
	cin.get();
	return 0;
}

/******************************************************************************/
 //检测是否有小数点,并返回它的位置,从右数第几个
int smallPoint(string &str)  
{
	int num = 0;
	while(str[num] != '.'&& num < str.size())
	{
		num++;
	}
	if(num < str.size())
	{
		return (str.size() - num);  //小数点后有几位数。
	}
	else return 0;
}

//将小数点清除,返回整数
void pointdelete(string &str, int pointnum)  
{
	int size = str.size() - 1;
	char *pointdelete = new char[size];
	
	int num = 0;
	for(int count = 0; count < str.size(); count++)
	{
		if(count != pointnum - 1)
		{
			pointdelete[num] = str[count];
			num++;
		}
		else
			continue;
	}
	pointdelete[num] = '\0';   //remember it
	str.assign(pointdelete);
}

 //将小数点添加回来
void pointadd(string &str, int pointnum_left) 
{
	int size = str.size() + 1;
	char *pointadd = new char[size];

	int num = 0;
	for(int count = 0; count < str.size(); count++)
	{
		if(count != (pointnum_left -1))
		{
			pointadd[num] = str[count];
			num++;
		}
		else
		{
			pointadd[num] = '.';
			num++;
			pointadd[num] = str[count];
			num++;
		}
	}
	pointadd[num] = '\0'; 
	str.assign(pointadd);
}

//清零工作
void deletezeros(string &str)
{
	int size = str.size();
	int count_beh = 0;
	char *deletezeros = new char[];
	int i = 0;
	int count_pre = 0;
	while(str[i] == '0')
	{
		count_pre++;
		i++;
	}
	i = 0;
	while(str[size-1 - i] == '0')
	{
		count_beh++;
		i++;
	}
	if(count_pre || count_beh)
	{
		if(count_pre)
		for(i = 0; i < size; i++)
		{
			deletezeros[i] = deletezeros[i+count_pre];
		}
		else if(count_beh)
		{
			for(i = 0; i < size - count_pre - count_beh; i++)
				deletezeros[i] = str[i+count_pre];
		}
		deletezeros[size - count_beh - count_pre] = '\0';
		str.assign(deletezeros);
	}
}

//判断小数点前是否全部为零
bool judgePreZeor(string &str, int pointnum_left)  
{
	int num = 0;
	while(str[num] == '0'&&num < pointnum_left)
	{
		num++;
	}
	if(num == pointnum_left - 1)
	{
		return false;   //全部为0
	}
	else 
		return true;
}

//小数点前如果前面全部为零,则将结果中的小数点前的0省掉,直接显示 ".?????"
void pointadd_pre(string &str, int pointnum_right)
{
	int size = str.size();
	char *pointadd_pre = new char[pointnum_right];
	pointadd_pre[0] = '.';

	int count;
	for(count = 1; count < (pointnum_right - size); count++)
	{
		pointadd_pre[count] = '0';
	}

	for(int i = 0; i < size; i++)
	{
		pointadd_pre[count] = str[i];
		count++;
	}
	pointadd_pre[count] = '\0';
	str.assign(pointadd_pre);
	
}

但是有一个问题:我自己在进行调试和运行时,和结果都是一样的,基本功能都实现了,但是就是通不过,我想应该用数组代替字符串,后序会进行更改,并且希望各位大神级人物能够给以点播!谢谢。


3. 并且一位网友给了一个可以通过的简洁代码,学习了,并在此表达感谢。

#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string mlp;     //乘数

	int power;     //乘数的幂
	int r[151];    //保存结果
	int hdot;

	while(cin>>mlp>>power)
	{
		hdot=0;
		for(int t=0;t<150;t++)
		{
			r[t]=-1;
		}
		if(mlp.find(".")!=string::npos)
				hdot=mlp.length()-mlp.find(".")-1;
		string::iterator itr=mlp.end()-1;

		while(hdot>0&&itr>=mlp.begin())
		{
			if(*itr!='0')
			{break;}
			hdot--;
			itr--;
		}

		int cn=0;

		while(itr>=mlp.begin())
		{
			if(*itr!='.')
			{
				r[cn]=*itr-'0';
				cn++;
			}
			itr--;
		}
	
		int k=cn-1;
		int m=0;     //保存临时数;

		while(k>-1)
		{
			m=m*10+r[k];
			k--;
		}

		for(int i=1;i<power;i++)
		{
			int j=0;
			while(r[j]>-1)
			{
				r[j]=r[j]*m;
				j++;
			}
			j=0;
			while(r[j]>-1)
			{
				if(r[j+1]==-1&&r[j]>=10)
				r[j+1]=r[j]/10;
				else
				r[j+1]+=r[j]/10;
				r[j]=r[j]%10;
				j++;
			}
		}

		hdot=hdot*power;
		int cnt=0;

		while(r[cnt]>-1)
		{
			cnt++;
		}
		if(hdot>=cnt)
		{
			cout<<".";
			while(hdot>cnt)
			{
				cout<<"0";
				hdot--;
			}
			hdot=0;
		}
		for(k=cnt-1;k>=0;k--)
		{
			if((k+1)==hdot&&hdot!=0)
				cout<<".";
			cout<<r[k];
			}
		cout<<endl;
	}
	return 0;
} 

 



你可能感兴趣的:(C++,String,Integer,iterator,input,output)