#include #include using namespace std; int n, l, f=0; bool addition=true, x=true; int main() { cin>>n; int nmidx=(n-1)/2, nmidy=(n-1)/2; int num[100][100]={0}; for(int i=1;i
#include <iostream>#include <math.h>using namespace std;int n, l, f=0;bool addition=true, x=true;int main() {cin>>n;int nmidx=(n-1)/2, nmidy=(n-1)/2;int num[100][100]={0};for(int i=1;i<n;i++){for(int k=0;k<2;k++){for(int j=0;j<i;j++){if(addition){if(x)nmidx+=1;elsenmidy-=1;}else{if(x)nmidx-=1;elsenmidy+=1;}f++;num[nmidy][nmidx]=f;}x=!x;}addition=!addition;}for(int i=0;i<n;i++){num[nmidy][nmidx+i]=num[nmidy][nmidx]+i;}string str;str=std::to_string(num[nmidy][nmidx+n-1]);for(int i=0;i<n;i++){for(int j=0;j<n;j++){cout.width(str.length()+2);cout.fill(' ');cout<<num[i][j];}cout<<"\n";}return 0;}//它跑掉了._
1.
cout.width(str.length()+2);
1. 題目沒特別說寬度多少,不過從範例輸出可以看出要設成4
2. 多筆測資
#include #include using namespace std; int n, l, f=0; bool addition=true, x=true; int main() { cin>>n; int nmidx=(n-1)/2, nmidy=(n-1)/2; int num[100][100]={0}; for(int i=1;i
cout << setw(4) << setfill(' ') << num[i][j];