您的位置:首页 > 其它

poj 1001 exponent by zhyh2010

2014-11-17 21:11 267 查看
这是我在poj上面遇到一个变态的题目,本来以为很简单,但是事实证明我错了,这道题很变态!!!在收到第一个WA的时候我第一时间参阅了相关资料,写出最后这个程序的时候借鉴了http://blog.csdn.net/shaw1994/article/details/12273987的思想。
下面说下主要遇到的问题:
1.数据长度的估算,虽然不知道数据长度应该怎么估算,但是我们发现,最终数据的长度最大不超过 len(R)^n;这里 R不计小数点之后一般是5位数据,所以结果最大长度应该只有5^25 = 125 位,再加上 小数点 占一位,所以存储长度取126就可以了。为方便起见我们这里取了
长度为 130.
2.数据输入 0 时候的问题,说实话 这在一开始真没考虑到,但是解决起来也很简单,对m_chushu的每一项判断是否为0就可以了。若为 0 直接输出 0。
3.数据前后需要消除多余的 0 ,这里我们分别从两端开始寻找第一个非 0 项,并记录下当时的位置,以方便我们后续输出数据。这里有个特别情况 ,当结果为 '1.0'时候,我们原先程序给出 ‘1.’ 通过修改 m_end完成debug。
4.这道问题主要考察高精度运算的模拟操作,实际上只需要模拟乘法运算就可以了。
5.我们的数据存储结构为,以95.123 为例,m_cheshu[0]=3,m_cheshu[1]=2,....,m_cheshu[4] = 9.
6.我们的程序写的有点乱,本来想用C++写,写了一半发现,题目需要循环处理数据,不是一次一个的那种,就硬着头皮写了下来,目测写成 C 的形式应该会简洁一些。
7.附上我们一开始写的算法流程:


Source Code
Problem: 1001User: zhyh2010
Memory: 228KTime: 0MS
Language: C++Result: Accepted
Source Code
// header

#include <iostream>
#include <string>
using namespace std;

const int MY_MAX_SIZE = 130;
const int R_size = 5;
const int my_end = -1;

class Exponentiation
{
public:
Exponentiation();
~Exponentiation();

void input();
void output();
void algorithm();

void algorithm_submit();

protected:

bool isChuShuZeros();
private:
short m_temp[MY_MAX_SIZE];
//short m_ori_multiply[R_size];
short m_res[MY_MAX_SIZE];
short m_res_each[MY_MAX_SIZE];
short m_cheshu[R_size];

int m_n;
string m_R;
int m_pos;      // 小数点位置

int m_begin;
int m_end;
};

// implement
Exponentiation::Exponentiation()
{
memset(m_temp, 0, sizeof(m_temp));
memset(m_res_each, my_end, sizeof(m_res_each));
memset(m_res, my_end, sizeof(m_res));
memset(m_cheshu, my_end, sizeof(m_cheshu));

m_pos = 0;
m_n = 0;
m_R = "";

m_begin = 0;
m_end = 0;
}

Exponentiation::~Exponentiation()
{

}

void Exponentiation::input()
{
cin >> m_R >> m_n;
{
m_pos = m_R.find('.');
for (int i = R_size,j = 0; i != -1; --i)
{
if (m_R[i] != '.')
{
m_cheshu[j] = m_R[i] - '0';
++j;
}
}
}
}

void Exponentiation::algorithm_submit()
{
while (cin >> m_R >> m_n)
{
m_pos = m_R.find('.');
for (int i = R_size, j = 0; i != -1; --i)
{
if (m_R[i] != '.')
{
m_cheshu[j] = m_R[i] - '0';
++j;
}
}

if (isChuShuZeros())
{
cout << 0 << endl;
continue;
}

algorithm();

memset(m_temp, 0, sizeof(m_temp));
memset(m_res_each, my_end, sizeof(m_res_each));
memset(m_res, my_end, sizeof(m_res));
memset(m_cheshu, my_end, sizeof(m_cheshu));
}
}

void Exponentiation::output()
{
/*for (int i = MY_MAX_SIZE-1; i != -1; --i)
{*/
/*if (m_res[i] == my_end)
{
continue;
}*/
for (int i = m_begin; i != m_end-1; --i)
{
if (m_res[i]=='.')
{
cout << ".";
}
else
{
cout << m_res[i];
}

}
cout << endl;

}

void Exponentiation::algorithm()
{
//input();

if (m_n == 1)
{
// res = cheshu
int j = 0;
for (int i = R_size; i != -1; --i)
{
if (m_R[i] == '.')
{
m_res[j] = '.';
}
else
m_res[j] = m_R[i] - '0';

++j;
}
}
else
{
// res_each = temp = cheshu
// k = 1
for (int i = 0; i != R_size; ++i)
{
m_res_each[i] = m_cheshu[i];
//m_temp[i] = m_cheshu[i];
}

for (int k = 2; k != m_n + 1; ++k)
{
// multiply
int i = 0;
while (m_res_each[i] != my_end)
{
for (int j = 0; j != R_size; ++j)
{
m_temp[i + j] += m_res_each[i] * m_cheshu[j];

// adjust
if (m_temp[i + j] >= 10)
{
m_temp[i + j + 1] += m_temp[i + j] / 10;
m_temp[i + j] %= 10;
}
}
++i;
}

// update m_res_each
for (int ii = 0; ii != R_size * k; ++ii)
{
m_res_each[ii] = m_temp[ii];
m_temp[ii] = 0;
}
}

// set res
for (int i = 0, j = 0; m_res_each[j] != my_end; ++i)
{
if (i == (R_size - m_pos)*m_n)
{
m_res[i] = '.';
}
else
{
m_res[i] = m_res_each[j];
++j;
}
}
}

// modify 0 in begin
m_begin = MY_MAX_SIZE - 1;
for (int i = MY_MAX_SIZE - 1; i != -1;--i)
{
if (m_res[i] == my_end || m_res[i] == 0)
{
continue;
}
else
{
m_begin = i;
break;
}
}

// modify 0 in the end
m_end = 0;
for (int i = 0; i != MY_MAX_SIZE; ++i)
{
if (m_res[i] == 0)
{
continue;
}
else
{
m_end = i;
break;
}
}

// special occasion  '1.'
if (m_res[m_end] == '.')
{
++m_end;
}

output();
}

bool Exponentiation::isChuShuZeros()
{
for (int i = 0; i != R_size; ++i)
{
if (m_cheshu[i] != 0)
{
return false;
}
}
return true;
}

// test

int main(int argc, char ** argv)
{
Exponentiation instance;
//instance.input();
//instance.algorithm();
//instance.output();
instance.algorithm_submit();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: