poj 1001 exponent by zhyh2010

来源:互联网 发布:洛阳平安数据科技招聘 编辑:程序博客网 时间:2024/06/08 02:50

    这是我在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: 1001 User: zhyh2010Memory: 228K Time: 0MSLanguage: 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;};// implementExponentiation::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 = cheshuint j = 0;for (int i = R_size; i != -1; --i){if (m_R[i] == '.'){m_res[j] = '.';}elsem_res[j] = m_R[i] - '0';++j;}}else{// res_each = temp = cheshu // k = 1for (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){// multiplyint 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];// adjustif (m_temp[i + j] >= 10){m_temp[i + j + 1] += m_temp[i + j] / 10;m_temp[i + j] %= 10;}}++i;}// update m_res_eachfor (int ii = 0; ii != R_size * k; ++ii){m_res_each[ii] = m_temp[ii];m_temp[ii] = 0;}}// set resfor (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 beginm_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 endm_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;}// testint main(int argc, char ** argv){Exponentiation instance;//instance.input();//instance.algorithm();//instance.output();instance.algorithm_submit();}
0 0
原创粉丝点击