以下是使用 Python 解决此问题的代码: ```python def count_happy_pairs(t, n, c_str): count = 0 for i in range(n): for j in range(n): is_happy = True for k in range(min(i, j), max(i, j) - 1): if (i < j and c_str[k]!= '<') or (i > j and c_str[k]!= '>'): is_happy = False break if is_happy and i!= j: count += 1 return count t = int(input()) for _ in range(t): n = int(input()) c_str = input() print(count_happy_pairs(t, n, c_str)) ``` 这段代码通过双重循环遍历所有可能的数对 `(i, j)`,然后对于每个数对检查它们之间的比较结果是否符合更开心的条件,如果符合则计数器加 1。 [2024-11-02 15:47:11 | AI写代码神器 | 226点数解答]