重载运算符CE,求助……

P1255 数楼梯

Zlc晨鑫 @ 2020-03-24 11:45:19

输入输出运算符重载CE,这是为什么啊?

#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#define maxn 5000 + 5
using namespace std;

struct INT {
    vector<int> v;
    INT(int x = 0) {
        this->v.push_back(x);
    }
    int& operator [](int i) {
        return this->v.at(i);
    }
    int size() {
        return this->v.size();
    }
    void push_back(int i) {
        this->v.push_back(i);
    }
    INT operator +(INT b) {
        INT a = (*this), c;
        int x = 0;
        for (int i = 0, len = min(a.size(), b.size()); i < len; i++) {
            c.push_back(a[i] + b[i] + x);
            x = c[i] / 10;
            c[i] %= 10;
        }
        while (c.size() <= a.size()) c.push_back(a[c.size()]);
        while (c.size() <= b.size()) c.push_back(b[c.size()]);
    }
    istream& operator >>(istream& in) {
        string s;
        in >> s;
        for (int i = s.size() - 1; i >= 0; i--)
            a.push_back(s.at(i) - '0');
        return in;
    }
    ostream& operator <<(ostream& out) {
        for (int i = this->v.size() - 1; i >= 0; i--)
            out << v.at(i);
        return out;
    }
}f[maxn] = {0, 1, 2};

int main() {
    int n;
    cin >> n;
    for (int i = 3; i <= n; i++) {
        f[i] = f[i - 1] + f[i - 2];
    }
    cout << f[n] << '\n';
    return 0;
}

by MY(一名蒟蒻) @ 2020-03-24 11:53:25

朴素算法不到30行您为什么偏偏要装逼...


by WYXkk @ 2020-03-24 11:54:07

@Zlc晨鑫

  1. INT operator +(INT b) 没写 return
  2. 输出流按你那么写的话,应该是 f[n]<<cout<<endl;

by IntrepidStrayer @ 2020-03-24 11:55:39


struct INT{...};
friend istream operator >> (istream& in, INT x){}

by andyli @ 2020-03-24 11:59:29

  1. friend istream& operator >> (istream& in, INT& x)
  2. friend ostream& operator << (ostream& out, const INT& x)

by andyli @ 2020-03-24 12:00:04

3.INT operator +(const INT& b) const


by zhy137036 @ 2020-03-24 12:08:52

@Zlc晨鑫 流必须重载为友元函数


by zhy137036 @ 2020-03-24 12:09:36

推荐这篇洛谷日报:https://www.luogu.com.cn/blog/zhy123456/qian-tan-yun-suan-fu-zhong-zai (逃


by Zlc晨鑫 @ 2020-03-24 12:13:04

@zhy137036 谢谢大佬


上一页 |