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 yongwangzhiqian at 2013-08-05 22:41:42 on Problem 1001
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <sstream>
#include <limits>
#include <string>
#include <cmath>
using namespace std;

string power(string &s, int n)
{
	int h,l;
	if (s.find('.') == string::npos) {
		s += '.';
	}

	for (h = s.size() - 1; h >= 0 && s[h] == '0'; h--) {
	}
	for (l = 0; l <= s.size() - 1 && s[l] == '0'; l++) {
	}
	
	if (s[l] == '.') {
		if (l == 0) {
			string t = "0";
			t += s.substr(0, h + 1);
			s = t;
		} else {
			s = s.substr(l - 1, h + 1 - (l - 1));
		}
	} else {
		s = s.substr(l, h + 1 - l);
	}


	int dot_size = (s.size() - s.find('.') - 1) * n;  //结果中小数的位数
	string tmp_result;
	string result = s;

	if (n == 1) {
		if (result[0] == '0') {
			result = result.substr(1, result.size() - 1);
		}
		if (dot_size == 0) {
			result = result.substr(0, result.size() - 1);
		}
		return result;
	}

	for (; n > 1; n--) {
		tmp_result = result;
		string temporary = "";
		int base = 0;
		int up = 0;
		for (string::const_reverse_iterator iter_t = tmp_result.rbegin(); iter_t != tmp_result.rend(); ++iter_t) {
			if (*iter_t == '.') {
				continue;
			}

			if (*iter_t == '0') {
				base++;
				continue;
			}

			string tmp = "";

			for (int i = 0; i < base; i++) {
				tmp += '0';
			}
			base++;

			up = 0;
			for (string::const_reverse_iterator iter_s = s.rbegin(); iter_s != s.rend(); ++iter_s) {
				if (*iter_s != '.') {
					int r = (*iter_t - '0') * (*iter_s - '0') + up;
					if (r == 0) {
						tmp += '0';
					} else {
						tmp += r % 10 + '0';
					}
					up = r / 10;
				}
			}

			if (up != 0) {
				tmp += '0' + up;
			}

			reverse(tmp.begin(), tmp.end());
			int size;
			int mutex = 0;
			if (temporary.size() < tmp.size()) {
				size = temporary.size();
				mutex = 1;
			} else {
				size = tmp.size();
			}

			up = 0;
			for (int i = temporary.size() - 1, j = tmp.size() - 1; i >= 0 && j>= 0; i--, j--) {
				int r = (temporary[i] - '0') + (tmp[j] - '0') + up;
				temporary[i] = r % 10 + '0';
				up = r / 10;
			}

			if (mutex == 0) {
				for (int i = temporary.size() - tmp.size() - 1; i >= 0 && up != 0; i--) {
					int r = (temporary[i] - '0') + up;
					temporary[i] = r % 10 + '0';
					up = r / 10;
				}

				if (up != 0) {
					string t = "";
					t += up + '0';
					t += temporary;
					temporary = t;
				}
			} else {
				string t;
				for (int i = tmp.size() - temporary.size() - 1; i >= 0; i--) {
					int r = (tmp[i] - '0') + up;
					t += r % 10 + '0';
					up = r / 10;
				}


				reverse(t.begin(), t.end());
				t += temporary;
				if (up != 0) {
					temporary = "";
					temporary += up + '0';
					temporary += t;
				} else {
					temporary = t;
				}
			}
			
		}

		result = temporary;
	}

	if (dot_size == 0) {
		return result;
	}

	if (result.size() < dot_size) {
		string t = ".";
		for (int i = 0; i < dot_size - result.size(); i++) {
			t += '0';
		}
		t += result;
		return t;
	} 
	result.insert(result.size() - dot_size, 1, '.');
	
	return result;
}

int main(void)
{
	string s;
	int n;
	//vector<string> result;
	while (cin >> s >> n) {
		float input;
		istringstream input_value(s);
		input_value >> input;
		if (input > 99.999 || fabs(input - 99.999) <= numeric_limits<float>::epsilon() || input < 0 || fabs(input) <= numeric_limits<float>::epsilon() || n <= 0 || n > 25) {
			cerr << "wrong input!" << endl;
			exit(-1);
		} else {
			s = power(s, n);
			cout << s << endl;
//			result.push_back(power(s, n));
		}
	}

	/*for (vector<string>::const_iterator iter = result.begin(); iter != result.end(); ++iter) {
		cout << *iter << 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