输出范例给的不工整
思路及代码
写的不优雅
分为三部分
枚举输出 n = 1-11
规律求出 n >= 12 的矩阵
输出需要的矩阵,把每一行存成一个字符串,方便左右对齐
#include<iostream>
#include<cmath>
#include<iomanip>
#include<string>
using namespace std;
int main(){
//input 多组 n int 1<= <=99
int n;
//solution 规律如图
int sum = 0;
while (cin >> n){
if (sum >= 1){
cout << endl;
}
sum++;
//edge case:1-11
if (1 <= n && n<= 11){
switch(n){
case 1:
cout << 1 << endl;
break;
case 2:
cout << 1 << endl << 2 <<endl;
break;
case 3:
cout << 1 << endl << 2 << ' ' << 3 << endl;
break;
case 4:
cout << 1 << ' ' << 4 << endl << 2 << ' ' << 3 << endl;
break;
case 5:
cout << ' ' << ' ' << 5 << endl << 1 << ' ' << 4 << endl << 2 << ' ' << 3 <<endl;
break;
case 6:
cout << 6 << ' ' << 5 << endl << 1 << ' ' << 4 << endl << 2 << ' ' << 3 << endl;
break;
case 7:
cout << 7 << ' ' << 6 << ' ' << 5 << endl << ' ' << ' ' << 1 << ' ' << 4 << endl << ' ' << ' ' << 2 << ' ' << 3 << endl;
break;
case 8:
cout << 7 << ' ' << 6 << ' ' << 5 << endl << 8 << ' ' << 1 << ' ' << 4 << endl << ' ' << ' ' << 2 << ' ' << 3 << endl;
break;
case 9:
cout << 7 << ' ' << 6 << ' ' << 5 << endl << 8 << ' ' << 1 << ' ' << 4 << endl << 9 << ' ' << 2 << ' ' << 3 << endl;
break;
case 10:
cout << ' ' << 7 << ' ' << 6 << ' ' << 5 << endl << ' ' << 8 << ' ' << 1 << ' ' << 4 << endl << ' ' << 9 << ' ' << 2 << ' ' << 3 << endl << 10 << endl;
break;
default:
cout << ' ' << 7 << ' ' << ' ' << 6 << ' ' << 5 << endl << ' ' << 8 << ' ' << ' ' << 1 << ' ' << 4 << endl << ' ' << 9 << ' ' << ' ' << 2 << ' ' << 3 << endl << 10 << ' ' << 11 << endl;
}
continue;
}
//normal case:
int flag = 1;
int t = 1;
int i = 4;
int j = 4;
string list[10][10];
list[4][4] = to_string(t);
while (t < n){
if (flag%2 == 1){
for (int k = 1; k <= flag; k++){
i++;
if( t+1 <= n){
list[i][j] = to_string(++t);
}else{
break;
}
}
for (int k = 1; k <= flag; k++){
j++;
if( t+1 <= n){
list[i][j] = to_string(++t);
}else{
break;
}
}
flag++;
}else{
for (int k = 1; k <= flag; k++){
i--;
if( t+1 <= n){
list[i][j] = to_string(++t);
}else{
break;
}
}
for (int k = 1; k <= flag; k++){
j--;
if( t+1 <= n){
list[i][j] = to_string(++t);
}else{
break;
}
}
flag++;
}
}
//output anslist[]
string anslist[10];
for(int row = 0; row <= 9; row++){
for (int col = 0; col <= 9; col++){
if (list[row][col] != ""){
if (stoi(list[row][col]) <= 9){
anslist[row] += " " + list[row][col] + " ";
}else if ((list[row][col+1] == "" && col+1 <= 9) || col == 9){
anslist[row] += list[row][col];
}else{
anslist[row] += list[row][col] + " ";
}
}
}
}
int max_len = 0;
for (string ele: anslist){
max_len = (int)ele.length() > max_len ? (int) ele.length() : max_len;
}
//偶数右对齐
if((int)sqrt(n)%2 == 0){
for (string ele: anslist){
if(ele != ""){
cout << setw(max_len) << setfill(' ') << ele << endl;
}
}
}else{ //奇数左对齐
for (string ele: anslist){
if(ele != ""){
cout << ele << endl;
}
}
}
}
return 0;
}
参考:1️⃣ C/C++中string和int相互转换的常用方法_string转int-CSDN博客
2️⃣ 【DHUOJ】进阶59_明明在上学的时候,参加数学兴趣班。在班上,老师介绍了一种非常有趣的阵列。 该阵-CSDN博客
3️⃣ C++string与int的相互转换(使用C++11)_c++ string转int-CSDN博客
4️⃣ C++中求string类型字符串的长度的方法_string的长度c++-CSDN博客
5️⃣ https://bbs.csdn.net/topics/190135371
收获:
1️⃣string转 int to_string(...)
int 转 string 头文件 string 函数 stoi(...)
2️⃣复习 string 类长度 name.length() long 类型
菜菜,不是教程,做题和学习记录