• HDU1041 Computer Transformation 大数


    这题打表找下规律就可以了,定义一个变量来表示增量,那么这个变量的格律就是 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;
    }
  • 相关阅读:
    ViewState
    jar包签名
    Eclipse打JAR包引用的第三方JAR包找不到 问题解决
    java项目打jar包
    像VS一样在Eclipse中使用(拖拉)控件
    Myeclipse buildpath 加server library
    nativeswing的关闭问题 当出现Socket连接未断开错误
    Windows 7 配置jdk 1.7环境变量
    myeclipse添加server library
    RichFaces 大概
  • 原文地址:https://www.cnblogs.com/Lyush/p/2630849.html
Copyright © 2020-2023  润新知