64,647
社区成员
发帖
与我相关
我的任务
分享
实验
#include<iostream>
using namespace std;
void func(int n, const int r, int res[], int cnt = 0)
{
if (r == cnt || 0 == n)
{
if (r == cnt)
{
for (int i = 0; i < r; ++i)
{
cout<<res[i];
}
cout<<endl;
}
}
else
{
res[cnt] = n;
func(n - 1, r, res, cnt + 1);
func(n - 1, r, res, cnt);
}
}
int main()
{
int n, r;
cout<<"input n, r :"<<endl;
cin>>n>>r;
if (n < 1 || r < 1 || n < r)
{
return -1;
}
res = new int[r];
func(n, r, res);
delete[]res;
res = NULL;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
void func(int n, const int r, int res[], int cnt = 0)
{
if (r == cnt || 0 == n)
{
if (r == cnt)
{
for (int i = 0; i < r; ++i)
{
cout<<res[i];
}
cout<<endl;
}
}
else
{
res[cnt] = n;
func(n - 1, r, res, cnt + 1);
func(n - 1, r, res, cnt);
}
}
int main()
{
int n, r, *res;
cout<<"input n, r :"<<endl;
cin>>n>>r;
if (n < 1 || r < 1 || n < r)
{
return -1;
}
if (NULL == (res = new int[r]))
{
return -1;
}
func(n, r, res);
delete[]res;
res = NULL;
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void func(int n, const int r, int res[], int cnt)
{
if (r == cnt || 0 == n)
{
if (r == cnt)
{
int i;
for (i = 0; i < r; ++i)
{
printf("%d", res[i]);
}
printf("\n");
}
}
else
{
res[cnt] = n;
func(n - 1, r, res, cnt + 1);
func(n - 1, r, res, cnt);
}
}
int main()
{
int n, r;
int *res;
puts("input n, r :");
scanf("%d%d", &n, &r);
if (n < 1 || r < 1 || n < r)
{
return -1;
}
if (NULL == (res = (int *)malloc(sizeof(int) * r)))
{
return -1;
}
func(n, r, res, 0);
free(res);
res = NULL;
system("pause");
return 0;
}