例题
#include <bits/stdc++.h>
using namespace std;
/*
输入一个n(奇数)
输出一个矩阵,对角线为 +,其余为--。
1. 只输出-号,容易。
2. +号的位置怎么表示。
//反斜线 (1,1)(2,2)(3,3) i==j
//斜线 (1,5) (2,4) (3,3)(4,2) i+j = n+1
怎么发现的?
1.总结归纳法
2.一次函数(解析几何,代数+几何)。8年级数学上册第六章。
*/
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i == j || i + j == n + 1)
cout << "+";
else
cout << "-";
}
cout << endl;
}
return 0;
}
练习:
答案
#include <bits/stdc++.h>
using namespace std;
/*
输入一个n(奇数)
输出n行n列图案
坐标分3类
最左列,最右列是 '|'
中间 (n+1)/2 行是 ‘-’
总结:
讲解错误写法,没用if else
*/
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (j == 1 || j == n) // i 1-n
cout << "|";
else if (i == (n + 1) / 2)
cout << '-';
else
cout << 'a';
}
cout << endl;
}
return 0;
}
练习:
B4037# GESP202409小杨的 N 字矩阵
作业:
答案
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
char c = 'A';
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
cout << c;
c++;
if (c > 'Z')
c = 'A';
}
cout << endl;
}
return 0;
}