我的代码(一直显示错,dev可以运行):
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e4;
int index,a[MAX],ans=0;
void step(int n,int k){
if(n==0){
ans++;
return;
}
index++;
a[index]=1;
step(n-1,k);
index--;
for(int i=1;i<k;i++){
if(n>i){
index++;
a[index]=i+1;
step(n-i-1,k);
index--;
}
}
return;
}
int n,k;
int main(){
cin>>n>>k;
step(n,k);
cout<<ans<<'\n';
return 0;
}
报错信息是:
/tmp/compiler_u7zkxw6v/src:4:5: 错误:‘int index’ redeclared as different kind of entity
4 | int index,a[MAX],ans=0;
| ^~~~~
In file included from /nix/store/79624djlfdc0a6anji2rwqd9p9ycqi8h-glibc-2.34-210-dev/include/string.h:446,
from /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/cstring:42,
from /nix/store/bbmwawbq7wjb54fa35wr72alcm083d1f-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h:48,
from /tmp/compiler_u7zkxw6v/src:1:
/nix/store/79624djlfdc0a6anji2rwqd9p9ycqi8h-glibc-2.34-210-dev/include/strings.h:61:1: 附注:previous declaration ‘const char* index(const char*, int)’
61 | index (const char *__s, int __c) __THROW
| ^~~~~
/tmp/compiler_u7zkxw6v/src: 在函数‘int step(int, int)’中:
/tmp/compiler_u7zkxw6v/src:10:7: 错误:类型没有后自增运算符
10 | index++;
| ^~
/tmp/compiler_u7zkxw6v/src:11:9: 错误:‘int [10000][<未解决的重载函数类型>]’用作数组下标类型无效
11 | a[index]=1;
| ^
/tmp/compiler_u7zkxw6v/src:13:7: 错误:类型没有后自减运算符
13 | index--;
| ^~
/tmp/compiler_u7zkxw6v/src:16:9: 错误:类型没有后自增运算符
16 | index++;
| ^~
/tmp/compiler_u7zkxw6v/src:17:11: 错误:‘int [10000][<未解决的重载函数类型>]’用作数组下标类型无效
17 | a[index]=i+1;
| ^
/tmp/compiler_u7zkxw6v/src:19:9: 错误:类型没有后自减运算符
19 | index--;
| ^~