问
  • 板块学术版
  • 楼主fkxr
  • 当前回复3
  • 已保存回复4
  • 发布时间2024/6/23 16:56
  • 上次更新2024/6/23 20:35:00
查看原帖
问
995934
fkxr楼主2024/6/23 16:56

求下面这段py代码的输出

def f(x, y):  
    if x == 1 or y == 1:  
        return x if x > y else y  
    else:  
        return f(x-1, y) + f(x, y-1) + x + y  
  
print(f(114, 514))

这是我写的c++代码,但不对求正确的输出

#include <bits/stdc++.h>
using namespace std;
#define N 5005

struct longerint {
	int len, a[N];
	longerint(int x = 0) {
		memset(a, 0, sizeof(a));
		for (len = 1; x; len++) {
			a[len] = x % 10, x /= 10;
		}
	}
	int &operator[](int i) {
		return a[i];
	}
	void flatten(int L) {
		len = L;
		for (int i = 1; i <= len; i++) {
			a[i + 1] += a[i] / 10, a[i] %= 10;
		}
		for (; !a[len];) {
			len--;
		}
	}
	void print() {
		for (int i = max(len, 1); i >= 1; i--) {
			printf("%d", a[i]);
		}
	}
};

longerint operator+(longerint a, longerint b) {
	longerint c;
	int len = max(a.len, b.len);
	for (int i = 1; i <= len; i++) {
		c[i] += a[i] + b[i];
	}
	c.flatten(len + 1);
	return c;
}

longerint operator*(longerint a, int b) {
	longerint c;
	int len = a.len;
	for (int i = 1; i <= len; i++) {
		c[i] = a[i] * b;
	}
	c.flatten(len + 11);
	return c;
}
bool vis[550][550];
longerint a[150][600];
longerint f(int x, int y) {
	if (x == 1 || y == 1) {
		longerint ans(max(x, y));
		return ans;
	}
	if (vis[x][y]) {
		return a[x][y];
	}
	vis[x][y] = 1;
	longerint xx(x);
	longerint yy(y);
	a[x][y] = f(x - 1, y) + f(x, y - 1) + xx + yy;
	return a[x][y];
}

int main() {
	f(114, 514).print();
	return 0;
}
2024/6/23 16:56
加载中...