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 生成所有有效数对的答案的方法。
/*
* // This is the custom function interface.
* // You should not implement it, or speculate about its implementation
* class CustomFunction {
* public:
* // Returns f(x, y) for any given positive integers x and y.
* // Note that f(x, y) is increasing with respect to both x and y.
* // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
* int f(int x, int y);
* };
*/
class Solution {
public:
vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
}
};
/**
* This is the declaration of customFunction API.
* @param x int
* @param x int
* @return Returns f(x, y) for any given positive integers x and y.
* Note that f(x, y) is increasing with respect to both x and y.
* i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
*/
func findSolution(customFunction func(int, int) int, z int)(ans [][]int) {
for x:=1;x<=1000;x++{
if customFunction(x,1)>z{
break
}
left,right := 1, 1000
for left<=right{
mid:=(left+right)/2
flag := customFunction(x,mid)
if flag>z{
right = mid-1
}else if flag<z{
left = mid+1
}else{
ans = append(ans, []int{x,mid})
break
}
}
}
return
}
"""
This is the custom function interface.
You should not implement it, or speculate about its implementation
class CustomFunction:
# Returns f(x, y) for any given positive integers x and y.
# Note that f(x, y) is increasing with respect to both x and y.
# i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
def f(self, x, y):
"""
class Solution:
def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:
ans = []
for x in range(1,1001):
if customfunction.f(x, 1) > z:
break
left, right = 1,1000
while left<=right:
mid = (left+right)//2
flag = customfunction.f(x, mid)
if flag > z:
right = mid-1
elif flag < z:
left = mid+1
else:
ans.append([x,mid])
break
return ans
/**
* This is the declaration of customFunction API.
* @param x int
* @param x int
* @return Returns f(x, y) for any given positive integers x and y.
* Note that f(x, y) is increasing with respect to both x and y.
* i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
*/
func findSolution(customFunction func(int, int) int, z int) [][]int {
ans := [][]int{}
x, y := 1, 1000
var v int
for x <= 1000 && y >= 1 {
v = customFunction(x, y)
if v < z {
x++
} else if v > z {
y--
} else {
ans = append(ans, []int{x, y})
x++
}
}
return ans
}
"""
This is the custom function interface.
You should not implement it, or speculate about its implementation
class CustomFunction:
# Returns f(x, y) for any given positive integers x and y.
# Note that f(x, y) is increasing with respect to both x and y.
# i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
def f(self, x, y):
"""
class Solution:
def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:
x, y = 1, 1000
ans = []
while x <= 1000 and y >= 1:
v = customfunction.f(x, y)
if v < z:
x += 1
elif v > z:
y -= 1
else:
ans.append([x, y])
x += 1
return ans
/*
* // This is the custom function interface.
* // You should not implement it, or speculate about its implementation
* class CustomFunction {
* // Returns f(x, y) for any given positive integers x and y.
* // Note that f(x, y) is increasing with respect to both x and y.
* // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
* public int f(int x, int y);
* };
*/
class Solution {
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
List<List<Integer>> result = new ArrayList<>();
int x=1, y=1000;
while (x<=1000 && y>=1) {
if (customfunction.f(x, y) > z) {
y--;
} else if (customfunction.f(x, y) < z) {
x++;
} else {
result.add(Arrays.asList(x, y));
x++;
}
}
return result;
}
}
/*
* // This is the custom function interface.
* // You should not implement it, or speculate about its implementation
* class CustomFunction {
* // Returns f(x, y) for any given positive integers x and y.
* // Note that f(x, y) is increasing with respect to both x and y.
* // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
* public int f(int x, int y);
* };
*/
class Solution {
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
List<List<Integer>> ans = new ArrayList<>();
for (int i = 1; i < 1001; i++) {
for (int j = 1; j < 1001; j++) {
if (customfunction.f(i, j) == z) {
ans.add(Arrays.asList(new Integer[] { i, j }));
}
}
}
return ans;
}
}