HDU-1041 Computer Transformation 大數

來源:互聯網
上載者:User

這題打表找下規律就可以了,定義一個變數來表示增量,那麼這個變數的格律就是 add = (add ± 1) * 2

手寫了大數的類,幸好只有-1這個值,這個類是沒定義減法運算的。

代碼如下:

#include <cstdlib>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;typedef long long int Int64;Int64 rec[65];int N;struct BigInteger{    char x[1005];    BigInteger ()    {        memset(x, 0, sizeof (x));        }    BigInteger operator + (BigInteger b);    BigInteger operator * (int m);    void print();}ret[1005], Add;BigInteger BigInteger :: operator + (BigInteger b){    BigInteger ans;    int lena = 0, lenb = 0, len;    for (int i = 1000; i >= 0; --i) {        if (x[i] && !lena) lena = i;        if (b.x[i] && !lenb) lenb = i;        if (lena && lenb) break;    }    len = max(lena, lenb);    for (int i = 0; i <= len; ++i) {        ans.x[i] += x[i] + b.x[i];        if (ans.x[i] >= 10) {            ans.x[i+1] += ans.x[i] / 10;            ans.x[i] %= 10;        }    }    return ans;}BigInteger BigInteger :: operator * (int m){    BigInteger ans;    int len = 0;    for (int i = 1000; i >= 0; --i) {        if (x[i] && !len) len = i;        if (len) break;    }    for (int i = 0; i <= len; ++i) {        ans.x[i] += x[i] * m;        if (ans.x[i] >= 10) {            ans.x[i+1] += ans.x[i] / 10;            ans.x[i] %= 10;        }        }    return ans;}void BigInteger :: print(){    int len = 0;    for (int i = 1000; i >= 0; --i) {        if (x[i] && !len) len = i;        if (len) break;    }     for (int i = len; i >= 0; --i) {        printf("%d", x[i]);        }    puts("");}BigInteger valueof(Int64 obj){    BigInteger ans;    int i = 0;    while (obj) {        ans.x[i] = obj % 10;        obj /= 10;        ++i;    }    return ans;}int main(){    ret[1] = valueof(0), ret[2] = ret[3] = valueof(1);     Add = valueof(0);    for (int i = 4; i <= 1000; ++i) {         Add = (Add + valueof(i & 1 ? -1 : 1)) * 2;         ret[i] = ret[i-1] + Add;    }    while (scanf("%d", &N) == 1) {        ret[N].print();    }    return 0;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.