• 模拟


    输入一个四行五列的矩阵,找出每列最大的两个数。

    输入描述:

    每个用例包含四行,每行包括五个整数。代表一个四行五列的矩阵,矩阵元素全部是整数。

    输出描述:

        可能有多组测试数据,对于每组数据,按照样例输出的格式将每列最大的两个数输出,如果最大的两个数中的一个数在这一列中有多个相同的值,则行值取行值小的那一个。     输出时要保留原矩阵的行列顺序,即在原矩阵中行值小的,在输出矩阵中的行值依然小。
    #include <bits/stdc++.h>
    using namespace std;
    
    int data1[4][5];
    int res[5][2];
    
    int main(){
    
        while(scanf("%d",&data1[0][0])!=EOF){
            memset(res,-1,10*sizeof(int));
            //cout<<res[0][1]<<" "<<res[0][2]<<endl;;
            int i=1;
            while(i<20){
                scanf("%d",&data1[i/5][i%5]);
                //cout<<i/4<<" "<<i%5<<endl;
                i++;
            }
            /*for(i=0;i<4;i++){
                for(int j=0;j<5;j++){
                    cout<<data[i][j]<<" ";
                }
                cout<<endl;
            }*/
            for(i=0;i<4;i++){
                for(int j=0;j<5;j++){
                    if(res[j][0]==-1){
                        res[j][0]=i;
                        //cout<<data[i][j]<<"fangl"<<endl;
                    }
                    else if(res[j][1]==-1){
                        res[j][1]=i;
                        //cout<<data[i][j]<<"fangl1"<<endl;
                    }
                    //else if(data[i][j]==data[res[j][0]][j]||data[i][j]==data[res[j][1]][j])
                      //  continue;
                    else if(data1[i][j]>data1[res[j][0]][j]&&data1[i][j]>data1[res[j][1]][j]){
                        if(data1[res[j][0]][j]>data1[res[j][1]][j]){
                            res[j][1]=i;
                        }
                        else
                            res[j][0]=i;
                    }
                    else if(data1[i][j]>data1[res[j][0]][j]){
                        res[j][0]=i;
                        //cout<<data[i][j]<<"fhuanl"<<endl;
                    }
                    else if(data1[i][j]>data1[res[j][1]][j]){
                        res[j][1]=i;
                        //cout<<data[i][j]<<"fang333l"<<endl;
                    }
                }
            }
            int temp;
            for(i=0;i<5;i++){
                if(res[i][0]>res[i][1]){
                    temp=res[i][0];
                    res[i][0]=res[i][1];
                    res[i][1]=temp;
                }
            }
            for(i=0;i<2;i++){
                for(int j=0;j<5;j++){
                    //cout<<res[j][i]<<"hhh"<<endl;
                    printf("%d ",data1[res[j][i]][j]);
                }
                printf("\n");
            }
        }
        return 0;
    }

     给出年分m和一年中的第n天,算出第n天是几月几号。

    #include <bits/stdc++.h>
    using namespace std;
    
    int days[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
                     {31,29,31,30,31,30,31,31,30,31,30,31}};
    
    void cpt(int ind,int day){
        int i=0;
        while(day-days[ind][i]>0){
            day-=days[ind][i++];
        }
        printf("-%02d-%02d\n",i+1,day);
    }
    
    int main(){
        int year,day;
        while(scanf("%d %d",&year,&day)!=EOF){
            cout<<year;
            int ind;
            if((year%4==0&&year%100!=0)||(year%400==0))
                ind=1;
            else
                ind=0;
            cpt(ind,day);
        }
        return 0;
    }

     螺线形矩阵1-n方

    #include <bits/stdc++.h>
    using namespace std;
    
    #define maxn 1000
    
    int X[4]={1,0,-1,0};//移动方向
    int Y[4]={0,-1,0,1};
    
    int res[maxn][maxn];
    
    int turn(int n,int x,int y){
        if((res[x][y-1]!=-1)&&(x-1<0||res[x-1][y]!=-1)&&(y+1>=n||res[x][y+1]!=-1))
            return 0;
        if((x+1>=n||res[x+1][y]!=-1)&&(y+1>=n||res[x][y+1]!=-1)&&(res[x-1][y]!=-1))
            return 1;
        if((x+1>=n||res[x+1][y]!=-1)&&(y-1<0||res[x][y-1]!=-1)&&(res[x][y+1]!=-1))
            return 2;
        if((y-1<0||res[x][y-1]!=-1)&&(res[x-1][y]!=-1)&&(res[x+1][y]!=-1))
            return 3;
        return -1;
    }
    
    int main(){
        memset(res,-1,maxn*maxn*sizeof(int));
        int n;
        cin>>n;
        res[0][0]=1;//初始位置
        int nextmove=3;
        int x=0,y=0;
        for(int i=2;i<=n*n;i++){
            if(turn(n,x,y)!=-1)
                nextmove=turn(n,x,y);
            x+=X[nextmove];
            y+=Y[nextmove];
            res[x][y]=i;
    
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                cout<<res[i][j]<<" ";
            }
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    新建maven 父子模块项目
    for循环删除list中多个元素出现的误区
    mysql不支持emoji表情的问题的解决方法
    innodb的读写参数优化
    redis list命令
    redis之常用Set和ZSet命令
    java中list里面存放map,根据map中的某一个字段进行排序
    spring-redis 存储数据
    批量mvn 打包 bat文件命令
    yum 安装docker后 无法启动
  • 原文地址:https://www.cnblogs.com/yaotong0830/p/15987097.html
Copyright © 2020-2023  润新知