我正在尝试获取这个数字模式
Input: 7
Output:
1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 4 3 2 1
1 2 3 3 3 2 1
1 2 2 2 2 2 1
1 1 1 1 1 1 1
但我不知道如何制作它,请问如何制作该图案有什么建议吗??
到目前为止我的代码:
int n, temp1, temp2,i,j;
cin >> n;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
temp1 = j;
temp2 = n-j+1;
if (temp1 < temp2) cout << temp1;
else cout << temp2;
}
cout << endl;
}
目前的输出是
1 2 3 4 3 2 1
1 2 3 4 3 2 1
1 2 3 4 3 2 1
1 2 3 4 3 2 1
1 2 3 4 3 2 1
1 2 3 4 3 2 1
1 2 3 4 3 2 1
提前致谢。
最佳答案
我希望这段代码(有效)可以让您对实现有更好的了解。
int main() {
int n;
cin >> n;
int arr[n][n];
//Numbers in the grid vary from 1 - (n/2 + 1)
for(int i = 0; i <= n / 2; i++) {
//Start filling the array in squares
//Like fill the outer square with 1 first followed by 2...
for(int j = i; j < n - i; j++) {
arr[i][j] = i + 1;
arr[n - 1 - i][j] = i + 1;
arr[j][i] = i + 1;
arr[j][n - 1 - i] = i + 1;
}
}
关于c++ - 创建数字模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40638372/