m0_54132659 2021-03-15 10:54 采纳率: 83.3%
浏览 111
已结题

c # 编程题,会的直接留下代码,感谢

Write a program that examines a square matrix of integers with dimensions N x N. The first input line contains the matrix size N, and the rows of the matrix follow on successive input lines. All numbers in each row will be separated by a single space.

Determine how many matrix columns contain a permutation of the numbers 1, 2, ..., N (i.e. each of the numbers 1, 2, ..., N appears exactly once in the column). Write this number to the output.

Note: N may be as large as 1,000. If your program runs in O(N^3), it will be too slow to pass all the tests.

Example:

Input:

5  
1 1 3 4 5  
2 2 3 1 5  
4 3 5 5 1
12 4 23 2 3  
5 5 3 3 2

Output:

2
  • 写回答

4条回答 默认 最新

  • Go 旅城通票 2021-03-15 17:16
    关注

     

     

    using System;
    
    namespace Test
    {
        class Program
        {
            private static void GetMinMax(int[] arr, out int min, out int max)
            {
                min = max = arr[0];
                int n = arr.Length;
                for (var i = 0; i < n; i++)
                {
                    if (min > arr[i]) min = arr[i];
                    if (max < arr[i]) max = arr[i];
                }
            }
            private static bool isConsecutive(int[] arr)
            {
                var n = arr.Length;
                int min, max;
                GetMinMax(arr, out min, out max);
                if (max - min + 1 == n)
                {
                    int i,j;
                    for (i = 0; i < n; i++)
                    {
                        if (arr[i] < 0) j = -arr[i] - min; else j = arr[i] - min;
                        if (arr[j] > 0) arr[j] = -arr[j]; else return false;
                    }
    
                    return true;
                }
                return false;
            }
            static void Main(string[] args)
            {
                int n = int.Parse(Console.ReadLine());
                var arr = new int[n][];
                for (var i = 0; i < arr.Length; i++) arr[i] = new int[n];
    
                for (int i = 0; i < n; i++)
                {
                    var item = Console.ReadLine().Split(' ');
                    for (var j = 0; j < item.Length&&j<n; j++)
                    {
                        arr[j][i] = int.Parse(item[j]);
                    }
                }
                int count = 0;
                for (var i = 0; i < n; i++) {
                    if (isConsecutive(arr[i])) count++;
                }
                Console.Write(count);
               
            }
        }
    
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错