zoukankan      html  css  js  c++  java
  • 华丽的大数类,华丽的AC:Integer Inquiry

    参考《算法竞赛入门经典》(刘汝佳)中大数类的实现,终于 AC 了;

    使用大数类写着感觉很轻松,AC 得也很轻松。。

    不过还没找到 C 代码 WA 的原因。

     1 /* UVa 424 - Integer Inquiry */
    2 # include <iostream>
    3 # include <string>
    4 # include <cstring>
    5 # include <algorithm>
    6
    7 using namespace std;
    8
    9 const int maxn = 105;
    10
    11 struct bign{
    12 int len, s[maxn];
    13 bign() {memset(s, 0, sizeof(s)); len = 1;}
    14 bign operator = (const char * num)
    15 {
    16 len = strlen(num);
    17 for (int i = 0; i < len; ++i)
    18 s[i] = num[len-i-1] - '0';
    19 return *this;
    20 }
    21 string str() const
    22 {
    23 string res = "";
    24 for (int i = 0; i < len; ++i)
    25 res = (char)(s[i]+'0') + res;
    26 if (res == "") res = "0";
    27 return res;
    28 }
    29 bign operator + (const bign& b) const
    30 {
    31 bign c;
    32 c.len = 0;
    33 for (int i = 0, g = 0; g || i < max(len, b.len); ++i)
    34 {
    35 int x = g;
    36 if (i < len) x += s[i];
    37 if (i < b.len) x += b.s[i];
    38 c.s[c.len++] = x % 10;
    39 g = x / 10;
    40 }
    41 return c;
    42 }
    43 };
    44
    45 istream& operator >> (istream &in, bign& x)
    46 {
    47 string s;
    48 in >> s;
    49 x = s.c_str();
    50 return in;
    51 }
    52 ostream& operator << (ostream &out, const bign& x)
    53 {
    54 out << x.str();
    55 return out;
    56 }
    57
    58 int main()
    59 {
    60 bign a, sum;
    61
    62 while (cin >> a)
    63 {
    64 if (a.len == 1 && a.s[0] == 0)
    65 break;
    66 sum = sum + a;
    67 }
    68 cout << sum << endl;
    69
    70 return 0;
    71 }

    一个好的大数类模板。

  • 相关阅读:
    Python中replace 不起作用的问题
    java 获取视频时长、大小
    MySQL 自定义排序
    加 synchronized 关键字进行同步
    SQL 查询当前周的开始、结束日期
    Java 按照一定的规则生成递增的编号
    Java中BigDecimal的8种舍入模式
    Lamada 表达式之 sort 排序
    搭建Java环境
    初识JAVA(学习记录)
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2402427.html
Copyright © 2011-2022 走看看