classSolution { public: intaverageValue(vector<int>& nums){ int total = 0, k = 0; for (int a : nums) { if (a % 6 == 0) { total += a; k++; } } return k > 0 ? total / k : 0; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { publicintaverageValue(int[] nums) { inttotal=0, k = 0; for (int a : nums) { if (a % 6 == 0) { total += a; k++; } } return k > 0 ? total / k : 0; } }
[sol1-Python3]
1 2 3 4 5 6 7 8 9
classSolution: defaverageValue(self, nums: List[int]) -> int: total = 0 k = 0 for a in nums: if a % 6 == 0: total += a k += 1 return total // k if k > 0else0
[sol1-Go]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
funcaverageValue(nums []int)int { total := 0 k := 0 for _, a := range nums { if a % 6 == 0 { total += a k++ } } if k > 0 { return total / k } else { return0 } }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11
var averageValue = function(nums) { let total = 0; let k = 0; for (let a of nums) { if (a % 6 === 0) { total += a; k++; } } return k > 0 ? Math.floor(total / k) : 0; };
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13
publicclassSolution { publicintAverageValue(int[] nums) { int total = 0, k = 0, n = nums.Length; for (int i = 0; i < n; i++) { int a = nums[i]; if (a % 6 == 0) { total += a; k++; } } return k > 0 ? total / k : 0; } }
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11
intaverageValue(int* nums, int numsSize){ int total = 0, k = 0; for (int i = 0; i < numsSize; i++) { int a = nums[i]; if (a % 6 == 0) { total += a; k++; } } return k > 0 ? total / k : 0; }