zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Kubernetes滚动更新介绍及使用-minReadySeconds
    Office 365 – SharePoint 2013 Online 与Office相关的应用
    Office 365 – SharePoint 2013 Online 中添加域和域名
    Office 365
    Office 365 – SharePoint 2013 Online 之WebPart开发、部署教程
    Office 365
    Office 365
    信仰坚持!程序员也有春天!
    SharePoint 2013 日历重叠功能简介
    SharePoint 2013 使用 PowerShell 更新用户
  • 原文地址:https://www.cnblogs.com/Lyush/p/2630849.html
Copyright © 2011-2022 走看看