= n; k++) {
cout << max(i - k + 1, k - i + 1) << " ";
}
cout << endl;
}
cout << endl;
}
return 0;
}
755
755. 平方矩阵 III - AcWing题库
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
long n, m[31] = {1};
for (int i = 1; i < 31; i++) {
m[i] = m[i - 1] * 2;
}
while (cin >> n, n) {
for (int i = 1; i <= n; i++) {
for (int k = 1; k <= n; k++) {
cout << m[i + k - 2] << " ";
}
cout << endl;
}
cout << endl;
}
return 0;
}
756 ?
756. 蛇形矩阵 - AcWing题库
涉及到状态的转变,逻辑题
解题法-1 传统逻辑解题
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main() {
int a[101][101];
memset(a, 0, sizeof a);
int n, m;
cin >> n >> m;
int i = 0, k = 0;
int c = 1;
int mode = 1;
while (i - 1 >= 0 && a[i - 1][k] == 0 || i + 1 < n && a[i + 1][k] == 0 ||
k - 1 >= 0 && a[i][k - 1] == 0 || k + 1 < n && a[i][k + 1] == 0 ||
a[i][k] == 0) {
if (mode == 1) {
a[i][k] = c++;
if (k + 1 == m || a[i][k + 1] != 0) {
mode = 2;
i++;
} else
k++;
} else if (mode == 2) {
a[i][k] = c++;
if (i + 1 == n || a[i + 1][k] != 0) {
mode = 3;
k--;
} else
i++;
} else if (mode == 3) {
a[i][k] = c++;
if (k - 1 < 0 || a[i][k - 1] != 0) {
mode = 4;
i--;
} else
k--;
} else {
a[i][k] = c++;
if (i - 1 < 0 || a[i - 1][k] != 0) {
mode = 1;
k++;
} else
i--;
}
}
for (int i = 0; i < n; i++) {
for (int k = 0; k < m; k++) {
cout << a[i][k] << " ";
}
cout << endl;
}
return 0;
}
解题法-2 将移动方式保存到数组中
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int res[101][101];
int main() {
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int m, n;
cin >> n >> m;
int mode = 0;
for (int k = 1, x = 0, y = 0; k <= m * n; k++) {
res[x][y] = k;
int a = x + dx[mode];
int b = y + dy[mode];
if (a < 0 || a >= n || b < 0 || b >= m || res[a][b]) {
mode = (mode + 1) % 4;
a = x + dx[mode];
b = y + dy[mode];
}
x = a;
y = b;
}
for (int i = 0; i < n; i++) {
for (int k = 0; k < m; k++) {
cout << res[i][k] << " ";
}
cout << endl;
}
return 0;
}
2023年8月27日
fgets
默认读入字符串遇到空格就会停止。
fgets用于读入一整行,参数为字符数组地址、最大读多少字符、从哪读,会读入最后的回车符
fgets(s,100000,stdin);
cin
cin.getline用于读入一整行,参数为字符数组地址、最大读多少字符
cin.getline(s,1000);
getline
getline用于string类型读入一整行
getline(cin,s);
puts
puts 输出字符数组,包括换行符
puts(s);
cstring 库函数
库里有 strlen
,strcmp
、strcpy
与 memcpy
类似
strlen(s); // 只计算字符串的元素,\0不计入其中
strcmp(a,b); // 字典序比较,小于返回-1(可能其他负数),等于返回0,大于返回1(可能其他正数)
strcpy(a,b); // 把b复制给a
两层循环优化
在这种循环下,每次循环 strlen(s)
都要执行一遍,是两层循环。
for (int i = 0; i < strlen(s) ; i++)
改成
for (int i = 0, len=strlen(s) ; i < len; i++)
或直接
for (int i = 0; i < s[i]; i++)
80%的情况用string处理
string s1; // 默认的空字符串
string s2 = s1; // s2是s1的副本
string s3 = "hiya"; // s3是该字符串字面值的副本
string s4(10,'c'); // s4的内容是 ccccc
cin >> s1 >> s2;
// string 不能用scanf读但可以用printf输出
printf("%s\n",s1.c_str());
cout << s1 << s2;
cout << s1.empty() << endl; // 布尔值返回 01
cout << s3.empty() << endl; // 布尔值返回 0
cout << s3.size() << endl; // 数组长度,O(1)复杂度
// 支持比较运算符,相加
string s3 = s3 + s4;
s3 += s4;
s3 = s3 + " is great!" + '!'; // 这里不能用 +=
// 加法运算符两边必须有一个 string 类型
// 遍历 string
for (char c : s) cout << c << endl; // 等价于 for 循环体里 char c = str[i]
// 引用符号(可以改变 c 的值)
for (char &c : s)