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

我也贴一个,C++写的

Posted by freelark at 2016-10-28 11:38:56 on Problem 1001
#include <iostream>
#include<string>
#include<stdlib.h>
#include<vector>
using namespace std;

string multiply(const char* x, const char* y);//整数乘法
string exponentiationInt(const char* x, int y);//整数幂运算
string exponentiation(const char* x, int y);//输入输出格式处理

int main(void)
{
	string  s;
	int n;
	vector<string> vs;
	vector<int> vi;
	while (cin >> s >> n){  //不匹配时退出
		vs.push_back(s);
		vi.push_back(n);
	}
	for (int i = 0; i < vs.size(); i++){
		cout << exponentiation(vs[i].c_str(), vi[i]).c_str() << endl;
	}
	system("pause");
	return 0;
}

string multiply(const char* x, const char* y){
	if (y == NULL)y = x;
	if (string(y) == "0" || string(x) == "0") return "0";
	int l1 = strlen(x);
	int l2 = strlen(y);
	int l = l1 + l2;
	
	int *temp = new int[l];
	memset(temp, 0, l*sizeof(int));

	int k = 0;
	for (int i = l2 - 1; i >= 0; i--){
		k = l2 - i - 1;
		for (int j = l1 - 1; j >= 0; j--){
			temp[k++] += (y[i] - '0')*(x[j] - '0');
		}
	}
	for (int i = 0; i<l - 1; i++){
		temp[i + 1] += temp[i] / 10;
		temp[i] = temp[i] % 10;
	}
	string str = "";
	for (int i = l - 1; i >= 0; i--){
		str += temp[i] + '0';
	}
	delete[]temp;
	if (str[0] == '0') str.erase(0, 1);
	return str;
}
string exponentiationInt(const char* x, int y){
	if (y >= 0){
		if (y == 1){
			return  x;
		}
		else if (y == 0)
			return "1";
		return multiply(x, exponentiationInt(x, y - 1).c_str());
	}
	else{
		return "0";
	}
}
string exponentiation(const char* x, int y){
	/*1、清除字串x前后的0;
	   2、如果有小数点,清除并记录位置;
	   3、判断字串(如果x全0,返回0,否则:如果y为0,返回1)
	*/
	string s = x;
	int pos = 0;	
	//1、清除字串x前的0;
	int len = s.length();
	for (int i = 0; i <len; i++){
		if (s[0] != '0') break;
		else s.erase(0, 1);
	}
	//清除字串x小数点后的0;
	if (s != "")
	while (*(s.end() - 1) == '0'&&s.length()>s.find('.'))
			s.erase(s.length() - 1, 1);
	//2、存在小数点时	
	if (s.find(".") != string::npos){		
		pos = s.find('.');   //记录位置
		s.erase(pos, 1);   //清除
		pos = (s.length() - pos) *y;  //小数点在最终结果时的位置
	}
	//3、判断字串(如果x全0,返回0,否则:如果y为0,返回1)
	if (s == "") return "0";
	if (y == 0) return "1";
	//4、计算,补小数点
	string  res = exponentiationInt(s.c_str(), y);
	if (pos!=0){
		if (pos - int(res.length()) > 0){           //这里先在结果字串前补0
			res = res.insert(0, (pos - int(res.length())), '0');
		}
		res = res.insert((res.length() - pos), 1, '.');//插入小数点
	}
	return res;
}

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