#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> #include <vector> #include <map> #include <string> #include <cstring> #define fast ios::sync_with_stdio(false),cin.tie(0) using namespace std; typedef pair<int, int> PII; typedef long long LL; const int N = 1010; int T; int f1, s1, f2, s2; vector<PII> v; string x[8] = {"11111111", "10000001", "10111101", "10111101", "10111101", "10111101", "10000001", "11111111"}; int main() { int n, m; scanf("%d %d", &n, &m); string s[N]; for(int i = 0; i < n; i ++ ) { cin >> s[i]; } for(int i = 0; i <= n - 8; i ++ ) { for(int j = 0; j <= m - 8; j ++ ) { int k, cnt = 0; for(k = 0; k < 8; k ++ ) { //cout << k << " " << x[k] << " " << s[i+k].substr(j, 8) << endl; if( x[k] == s[i+k].substr(j, 8) ) cnt ++; } if(cnt == 8) v.push_back({i, j}); } } sort(v.begin(), v.end()); for(int i = 0; i < 3; i ++ ) printf("%d %d\n", v[i].first, v[i].second); return 0; }转成c语言
时间: 2024-03-13 08:47:23 浏览: 19
这是一段 C++ 代码,如果要将其转换为 C 语言,可以做出以下修改:
1. 将头文件的引用改为 C 语言的头文件,例如将 `#include <iostream>` 改为 `#include <stdio.h>`。
2. 将 `using namespace std;` 删除,因为 C 语言中没有命名空间的概念。
3. 将 `string` 类型改为 C 语言中的字符数组,例如将 `string s[N];` 改为 `char s[N][M];`,其中 `M` 是矩阵的列数。
4. 将 `vector` 类型改为数组,例如将 `vector<PII> v;` 改为 `PII v[N];`。
5. 将 `cin` 改为 `scanf` 或 `gets`,将 `cout` 改为 `printf` 或 `puts`。
6. 将 `sort` 函数改为手动实现排序算法。
7. 将 `auto` 关键字改为 `int` 或 `char`,因为 C 语言不支持自动类型推断。
修改后的代码如下所示:
```
#include <stdio.h>
#include <string.h>
#define N 1010
typedef struct PII {
int first;
int second;
} PII;
int f1, s1, f2, s2;
PII v[N];
char x[8][9] = {"11111111", "10000001", "10111101", "10111101", "10111101", "10111101", "10000001", "11111111"};
int cmp(const void* a, const void* b) {
PII* pa = (PII*)a;
PII* pb = (PII*)b;
if(pa->first != pb->first) return pa->first - pb->first;
else return pa->second - pb->second;
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
char s[N][N];
for(int i = 0; i < n; i ++ ) {
scanf("%s", s[i]);
}
int index = 0;
for(int i = 0; i <= n - 8; i ++ ) {
for(int j = 0; j <= m - 8; j ++ ) {
int k, cnt = 0;
for(k = 0; k < 8; k ++ ) {
if( !strncmp(x[k], s[i+k]+j, 8) ) cnt ++;
}
if(cnt == 8) {
v[index].first = i;
v[index].second = j;
index ++;
}
}
}
qsort(v, index, sizeof(PII), cmp);
for(int i = 0; i < 3 && i < index; i ++ ) {
printf("%d %d\n", v[i].first, v[i].second);
}
return 0;
}
```
需要注意的是,这里使用了 C 语言标准库中的 `strncmp` 函数来比较两个字符串是否相等,因为 C 语言中没有自带的字符串类型,需要使用字符数组来表示字符串。另外,排序算法使用了 C 语言标准库中的 `qsort` 函数,需要定义一个比较函数来指定排序的规则。