interface CustomFunction {
public:
// Returns some positive integer f(x, y) for two positive integers x and y based on a formula.
int f(int x, int y);
};
你的解决方案将按如下规则进行评判:
判题程序有一个由 CustomFunction 的 9 种实现组成的列表,以及一种为特定的 z 生成所有有效数对的答案的方法。
根据题目给出的 x 和 y 的取值范围,枚举所有的 x, y 数对,保存满足 f(x,y)=z 的数对,最后返回结果。
[sol1-Python3]
1 2 3 4 5 6 7 8 9
# 超时 classSolution: deffindSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]: ans = [] for x inrange(1, 1001): for y inrange(1, 1001): if customfunction.f(x, y) == z: ans.append([x, y]) return ans
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public: vector<vector<int>> findSolution(CustomFunction& customfunction, int z) { vector<vector<int>> res; for (int x = 1; x <= 1000; x++) { for (int y = 1; y <= 1000; y++) { if (customfunction.f(x, y) == z) { res.push_back({x, y}); } } } return res; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public List<List<Integer>> findSolution(CustomFunction customfunction, int z) { List<List<Integer>> res = newArrayList<List<Integer>>(); for (intx=1; x <= 1000; x++) { for (inty=1; y <= 1000; y++) { if (customfunction.f(x, y) == z) { List<Integer> pair = newArrayList<Integer>(); pair.add(x); pair.add(y); res.add(pair); } } } return res; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13
publicclassSolution { public IList<IList<int>> FindSolution(CustomFunction customfunction, int z) { IList<IList<int>> res = new List<IList<int>>(); for (int x = 1; x <= 1000; x++) { for (int y = 1; y <= 1000; y++) { if (customfunction.f(x, y) == z) { res.Add(new List<int> {x, y}); } } } return res; } }
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int** findSolution(int (*customFunction)(int, int), int z, int* returnSize, int** returnColumnSizes) { int **res = (int **)malloc(sizeof(int *) * 1000 * 1000); int pos = 0; for (int x = 1; x <= 1000; x++) { for (int y = 1; y <= 1000; y++) { if (customFunction(x, y) == z) { res[pos] = (int *)malloc(sizeof(int) * 2); res[pos][0] = x, res[pos][1] = y; pos++; } } } *returnSize = pos; *returnColumnSizes = (int *)malloc(sizeof(int) * pos); for (int i = 0; i < pos; i++) { (*returnColumnSizes)[i] = 2; } return res; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11
var findSolution = function(customfunction, z) { const res = []; for (let x = 1; x <= 1000; x++) { for (let y = 1; y <= 1000; y++) { if (customfunction.f(x, y) === z) { res.push([x, y]); } } } return res; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10
funcfindSolution(customFunction func(int, int)int, z int) (ans [][]int) { for x := 1; x <= 1000; x++ { for y := 1; y <= 1000; y++ { if customFunction(x, y) == z { ans = append(ans, []int{x, y}) } } } return }
classSolution: deffindSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]: ans = [] y = 1000 for x inrange(1, 1001): while y and customfunction.f(x, y) > z: y -= 1 if y == 0: break if customfunction.f(x, y) == z: ans.append([x, y]) return ans
[sol3-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: vector<vector<int>> findSolution(CustomFunction& customfunction, int z) { vector<vector<int>> res; for (int x = 1, y = 1000; x <= 1000 && y >= 1; x++) { while (y >= 1 && customfunction.f(x, y) > z) { y--; } if (y >= 1 && customfunction.f(x, y) == z) { res.push_back({x, y}); } } return res; } };
[sol3-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { public List<List<Integer>> findSolution(CustomFunction customfunction, int z) { List<List<Integer>> res = newArrayList<List<Integer>>(); for (intx=1, y = 1000; x <= 1000 && y >= 1; x++) { while (y >= 1 && customfunction.f(x, y) > z) { y--; } if (y >= 1 && customfunction.f(x, y) == z) { List<Integer> pair = newArrayList<Integer>(); pair.add(x); pair.add(y); res.add(pair); } } return res; } }
[sol3-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicclassSolution { public IList<IList<int>> FindSolution(CustomFunction customfunction, int z) { IList<IList<int>> res = new List<IList<int>>(); for (int x = 1, y = 1000; x <= 1000 && y >= 1; x++) { while (y >= 1 && customfunction.f(x, y) > z) { y--; } if (y >= 1 && customfunction.f(x, y) == z) { res.Add(new List<int> {x, y}); } } return res; } }
int** findSolution(int (*customFunction)(int, int), int z, int* returnSize, int** returnColumnSizes) { int **res = (int **)malloc(sizeof(int *) * 1000 * 1000); int pos = 0; for (int x = 1, y = 1000; x <= 1000 && y >= 1; x++) { while (y >= 1 && customFunction(x, y) > z) { y--; } if (y >= 1 && customFunction(x, y) == z) { res[pos] = (int *)malloc(sizeof(int) * 2); res[pos][0] = x, res[pos][1] = y; pos++; } } *returnSize = pos; *returnColumnSizes = (int *)malloc(sizeof(int) * pos); for (int i = 0; i < pos; i++) { (*returnColumnSizes)[i] = 2; } return res; }
[sol3-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12
var findSolution = function(customfunction, z) { const res = []; for (let x = 1, y = 1000; x <= 1000 && y >= 1; x++) { while (y >= 1 && customfunction.f(x, y) > z) { y--; } if (y >= 1 && customfunction.f(x, y) === z) { res.push([x, y]); } } return res; };
[sol3-Golang]
1 2 3 4 5 6 7 8 9 10 11
funcfindSolution(customFunction func(int, int)int, z int) (ans [][]int) { for x, y := 1, 1000; x <= 1000 && y > 0; x++ { for y > 0 && customFunction(x, y) > z { y-- } if y > 0 && customFunction(x, y) == z { ans = append(ans, []int{x, y}) } } return }