方块转换 题目描述
#include<bits/stdc++.h>
using namespace std;
#define N 15
int n;
char a[N][N], b[N][N], c[N][N];
void reset() { // 重置矩阵
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) a[i][j] = b[i][j];
}
bool check(char l[][N], char p[][N]) { // 判断两个矩阵是否相同
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
if(l[i][j] != p[i][j]) return 0;
}
return 1;
}
bool work1() {
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) b[j][n-i+1] = a[i][j];
return check(b, c);
}
bool work2() {
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) b[n-i+1][n-j+1] = a[i][j];
return check(b, c);
}
bool work3() {
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) b[n-j+1][i] = a[i][j];
return check(b, c);
}
bool work4() {
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) b[i][n-j+1] = a[i][j];
return check(b, c);
}
bool work5() {
work4();
reset();
if(work1()) return 1;
reset();
if(work2()) return 1;
reset();
if(work3()) return 1;
return 0;
}
bool work6() {
return check(b, c);
}
void work() {
if(work1()) {
cout << 1; return;
}
else if(work2()) {
cout << 2; return;
}
else if(work3()) {
cout << 3; return;
}
else if(work4()) {
cout << 4; return;
}
else if(work5()) {
cout << 5; return;
}
else if(work6()) {
cout << 6; return;
}
else cout << 7;
}
int main() {
cin >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
cin >> a[i][j];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
cin >> c[i][j];
work();
return 0;
}
判断两矩阵是否相同涉及函数传递二维数组知识,可以使用两种方法:
1. 指定第二维和更高维度的大小,如上check函数所示。
2. 可以通过指针来寻址二维数组,代码如下
bool check(char *l, char *p) {
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
if(*(l + i * N + j) != *(p + i * N + j)) return 0;
}
return 1;
}
check(&b[0][0], &c[0][0])
插火把题目描述
#include<iostream>
#include<cmath>
using namespace std;
int n, m, k, o, p, ans;
int s[105][105];
bool beyond(int x, int y) { //判断是否越界
if(x > 0 && x <= n && y > 0 && y <= n) return 1;
return 0;
}
int main() {
cin >> n >> m >> k;
for(int i = 1; i <= m + k; i++) {
cin >> o >> p;
for(int x = -2; x <= 2; x++)
for(int y = -2; y <= 2; y++) {
if((i > m || abs(x) + fabs(y) <= 2) && beyond(o + x, p + y))
//如果是萤石(i > m)或者x与y的坐标差的和不超过2且没有越界就标记
s[o+x][p+y]++;
}
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
ans += s[i][j] == 0; //枚举每一个方格,看看是不是==0
printf("%d\n", ans);
return 0;
}
蛇形方阵题目描述
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int a[10][10] = {0};
int n, k = 1, x = 1, y = 0;
cin >> n;
while (k <= n * n) {
//切勿y<=n,后面是++y,是一种向前探索
while (y < n && !a[x][y + 1]) a[x][++y] = k++;
while (x < n && !a[x + 1][y]) a[++x][y] = k++;
while (y > 1 && !a[x][y - 1]) a[x][--y] = k++;
while (x > 1 && !a[x - 1][y]) a[--x][y] = k++;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) printf("%3d", a[i][j]);
printf("\n");
}
return 0;
}
垂直柱状图题目描述
#include<iostream>
#include<string>
#define MAX 410
using namespace std;
int main() {
int c[26] = {0};
string a;
for(int i = 0; i < 4; i++) {
getline(cin, a);
//统计字符出现次数
for(int j = 0; j < a.length(); j++) if(a[j] >= 'A' && a[j] <= 'Z') c[a[j]-'A']++;
}
int max = 0;
for(int i = 0; i < 26; i++) if(c[i] > max) max = c[i];
for(int i = max; i > 0; i--) {
for(int j = 0; j < 26; j++)
if(c[j] >= i) printf("* "); //模拟,满足条件的输出*
else printf(" ");
printf("\n");
}
for(int i = 0; i < 26; i++) printf("%c ", 'A' + i);
return 0;
}
魔法少女小Scarlet题目描述
#include<iostream>
#include<string>
#define MAX 510
using namespace std;
int s[MAX][MAX], a[MAX][MAX];
int n, m;
void swit1(int x, int y, int r) {
for(int i = -r; i <= r; i++)
for(int j = -r; j <= r; j++)
a[x+j][y-i] = s[x+i][y+j];
for(int i = x - r; i <= x + r; i++)
for(int j = y - r; j <= y + r; j++)
s[i][j] = a[i][j];
}
void swit2(int x, int y, int r) {
for(int i = -r; i <= r; i++)
for(int j = -r; j <= r; j++)
a[x-j][y+i] = s[x+i][y+j];
for(int i = x - r; i <= x + r; i++)
for(int j = y - r; j <= y + r; j++)
s[i][j] = a[i][j];
}
int main() {
int x, y, r, z;
cin >> n >> m;
int count = 1;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
s[i][j] = count++;
}
for(int i = 0; i < m; i++) {
cin >> x >> y >> r >> z;
if(z == 0) swit1(x, y, r);
else swit2(x, y, r);
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) cout << s[i][j] << ' ';
cout << endl;
}
return 0;
}