#include <stdio.h> //获得一个数的二进制(共V位)表示,把num表示为二进制 void ToBinary(int num,int b[]) { int V; for(int i = 0;i < V;i++) { b[i] = num & 1;//取最低位 num = num >> 1;//右移一位 } } int Count1(int b[]) { int V; int count = 0; for(int i = 0;i < V;i++) { count += b[i]; } return count; } int main() { int V,F; scanf("%d%d",&V,&F); int a[V][F]; for(int i = 0;i < F;i++) { for(int j = 0;j < V;j++) { scanf("%d",&a[i][j]); } } int v,f; int best[V + 1][F + 1]; best[V + 1][F + 1] = {{0}}; int put[V + 1][F + 1]; put[V + 1][F + 1] = {{0}}; for(int v = 1;v <= V;v++) { for(int f = 1;f <= v && f <= F;f++) { //v号花瓶插入f号花 best[v][f] = best[v - 1][f - 1] + a[v - 1][f - 1]; put[v][f] = put[v - 1][f - 1] + (1 << (v - 1)); //v号花瓶不插花更优 if(f < v &&best[v][f] < best[v - 1][f]) { best[v][f] = best[v - 1][f]; put[v][f] = put[v - 1][f]; } } } printf("best = %d\n",best[V][F]); int b[V]; b[V] = {0}; ToBinary(put[V][F],b); for(int v = V - 1,f = F;v >= 0;v--) { if(b[V] == 1) { printf("%d",f); f--; } else { printf("0"); } } return 0; }如何解决报错,正确代码应该怎么写
时间: 2024-01-20 10:02:18 浏览: 35
在C语言中,不能在定义数组时使用花括号直接初始化,因此在你的代码中出现了错误。在定义数组时,你需要使用循环逐个为数组元素赋值,例如:
```c
int best[V + 1][F + 1] = {0};
int put[V + 1][F + 1] = {0};
```
另外,你在函数中引用了没有定义的变量V,应该在main函数中定义V和F的值,并传递给函数。修改后的代码如下:
```c
#include <stdio.h>
//获得一个数的二进制(共V位)表示,把num表示为二进制
void ToBinary(int num, int b[], int V) {
for(int i = 0; i < V; i++) {
b[i] = num & 1; //取最低位
num = num >> 1; //右移一位
}
}
int Count1(int b[], int V) {
int count = 0;
for(int i = 0; i < V; i++) {
count += b[i];
}
return count;
}
int main() {
int V, F;
scanf("%d%d", &V, &F);
int a[V][F];
for(int i = 0; i < F; i++) {
for(int j = 0; j < V; j++) {
scanf("%d", &a[j][i]);
}
}
int best[V + 1][F + 1] = {0};
int put[V + 1][F + 1] = {0};
for(int v = 1; v <= V; v++) {
for(int f = 1; f <= v && f <= F; f++) {
//v号花瓶插入f号花
best[v][f] = best[v - 1][f - 1] + a[v - 1][f - 1];
put[v][f] = put[v - 1][f - 1] + (1 << (v - 1));
//v号花瓶不插花更优
if(f < v && best[v][f] < best[v - 1][f]) {
best[v][f] = best[v - 1][f];
put[v][f] = put[v - 1][f];
}
}
}
printf("best = %d\n", best[V][F]);
int b[V] = {0};
ToBinary(put[V][F], b, V);
for(int v = V - 1, f = F; v >= 0; v--) {
if(b[v] == 1) {
printf("%d", f);
f--;
} else {
printf("0");
}
}
return 0;
}
```
修改后的代码应该就能正常编译和运行了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)