![]() | |
| |||||||
| Home | Register | Blogs | FAQ | Members List | Calendar | Downloads | Arcade | Mark Forums Read |
| Algorithms The source of algorithms for your project |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Дошкольник | Я тут написал небольшую прогу которая последовательно выводит все сочетания из n цифр по m. Алгоритм работает как счетчик, т.е. крутит последнее число, потом предпоследнее итд, при этом делает кое какие проверки. Вообщем: Code: /* Генерация сочетаний из n цифр по m.
abyss 11:54 AM 5/12/02
*/
#include <stdio.h>
const n=12;
const m=5;
void main(){
int i,j;
int v[m+1]; // we want to use v[1]..v[m]
bool b; // so declare it as v[m+1]
int count=0;
for(i=1;i<=m;i++)v[i]=i; // initialization
do {
for (;v[m]<=n;v[m]++){ // increment the last element
for (j=1;j<=m;j++)printf("%d ",v[j]);
printf("\n");
count++;
}
/* the last element has reached the right boundary, so make
arrangements and continue */
b=false;
for (i=m-1;i>0;i--)
if(b=v[i]+1<=n-(m-i)) break;
v[i]++;
/* v[i] incremented, now arrange elements v[j],j>i */
for (j=i+1;j<=m;j++)v[j]=v[j-1]+1;
} while (b);
printf ("Count=%d\n",count);
} напишет свою версию, потом сравним, оценим сложности алгоритмов итд.
__________________ [x]-=-[ ]-=-[x] |
| | |
| | #2 | |
| The Reloaded Join Date: Jan 2002 Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,166
Rep Power: 7 Reputation:
34 | Quote:
![]()
__________________ Сайт армянских маньяков | |
| | |
| | #3 |
| Игроман Заядлый - 1шт. ;) | i mojet vsetaki schetchiki nachnesh` s nulya kak prinyato v C/C++???? Eto nasledie ot paskalya??? |
| | |
| | #4 |
| Младенец Join Date: May 2002 Location: Yerevan
Posts: 5
Rep Power: 0 Reputation:
10 | Code:
#include <iostream>
#include <vector>
using namespace std;
void print(const vector<int>& a)
{
for (unsigned i = 0; i< a.size(); i++)
{
cout << a[i] << ' ';
}
cout << endl;
}
void perm(vector<int>& a, int n, int m, int first, int ind)
{
if (ind > m)
{
print (a);
return;
}
for (int i = first; i <= n - (m - ind); i++)
{
a[ind - 1] = i;
perm (a, n, m, i + 1, ind + 1);
}
}
void print_perm (int n, int m)
{
vector<int> a;
a.resize (m);
perm(a, n, m, 1, 1);
}
int main()
{
print_perm(12, 5);
return 0;
} |
| | |
| | #5 | ||
| The Reloaded Join Date: Jan 2002 Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,166
Rep Power: 7 Reputation:
34 | Quote:
__________________ Сайт армянских маньяков | ||
| | |