QianYi Ke? 2020-04-04 12:39 采纳率: 100%
浏览 455
已采纳

问题如下,请使用C++?

请用C++
Vestigium means "trace" in Latin. In this problem we work with Latin squares and matrix traces.
The trace of a square matrix is the sum of the values on the main diagonal (which runs from the upper left to the lower right).
An N-by-N square matrix is a Latin square if each cell contains one of N different values, and no value is repeated within a row or a column. In this problem, we will deal only with "natural Latin squares" in which the N values are the integers between 1 and N.
Given a matrix that contains only integers between 1 and N, we want to compute its trace and check whether it is a natural Latin square. To give some additional information, instead of simply telling us whether the matrix is a natural Latin square or not, please compute the number of rows and the number of columns that contain repeated values.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each starts with a line containing a single integer N: the size of the matrix to explore. Then, N lines follow. The i-th of these lines contains N integers Mi,1, Mi,2 ..., Mi,N. Mi,j is the integer in the i-th row and j-th column of the matrix.
Output
For each test case, output one line containing Case #x: k r c, where x is the test case number (starting from 1), k is the trace of the matrix, r is the number of rows of the matrix that contain repeated elements, and c is the number of columns of the matrix that contain repeated elements.
Limits
Test set 1 (Visible Verdict)
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
2 ≤ N ≤ 100.
1 ≤ Mi,j ≤ N, for all i, j.
Sample

Input

3
4
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
4
2 2 2 2
2 3 2 3
2 2 2 3
2 2 2 2
3
2 1 3
1 3 2
1 2 3

Output
Case #1: 4 0 0
Case #2: 9 4 4
Case #3: 8 0 2

In Sample Case #1, the input is a natural Latin square, which means no row or column has repeated elements. All four values in the main diagonal are 1, and so the trace (their sum) is 4.
In Sample Case #2, all rows and columns have repeated elements. Notice that each row or column with repeated elements is counted only once regardless of the number of elements that are repeated or how often they are repeated within the row or column. In addition, notice that some integers in the range 1 through N may be absent from the input.
In Sample Case #3, the leftmost and rightmost columns have repeated elements

  • 写回答

2条回答 默认 最新

  • 白色一大坨 2020-04-04 21:30
    关注

    代码如下:

    #include <iostream>
    #include <vector>
    #include <windows.h>
    using namespace std;
    struct result
    {
        int a;
        int b;
        int c;
    };
    
    int main() {
        int i,j,k,l,n,m;
        bool row;
        bool *col;
        int **matrix;
        vector<result> res;
        result r;
        cin>>n;
        for (i=0;i<n;i++)
        {
            cin>>m;
            matrix = new int *[m];  
            for (j = 0;j < m; j++) matrix[j] = new int[m];
            col = new bool[m];
            for (j = 0;j < m; j++) col[j] = false;
            memset(&r, 0, sizeof(result));
    
            for (j=0;j<m;j++)
            {
                row = false;
                for (k=0;k<m;k++)
                {
                    cin>>matrix[j][k];
                    if(j==k) r.a+=matrix[j][k];
                    if (k>0 && !row)
                    {
                        for (l=0;l<k;l++)
                        {
                            if(matrix[j][l] == matrix[j][k])
                            {
                                row = true;
                                r.b++;
                                break;
                            }
                        }
                    }
    
                    if (j>0 && !col[k])
                    {
                        for (l=0;l<j;l++)
                        {
                            if(matrix[l][k] == matrix[j][k])
                            {
                                col[k] = true;
                                r.c++;
                                break;
                            }
                        }
                    }
                }
            }
    
            for (j = 0;j < m;j++)  delete []matrix[j];
            delete []matrix;
            delete []col;
            res.push_back(r);
        }
    
        for (i=0;i<res.size();i++)
        {
            cout<<"Case #" << i+1 <<":"<<res[i].a<<" "<<res[i].b<<" "<<res[i].c<<" "<<endl;
        }
        res.clear();
        system("pause");
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题