将这个C语言改写成VBA
从1到N,这N个数,从中选取M个出来,请输出每一种组合情况
组合结果从单元格A1开始输出
#include <stdio.h>
int a[10];
int main()
{
void f(int start,int has,int m,int n);
int n,m;
printf("请输入组合上下标中间用空格隔开 ");
scanf("%d%d",&n,&m);
f(0,0,m,n);
return 0;
}
void f(int start,int has,int m,int n)
{
int i;
if (has==m)
{
printf("%d ",a[0]);
for (i=1;i<m;i++)
printf("%d ",a[i]);
printf("\n");
return;
}
for (i=start+1;i+m<=n+has+1;i++)
{
a[has]=i;
f(i,has+1,m,n);
}
}
从1到N改成VBA输出
答案:1 悬赏:40 手机版
解决时间 2021-03-11 14:04
- 提问者网友:最爱你的唇
- 2021-03-10 14:26
最佳答案
- 五星知识达人网友:污到你湿
- 2021-03-10 14:56
Dim a(10) As Integer, k As Long
Sub main()
Dim m As Integer, n As Integer
n = InputBox("请输入组合下标n")
m = InputBox("请输入组合上标m")
f 0, 0, m, n
End Sub
Sub f(ByVal start As Integer, ByVal has As Integer, ByVal m As Integer, ByVal n As Integer)
Dim i As Integer
If has = m Then
k = k + 1
For i = 1 To m
Cells(k, i) = a(i - 1)
Next
Exit Sub
End If
For i = start + 1 To n - m + has + 1
a(has) = i
f i, has + 1, m, n
Next
End Sub
Sub main()
Dim m As Integer, n As Integer
n = InputBox("请输入组合下标n")
m = InputBox("请输入组合上标m")
f 0, 0, m, n
End Sub
Sub f(ByVal start As Integer, ByVal has As Integer, ByVal m As Integer, ByVal n As Integer)
Dim i As Integer
If has = m Then
k = k + 1
For i = 1 To m
Cells(k, i) = a(i - 1)
Next
Exit Sub
End If
For i = start + 1 To n - m + has + 1
a(has) = i
f i, has + 1, m, n
Next
End Sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯