Exponentiation
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 Rn 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.
这个题总体比较简单,我没完全按照给的数据范围写代码,用字符串就可以计算更大数据范围
记录小数点个数转化为大整数计算
我是菜鸡,,还没学快速幂,,就直接用递归计算了
#include<iostream>
#include<string>
using namespace std;
int getposition(const string &str) // 获得小数点的位置
{
for (int i = 0; i < str.size(); i++) if (str[i] == '.') return i;
return -1;
}
class LongNumber // 用一个类来记录这个数字
{
string number; int position;
string calculate(int n); // 计算规则
public:
LongNumber(string str) { // 将浮点型转化为整型储存
position = getposition(str);
if (position >= 0) { number = str.erase(position, 1); position = str.size() - position; }
else number = str;
while (number[0] == '0') number.erase(0,1);
}
string dealAnswer (int n); // 处理计算结果
};
int main()
{
string r; int n;
while (cin >> r >> n)
{
LongNumber ln(r);
cout << ln.dealAnswer(n)<<endl;
}
return 0;
}
void addstr(string &eachAns,int offset, string &ans)
{
eachAns.append(offset, '0'); // 对齐
int rest = 0, carry = 0;
while (ans.size() < eachAns.size())ans.insert(ans.begin(), '0');
for (int i = ans.size() - 1; i >= 0; i--) {
rest = (ans[i] - '0') + (eachAns[i] - '0') + carry;
carry = rest / 10; rest %= 10;
ans[i] = char(rest + '0');
}
// 处理进位
if (carry) ans.insert(ans.begin(), char(carry + '0'));
}
string LongNumber::calculate(int n) // 递归计算
{
if (n == 0) return "1";
else {
string haveCal = calculate(n - 1);
string ans;
for (int i = number.size() - 1; i >= 0; i--)
{
string eachAns;
int rest = 0, carry = 0;
for (int j = haveCal.size() - 1; j >= 0; j--)
{
rest = (haveCal[j] - '0') * (number[i] - '0') + carry;
carry = rest / 10; rest %= 10;
eachAns = char(rest + '0') + eachAns;
}
if(carry) eachAns = char(carry + '0') + eachAns;
addstr(eachAns, number.size() - 1 - i, ans);
}
return ans;
}
}
string LongNumber::dealAnswer(int n)
{
string ans = calculate(n);
int offposition = n * position;
if (offposition > 0) {
if (offposition > ans.size())
{
int zeros = offposition - ans.size();
while (zeros--) ans.insert(ans.begin(), '0');
ans.insert(ans.begin(), '.');
}
else ans.insert(ans.end() - offposition, '.');
int pos = getposition(ans);
// 这里算个坑点吧,用对拍对了几百组数据才发现之前会抹掉整数后面的零....
for (int i = ans.size() - 1; i >= pos && pos >= 0; i = ans.size() - 1) {
if (ans[i] == '0' || ans[i] == '.') ans.erase(ans.end() - 1);
else break;
}
}
return ans;
}
看了一点别人写的代码,这样计算幂次会快很多,,,
这个直接不用再用string多此一举地对齐,效率会高很多