C++ 不可分子集-Hackerrank
我正试图解决Hackerrank的不可分子集问题。我试着使用这样的想法,如果a和b的和可以被k整除,那么a%k+b%k=k,但是,它的效果不是很好。 以下是我到目前为止所写的内容:C++ 不可分子集-Hackerrank,c++,algorithm,math,C++,Algorithm,Math,我正试图解决Hackerrank的不可分子集问题。我试着使用这样的想法,如果a和b的和可以被k整除,那么a%k+b%k=k,但是,它的效果不是很好。 以下是我到目前为止所写的内容: #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() {
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
int k;
cin >> n;
cin >> k;
int j;
vector<int>numbers;
vector<int>remainders;
for(int i = 0; i < n; i++) {
int z;
cin >> z;
numbers.push_back(z);
}
for (vector<int>::iterator it = numbers.begin(); it != numbers.end(); it++) {
j = *it % k;
remainders.push_back(j);
}
for(vector<int>::iterator it2 = remainders.begin(); it2 != remainders.end(); it2++) {
int remainderCount = 0;
int otherRemainderCount = 0;
otherRemainderCount = std::count(remainders.begin(), remainders.end(), k-*it2);
remainderCount = std::count(remainders.begin(), remainders.end(), *it2);
if (remainderCount > otherRemainderCount) {
theChosenOne = *it2;
} else if (otherRemainderCount > remainderCount) {
theChosenOne = k-*it2;
}
cout << theChosenOne << endl;
}
return 0;
}
我为余数创建了一个向量,并使用std::cout函数来找出哪个余数在向量中出现得更多。如果K是5,*it2=4,K-*it2=1。如果*it2出现更多次,那么我会选择*it2。否则,我会选择k-*it2。您的解决方案看起来走上了正确的轨道,但需要进行一些更改 您基本上需要将数组中的数字散列到正确的位置 将数组rem[k]初始化为0 迭代数组中的n个数字,并执行以下操作:
rem[array[i]%k]++;
现在只需处理rem[]数组,即可找到最大子集。rem数组的大小为最大k这似乎有效
#include <stdio.h>
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int main() {
int n, k, a, total = 0;
scanf("%d %d", &n, &k);
int mods[k];
for (int i = 0; i < k; i++)
mods[i] = 0;
while (n--) {
scanf("%d", &a);
mods[a % k]++;
}
// can only have 1 value congruent to 0 mod k
total += min(1, mods[0]);
// if even, can only have 1 value congruent to k/2 mod k
if (k % 2 == 0)
total += min(1, mods[k / 2]);
// for all others, pick max of those k and n-k mod k
for (int d = 1; d < (k + 1) / 2; d++) { // for all others,
total += max(mods[d], mods[k - d]);
}
printf("%d", total);
return 0;
}
你已经用调试器检查过你的程序了吗?我真的不喜欢HackerRank和类似的东西。他们鼓励可爱的一次性代码,而这些代码是不可维护的。@MirunaCamelia是的,它不起作用。这实际上太模糊了。请在您的问题中准确地描述它是如何工作的,并告诉我们当您使用调试器检查代码时,您观察到了什么。@MartinBonner您会推荐什么替代方案?@0x499602D2:一本合适的课程或书。为什么直到fori=1中的k/2;我已经在解释中解释了原因。假设k=7,我试图在rem[1]和rem[6],rem[2]和rem[5],rem[3]和rem[4]之间找到max。因此,我只需要写一个循环,直到k/2。这个算法没有给出正确的答案。例如,对于输入10 4和设置1 2 3 4 5 6 7 8 9 10,它会给出答案3,但正确的答案是5@tzanou我的代码给出的答案是5。请在评论之前正确测试!!快乐编码请发布相关解释,而不仅仅是代码。
int main() {
int n,k;
cin>>n>>k;
vector <int> a(n);
vector <int> r(k,0);
for(int i=0;i<n;i++)
{
cin>>a[i];
r[a[i]%k]++;
}
int ctr=min(1,r[0]);
for(int a=1;a<(k/2+1);a++)
{
if(a!=k-a)
ctr+=max(r[a],r[k-a]);
}
if(k%2==0&&r[k/2]!=0)
ctr++;
cout<<ctr;
return 0;
}
#include <stdio.h>
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int main() {
int n, k, a, total = 0;
scanf("%d %d", &n, &k);
int mods[k];
for (int i = 0; i < k; i++)
mods[i] = 0;
while (n--) {
scanf("%d", &a);
mods[a % k]++;
}
// can only have 1 value congruent to 0 mod k
total += min(1, mods[0]);
// if even, can only have 1 value congruent to k/2 mod k
if (k % 2 == 0)
total += min(1, mods[k / 2]);
// for all others, pick max of those k and n-k mod k
for (int d = 1; d < (k + 1) / 2; d++) { // for all others,
total += max(mods[d], mods[k - d]);
}
printf("%d", total);
return 0;
}