码迷,mamicode.com
首页 > 其他好文 > 详细

最大的两个数

时间:2015-01-02 10:57:25      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

最大的两个数

题目描述:

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

输入:

    输入第一行包括一个整数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 
提示:

每个数字后面都要输出一个空格

 

Code:
#include <iostream>
#include <algorithm>
#include <vector>
 
using namespace std;
 
int main()
{
    int arr[6][6];
    int n;
    while(cin>>n){
        while(n--){
            for(int i=1;i<=4;++i){
                for(int j=1;j<=5;++j){
                    cin>>arr[i][j];
                }
            }
            int first,second;
            vector<int> tmpVec;
            vector<int> ansVec;
            for(int i=1;i<=5;++i){
                tmpVec.clear();
                for(int j=1;j<=4;++j){
                   tmpVec.push_back(arr[j][i]);
                }
                sort(tmpVec.begin(),tmpVec.end());
                first=tmpVec[3];
                second=tmpVec[2];
                bool notFoundFirst=false;
                bool notFoundSecond=false;
                int index;
                for(int j=1;j<=4;++j){
                    if(arr[j][i]==first||arr[j][i]==second){
                        ansVec.push_back(arr[j][i]);
                        if(arr[j][i]==first){
                            notFoundFirst=true;
                        }else{
                            if(arr[j][i]==second){
                                notFoundSecond=true;
                            }
                        }
                        index=j;
                        break;
                    }
                }
                if(notFoundFirst==true){
                    for(int j=index+1;j<=4;++j){
                        if(arr[j][i]==second){
                            ansVec.push_back(arr[j][i]);
                            break;
                        }
                    }
                }
                if(notFoundSecond==true){
                    for(int j=index+1;j<=4;++j){
                        if(arr[j][i]==first){
                            ansVec.push_back(arr[j][i]);
                            break;
                        }
                    }
                }
            }
            cout<<ansVec[0]<<" "<<ansVec[2]<<" "<<ansVec[4]<<" "<<ansVec[6]<<" "<<ansVec[8]<<" "<<endl;
            cout<<ansVec[1]<<" "<<ansVec[3]<<" "<<ansVec[5]<<" "<<ansVec[7]<<" "<<ansVec[9]<<" "<<endl;
        }
    }
    return 0;
}
 
/**************************************************************
    Problem: 1200
    User: lcyvino
    Language: C++
    Result: Accepted
    Time:40 ms
    Memory:1520 kb
****************************************************************/

 

 

最大的两个数

标签:

原文地址:http://www.cnblogs.com/Murcielago/p/4198182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!