Найти количество подмножеств с заданным средним значением в C
даны n уникальных чисел и нужно найти количество подмножеств из них, среднее значение которых будет делиться на другое заданное число.
Что я уже пробовал:
Я искал алгоритм и к сожалению просто нашел
Структуры данных и алгоритм: найти подмножество с заданным средним значением[^]
который написан на языке java, и я не могу его понять.
может ли кто-нибудь показать мне реализацию этого алгоритма на языке Си ? или объясните это еще немного, пожалуйста.
Richard MacCutchan
Попробуйте сами подумать о проблеме. Запишите некоторые цифры и подумайте, как вы могли бы сгруппировать их так, чтобы они были близки к общему среднему значению.
Rick York
Here are some things to consider. I will assume you know to find the average of some numbers and how to check divisibility. If not, you need to figure those out first. The next thing is figuring out how to find a subset. That is probably the trickiest aspect of the problem. The link you posted shows one way and that code can be transformed into C fairly easily. Since you don't understand it then you should try an alternative. Look for code to find permutations and combinations. Those are how a subset is usually determined. There are several ways. The way I sometimes do it is a brute force method and works OK when you have less than 64 elements in the set. I "map" each item to a bit and then increment up to the binary limit of that many items. If a bit is on then that means that item is a member of that particular subset. As an example, let's say there are four items in a set. The count will range from 0 to 15 (two to the fourth power minus one). When the step counter is at five the binary value is 0101 so that means items 2 and 0 are in the subset. This is not a particularly efficient method but it works fairly well with small sets, those with less than 64 items.
Member 13606974
О Боже, спасибо, извините, что я не родной и, возможно, именно поэтому я не могу понять алгоритм в приведенной выше ссылке, поэтому попытался узнать, что это код, который был java, и снова я не был знаком с java, но ваша помощь имеет смысл, я попробую это сделать.