zoukankan      html  css  js  c++  java
  • 华科机考:最大的两个数

    时间限制:1秒  空间限制:32768K 

     题目描述

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

    输入描述:     输入第一行包括一个整数n(1<=n<=1000),接下来的四行每行包括五个整数。代表一个四行五列的矩阵,矩阵元素全部是整数。

    输出描述:     可能有多组测试数据,对于每组数据,按照样例输出的格式将每列最大的两个数输出,如果最大的两个数中的一个数在这一列中有多个相同的值,则行值取行值小的那一个。     输出时要保留原矩阵的行列顺序,即在原矩阵中行值小的,在输出矩阵中的行值依然小。

    输入例子: 1 

                  1  2   4  9  8

                 -1  4  9  8  8

                 12  9  8  7  0

                  7   8  9  7  0

    输出例子: 12 9 9 9 8

                   7 8 9 8 8

    思路:定义两个数组,分别用存第一排的数和第二排的数,遍历每一列,找到最小的两个数(需要考虑到下标的问题),分别存在两个数组中,行数小的存在前一个数组,大的存在后一个数组中.

    代码:

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    
    int main(){
       int n;
       int a[4][5];
       int ans1[5],ans2[5];
       cin>>n;
         while(n--){
         for(int p=0;p<4;p++)
            for(int q=0;q<5;q++)
               cin>>a[p][q];
         int max1,max2;
         int tmp1,tmp2;
         for(int p=0;p<5;p++){
         max1=-99999999,max2=-99999999;
         for(int q=0;q<4;q++){//找最大值
           if(a[q][p]>max1){
             max1=a[q][p];
             tmp1=q;
           }
         }
         a[tmp1][p]=-99999999;
         for(int q=0;q<4;q++){//找次大值
           if(a[q][p]>max2){
             max2=a[q][p];
             tmp2=q;
           }
         }
         if(tmp1<tmp2){
          ans1[p]=max1;
          ans2[p]=max2;
         }
         else{
         ans1[p]=max2;
         ans2[p]=max1;
         }
         }
         for(int i=0;i<5;i++)
           cout<<ans1[i]<<" ";
         cout<<endl;
         for(int i=0;i<5;i++)
           cout<<ans2[i]<<" ";
         cout<<endl;
       }
       return 0;
    }
  • 相关阅读:
    bzoj1996
    bzoj2839
    bzoj1304
    bzoj1097
    bzoj4547
    bzoj3379
    bzoj3090
    树莓派/Debian 构建LAMP Web服务器并搭建WordPress博客(一)
    树莓派/Debian Apache2 配置自建 CA 实现 HTTPS(SSL) 服务
    树莓派/Debian Apache2 实现 HTTPS(SSL) 服务
  • 原文地址:https://www.cnblogs.com/mlgjb/p/6667686.html
Copyright © 2011-2022 走看看