东华OJ 字符串中等_子字符串排序、二维数组中等_阵列

前言:这阵列比我抽到的暗恋厉害多了。实现思路都挺容易想到但是阵列这题我的思路很容易PE。对于输出条件的控制很高。

阵列:

  1. 分析题意:
    1. 需要我们按照一定规则打印一个矩阵(可能不完全-即3*3的矩阵内可能位置不变但是没有8&9)
    2. 在左边的虽然不需要打印,但是我们仍然需要输入空格来达成格式相同。
  2. 实现思路:
    1. 既然我们判断了一个矩形能够容下这n个元素,那么我们就可以先定义一个足够放下100个元素的二维数组(防止越界),然后判断n个元素需要多大的矩阵存放后再向二维数组存放这个矩阵。
    2. 最后根据n的大小和大于n的元素的位置关系来判断是停止输出还是打印空格占位。

最后判断一行有无二位数从而打印的条件不想写了,一共也就三种情况,直接打表了。

AC代码

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;
int main()
{
    int n;
    while(cin>>n){
        int martix[11][11];
        int edge;
        if(sqrt(n)!=(int)sqrt(n)) edge= (int)sqrt(n)+1;
        else edge=sqrt(n);
        int x,y;
        if(edge%2!=0){
            martix[edge/2+1][edge/2+1]=1;
            x=edge/2+1;
            y=edge/2+1;
        }
        else {
            martix[edge/2][edge/2]=1;
            x=edge/2;
            y=edge/2;
        }
        int dir=1;
        int step=0;
        bool flag = 0;
        for(int i=2,move=0;i<=edge*edge;i++){
            if(dir==1||dir==3){
                if(!flag){
                    step++;
                    flag=1;
                }
            }
            if(dir==1){
                    y++;
                    martix[y][x]=i;
                    move++;
                    if(move==step){
                        dir=2;
                        move =0;
                    }
            }
            else if(dir==2){
                    flag =0;
                    x++;
                    martix[y][x]=i;
                    move++;
                    if(move==step){
                        dir=3;
                        move =0;
                    }
            }
            else if(dir==3){
                    y--;
                    martix[y][x]=i;
                    move++;
                    if(move==step){
                        dir=4;
                        move=0;
                    }
            }
            else if(dir==4){
                    flag =0;
                    x--;
                    martix[y][x]=i;
                    move++;
                    if(move==step){
                        dir=1;
                        move=0;
                    }
            }
        }
        int j;
        flag =0;
        for(int i=1;i<=edge;i++){
            if(martix[1][1]>n) j=2;
            else j=1;
            for(int no1=j;j<=edge;j++){
                if(edge%2!=0){//奇数 没得选 每个位置必须有东西
                    if(n>=10){
                        if(j==no1){
                            if(martix[i][j]>n){
                                cout<<"  ";
                            }
                            else{
                                cout<<setw(2)<<setfill(' ')<<right<<martix[i][j];
                            }
                        }
                        else{
                            if(martix[i][j]>n){
                                cout<<"   ";
                            }
                            else{
                                cout<<' '<<setw(2)<<setfill(' ')<<right<<martix[i][j];
                            }
                        }
                    }
                    else{
                        if(j==no1){
                            if(martix[i][j]>n){
                                cout<<" ";
                            }
                            else{
                                cout<<martix[i][j];
                            }
                        }
                        else{
                            if(martix[i][j]>n){
                                cout<<"  ";
                            }
                            else{
                                cout<<' '<<martix[i][j];
                            }
                        }
                    }
                }
                else{//偶数->最后一位如果停在(n-1)^2到(n)^2间就可以不输出后面的直接退出
                    if(n<12&&n>=10){
                        if (n==10)
                        {
                            cout<<" 7 6 5"<<endl;
                            cout<<" 8 1 4"<<endl;
                            cout<<" 9 2 3"<<endl;
                            cout<<"10"<<endl;
                            flag =1;
                            break;

                        }
                        else if(n==11){
                            cout<<" 7  6 5"<<endl;
                            cout<<" 8  1 4"<<endl;
                            cout<<" 9  2 3"<<endl;
                            cout<<"10 11"<<endl;
                            flag =1;
                            break;
                        }
                    }
                    else if(n>=12){//16开始每一位保留两个位置
                        if(j==no1){
                            if(martix[i][j]<=n)
                                cout<<setw(2)<<setfill(' ')<<right<<martix[i][j];
                            else break;
                        }
                        else {
                            if(martix[i][j]<=n){
                                cout<<' '<<setw(2)<<setfill(' ')<<right<<martix[i][j];
                            }
                            else break;
                        }
                    }
                    else{
                        if(j==no1){
                            if(martix[i][j]<=n){
                                cout<<martix[i][j];
                            }
                            else break;
                        }
                        else{
                            if(martix[i][j]<=n){
                                cout<<' '<<martix[i][j];
                            }
                            else break;
                        }
                    }
                }
            }
            cout<<endl;
            if(flag){
                break;
            }
        }
        cout<<endl;
    }
    return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值