1.长数组初始化:
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char ar[5][5];
memset(ar,'o',sizeof(ar));
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
cout<<ar[i][j];
}
cout<<endl;
}
}
2.计算某一行元素的同行,同列,对角线的坐标:
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int x, y, x1, y1, x2, y2;
cin >> x >> y;
for (int i = 1; i <= n; i++) {
cout << "(" << x << "," << i << ")";
}
cout << endl;
for (int j = 1; j <= n; j++) {
cout << "(" << j << "," << y << ")";
}
cout << endl;
if (x < y) {
y1 = y - x + 1;
x1 = 1;
} else {
y1 = 1;
x1 = x - y + 1;
}
for (int i = x1, j = y1; i <= n && j <= n; i++, j++) {
cout << "(" << i << "," << j << ")";
}
cout << endl;
x2 = 4;
y2 = y - 4 + x;
for (int i = x2, j = y2; i >= 1 && j <= n; i--, j++) {
cout << "(" << i << "," << j << ")";
}
}
3. 查找每一行每一列的和是否为偶数,如是,则输出OK,如否,只要改变一个元素即可变成每一行每一列和为偶数的,则输出那一个需要改变的元素的坐标,否则就输出NO:
#include <iostream>
using namespace std;
int main() {
int n, s = 0, x = -1, y = -1;
cin >> n;
int ar[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> ar[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
s += ar[i][j];
}
if (s % 2 != 0) {
x = i;
s = 0;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
s += ar[j][i];
}
if (s % 2 != 0) {
y = i;
s = 0;
}
}
if (x == -1 && y == -1) {
cout << "OK";
} else if (x > -1 && y > -1) {
cout << x << " " << y;
} else {
cout << "NO";
}
}