计算C ++中所有大小为2或3且总和为3的倍数的组
在本教程中,我们将讨论一个程序,以查找大小为2或3且总和为3的倍数的组的数量。
在本教程中,我们将讨论一个程序,以查找大小为2或3且总和为3的倍数的组的数量。
示例
#include<bits/stdc++.h>
using namespace std;
//返回对数
//2或3-
int count_groups(int arr[], int n){
int c[3] = {0}, i;
int res = 0;
for (i=0; i<n; i++)
c[arr[i]%3]++;
res += ((c[0]*(c[0]-1))>>1);
res += c[1] * c[2];
res += (c[0] * (c[0]-1) * (c[0]-2))/6;
res += (c[1] * (c[1]-1) * (c[1]-2))/6;
res += ((c[2]*(c[2]-1)*(c[2]-2))/6);
res += c[0]*c[1]*c[2];
return res;
}
int main(){
int arr[] = {3, 6, 7, 2, 9};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Required number of groups are " << count_groups(arr,n) << endl;
return 0;
}输出结果
Required number of groups are 8
热门推荐
1 训练狗狗搜寻
2 如何训练狗狗装死
3 狗狗训练书
6 怎么训练狗狗拜年
8 狗狗训练游戏
10 狗狗嗅觉训练
11 怎么训练狗狗在笼子里不叫
12 怎么训练狗狗站立走路
13 怎么训练狗狗在马桶上
14 狗狗尿尿怎么训练
15 训练狗狗用品
16 泰迪狗狗的训练方法大小便
17 训练狗狗可以打吗
18 王曼丽的狗狗训练