题解 | #农场牛类别匹配#
农场牛类别匹配
https://www.nowcoder.com/practice/270db1e1d65b4366a49a517ec7822912
题目考察的知识点
考察数组的操作
题目解答方法的文字分析
根据题意,使用双重遍历去检查每个组合是否可以符合要求即可。
本题解析所用的编程语言
使用Java解答
完整且正确的编程代码
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param breeds int整型一维数组
* @param target_sum int整型
* @return int整型
*/
public int countMatchingPairs (int[] breeds, int target_sum) {
// write code here
int count = 0;
for (int i = 0; i < breeds.length; i++) {
for (int j = i + 1; j < breeds.length; j++) {
if ((breeds[i] + breeds[j]) == target_sum)
count++;
}
}
return count;
}
}

