int c[m+1]={0};改为C语言
时间: 2024-02-22 17:00:17 浏览: 21
在C语言中,数组的长度必须是一个常量,而不能是一个变量。因此,如果要定义一个长度为m+1的数组,可以使用动态内存分配的方式来实现。代码如下:
```
int *c = (int*)malloc((m+1)*sizeof(int));
memset(c, 0, (m+1)*sizeof(int)); //初始化数组元素为0
```
其中,malloc函数可以动态分配一段内存,(m+1)*sizeof(int)表示需要分配的内存大小,单位是字节。需要将返回的指针强制转换为int类型的指针,才能使用。memset函数可以将一段内存中的所有字节都设置为指定的值,这里将数组元素都设置为0。使用完数组后,需要使用free函数释放内存,避免内存泄漏。
相关问题
#include<iostream> #include<string> #include<math.h> using namespace std; int main(){ int m,n; cin>>m>>n; int a[m+5][n+5]; for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) cin>>a[i][j]; int r,s; cin>>r>>s; int b[r+5][s+5]; for(int i=1;i<=r;i++) for(int j=1;j<=s;j++) cin>>b[i][j]; int minX=1; int minY=1; int min=10000000; for(int u=1;u<=m-r+1;u++) for(int v=1;v<=n-s+1;v++){ int sum=0; for(int i=1;i<=r;i++){ for(int j=1;j<=s;j++){ sum+=abs(a[u+i-1][v+j-1]-b[i][j]); } } if(sum<min){ minX=u; minY=v; min=sum; } } for(int i=1;i<=r;i++){ for(int j=1;j<=s;j++){ cout<<a[minX+i-1][minY+j-1]<<' '; } cout<<endl; } return 0; }改为c语言
把代码中的"cout"改为"printf",去掉命名空间,整体代码如下:
#include <stdio.h>
#include <string.h>
#include <math.h>
int main() {
int m, n;
scanf("%d%d", &m, &n);
int a[m + 5][n + 5];
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
int r, s;
scanf("%d%d", &r, &s);
int b[r + 5][s + 5];
for (int i = 1; i <= r; i++)
for (int j = 1; j <= s; j++)
scanf("%d", &b[i][j]);
int minX = 1;
int minY = 1;
int min = 10000000;
for (int u = 1; u <= m - r + 1; u++)
for (int v = 1; v <= n - s + 1; v++) {
int sum = 0;
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= s; j++) {
sum += abs(a[u + i - 1][v + j - 1] - b[i][j]);
}
}
if (sum < min) {
minX = u;
minY = v;
min = sum;
}
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= s; j++) {
printf("%d ", a[minX + i - 1][minY + j - 1]);
}
printf("\n");
}
return 0;
}
改代码#include <bits/stdc++.h> using namespace std; #define ll long long #define std ios::sync_with_stdio(false) int a[1000]; int b[29]; void pme(int m,int n) { if(m == n) { for(ll i=0;i<=n-1;i++) { if(a[i]) cout << "1 "; else cout << "0 "; } cout << endl; } else { a[m] = 1; pme(m+1,n); a[m] = 0; pme(m+1,n); } } void print(string T) { ll i; for(i=0;i<=T.size()-1;i++) { if(T[i] == ' ') continue; else if(((T[i]=='-' && T[i+1]=='>')||T[i]=='|'&&T[i+1]=='|')&&i<T.size()-1) cout << T[i] << T[i+1] << " ",i++; else if(T[i]=='<'&&T[i+1]=='-'&&T[i+2]=='>'&&i<T.size()-2) cout << T[i] << T[i+1] << T[i+2] << " ",i+=2; else cout << T[i] << " "; } cout << endl; return; } void sort(string T,char p[],int m) { for(ll i=0;i<=m-2;i++) { for(ll j=i+1;j<=m-1;j++) { if(p[i] > p[j]) swap(p[i],p[j]); } } return; } void show(string T,char p[],int m) { sort(T,p,m); for(ll i=0;i<=m-1;i++) { cout << p[i] << " "; } cout << endl; } int main() { std; string T; char p[1000]; while(getline(cin,T)) { fill(b,b+25,0); int c = 0; int m = 0; for(ll i=0;i<=T.size()-1;i++) { if(T[i] <= 'z' && T[i]>='a' && b[T[i]-'a']==0) { c++; b[T[i]-'a'] = 1; p[m++] = T[i]; } } p[m] = '\0'; print(T); show(T,p,m); pme(0,c); } return 0; }
这个代码稍作修改就能够在C语言中使用了,修改如下:
```c
#include <stdio.h>
#include <string.h>
#define ll long long
int a[1000];
int b[29];
void pme(int m,int n) {
if(m == n) {
for(ll i=0;i<=n-1;i++) {
if(a[i])
printf("1 ");
else
printf("0 ");
}
printf("\n");
}
else {
a[m] = 1;
pme(m+1,n);
a[m] = 0;
pme(m+1,n);
}
}
void print(char T[]) {
ll i;
for(i=0;i<=strlen(T)-1;i++) {
if(T[i] == ' ')
continue;
else if(((T[i]=='-' && T[i+1]=='>')||T[i]=='|'&&T[i+1]=='|')&&i<strlen(T)-1)
printf("%c%c ", T[i], T[i+1]),i++;
else if(T[i]=='<'&&T[i+1]=='-'&&T[i+2]=='>'&&i<strlen(T)-2)
printf("%c%c%c ", T[i], T[i+1], T[i+2]),i+=2;
else
printf("%c ", T[i]);
}
printf("\n");
return;
}
void sort(char T[],char p[],int m) {
for(ll i=0;i<=m-2;i++) {
for(ll j=i+1;j<=m-1;j++) {
if(p[i] > p[j])
// 交换p[i]和p[j]
char tmp = p[i];
p[i] = p[j];
p[j] = tmp;
}
}
return;
}
void show(char T[],char p[],int m) {
sort(T,p,m);
for(ll i=0;i<=m-1;i++) {
printf("%c ", p[i]);
}
printf("\n");
}
int main() {
char T[1000];
char p[1000];
while(fgets(T, sizeof(T), stdin)) {
memset(b, 0, sizeof(b));
int c = 0;
int m = 0;
for(ll i=0;i<strlen(T);i++) {
if(T[i] <= 'z' && T[i]>='a' && b[T[i]-'a']==0) {
c++;
b[T[i]-'a'] = 1;
p[m++] = T[i];
}
}
p[m] = '\0';
print(T);
show(T,p,m);
pme(0,c);
}
return 0;
}
```
主要的修改内容包括:
1. 头文件改为`stdio.h`和`string.h`。
2. `using namespace std`删除。
3. `cout`改为`printf`。
4. `string`改为`char`。
5. `getline`改为`fgets`。
6. `swap`改为手动交换。
需要注意的是,C语言中的字符串是以字符数组的形式存储的,因此需要使用字符数组来替换原代码中的字符串。
相关推荐
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)