注册 登录
编程论坛 C++教室

不懂题目什么意思,请教高手

hugh2006 发布于 2008-07-13 18:22, 749 次点击
Problem A. Matrix
Description:

    Given an n*n matrix A, whose entries Ai,j are integer numbers ( 0 <= i < n, 0 <= j < n ). An operation SHIFT at row i ( 0 <= i < n ) will move the integers in the row one position right, and the rightmost integer will wrap around to the leftmost column.

 

    You can do the SHIFT operation at arbitrary row, and as many times as you like. Your task is to minimize

max0<=j< n{ Cj | Cj=Σ0<=i<n Ai,j }   

Input:

    The input consists of several test cases. The first line of each test case contains an integer n. Each of the following n lines contains n integers, indicating the matrix A. The input is terminated by a single line with an integer −1. You may assume that 1 <= n <= 7 and |Ai,j| < 104.

Output:

    For each test case, print a line containing the minimum value of the maximum of column sums.

Sample Input:

2
4 6
3 7
3
1 2 3
4 5 6
7 8 9
-1
Sample Output:

11
15
4 回复
#2
xin2222008-07-13 20:25
题目A  矩阵

描述:
    给定一个n行n列的矩阵A,矩阵元素为整型数字,用Ai,j表示第i行j列的元素。对某一行的操作SHIFT实现把该行的元素往右移一位,最右边的数字移到最左边。
    你可以对任意行作SHIFT操作任意次,最终使得各纵列数字的和中最大的那一个最小。

输入:
    可以一次输入几个矩阵,每一个矩阵的输入格式为,第一行为表示行列数的数字n,接下来的n行每行有n个数字,表示矩阵中的元素。最后以-1作为单独一行结束输入。可以假设1<=n<=7,|Ai,j|<=104

输出:
    对于每一个矩阵输出一行结果,结果内容为该矩阵调整之后各纵列之和的最大值。

下面是一个输入输出的例子:
Sample Input:

2
4 6
3 7
3
1 2 3
4 5 6
7 8 9
-1
Sample Output:

11
15
#3
hugh20062008-07-14 00:34
知道了,谢谢
#4
leeco2008-07-14 02:04
1 <= n <= 7 说明这是一道硬搞题。
#5
xin2222008-07-14 08:31
我试着写了一下。

//矩阵类
class CMatrix
{
public:
    int m_n;                   //矩阵行、列数(本程序中使用的矩阵为方阵)
    int m_matrix[7][7];        //储存矩阵元素的数组
    int m_sumclo[7];           //记录纵列数字之和
    int m_maxsum;              //纵列数字之和的最大值
public:
    void SHIFT(int i);         //实现将第j行元素向右移一位
    void Adjustment();         //调整矩阵使其纵列之和的最大值最小
public:
    CMatrix(void);
public:
    ~CMatrix(void);
};

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#include "Matrix.h"
#include "math.h"

void CMatrix::SHIFT(int i)
{
    int j;
    int temp;
    temp=m_matrix[i][m_n-1];
    for(j=m_n-1;j>0;j--)
    {
        m_matrix[i][j]=m_matrix[i][j-1];
    }
    m_matrix[i][0]=temp;
}
void CMatrix::Adjustment()
{
    int i,j,k;
    int max;
    int turn[7]={0};
    m_maxsum=-1;
    for(i=0;i<(int)pow((double)m_n,(double)m_n);i++)
    {
        for(j=0;j<m_n;j++)
        {
            m_sumclo[j]=0;
            for(k=0;k<m_n;k++)
            {
                m_sumclo[j]+=m_matrix[k][j];
            }
        }
        max=0;
        for(j=0;j<m_n;j++)
        {
            if(m_sumclo[j]>max)
            {
                max=m_sumclo[j];
            }
        }
        if(m_maxsum==-1)
        {
            m_maxsum=max;
        }
        else
        {
            if(m_maxsum>max)
            {
                m_maxsum=max;
            }
        }
        SHIFT(0);
        turn[0]++;
        for(j=0;j<m_n;j++)
        {
            if(turn[j]==m_n)
            {
                turn[j]=0;
                SHIFT(j+1);
                turn[j+1]++;
            }
        }
    }
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#include "Matrix.h"
#include <iostream>
using namespace std;
int main()
{
    int i,j;
    int n;
    int num=0;
    CMatrix matrix[7];
    cout<<"请输入:"<<endl;
    while(1)
    {
        cin>>n;
        if(n==-1)
        {
            break;
        }
        if(0<n&&n<=7)
        {
            matrix[num].m_n=n;
            for(i=0;i<n;i++)
            {
                for(j=0;j<n-1;j++)
                {
                    cin>>matrix[num].m_matrix[i][j];
                }
                cin>>matrix[num].m_matrix[i][j];
            }
            num++;
        }
        else
        {
            cout<<"输入出错!"<<endl;
            break;
        }
    }
    for(i=0;i<num;i++)
    {
        matrix[i].Adjustment();
        cout<<matrix[i].m_maxsum<<endl;
    }
    return 0;
}
1