典型90問    次の典型90問へ    前の典型90問へ

典型90問 004 Cross Sum(★2)


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("3 3");
            WillReturn.Add("1 1 1");
            WillReturn.Add("1 1 1");
            WillReturn.Add("1 1 1");
            //5 5 5
            //5 5 5
            //5 5 5
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4 4");
            WillReturn.Add("3 1 4 1");
            WillReturn.Add("5 9 2 6");
            WillReturn.Add("5 3 5 8");
            WillReturn.Add("9 7 9 3");
            //28 28 25 26
            //39 33 40 34
            //38 38 36 31
            //41 41 39 43
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("2 10");
            WillReturn.Add("31 41 59 26 53 58 97 93 23 84");
            WillReturn.Add("62 64 33 83 27 95 2 88 41 97");
            //627 629 598 648 592 660 567 653 606 662
            //623 633 651 618 645 650 689 685 615 676
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("10 10");
            WillReturn.Add("83 86 77 65 93 85 86 92 99 71");
            WillReturn.Add("62 77 90 59 63 76 90 76 72 86");
            WillReturn.Add("61 68 67 79 82 80 62 73 67 85");
            WillReturn.Add("79 52 72 58 69 67 93 56 61 92");
            WillReturn.Add("79 73 71 69 84 87 98 74 65 70");
            WillReturn.Add("63 76 91 80 56 73 62 70 96 81");
            WillReturn.Add("55 75 84 77 86 55 96 79 63 57");
            WillReturn.Add("74 95 82 95 64 67 84 64 93 50");
            WillReturn.Add("87 58 76 78 88 84 53 51 54 99");
            WillReturn.Add("82 60 76 68 89 62 76 86 94 89");
            //1479 1471 1546 1500 1518 1488 1551 1466 1502 1546
            //1414 1394 1447 1420 1462 1411 1461 1396 1443 1445
            //1388 1376 1443 1373 1416 1380 1462 1372 1421 1419
            //1345 1367 1413 1369 1404 1368 1406 1364 1402 1387
            //1416 1417 1485 1429 1460 1419 1472 1417 1469 1480
            //1410 1392 1443 1396 1466 1411 1486 1399 1416 1447
            //1397 1372 1429 1378 1415 1408 1431 1369 1428 1450
            //1419 1393 1472 1401 1478 1437 1484 1425 1439 1498
            //1366 1390 1438 1378 1414 1380 1475 1398 1438 1409
            //1425 1442 1492 1442 1467 1456 1506 1417 1452 1473
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[,] BanArr = CreateBanArr(InputList.Skip(1));
        int UB_X = BanArr.GetUpperBound(0);
        int UB_Y = BanArr.GetUpperBound(1);

        // 横計を求める
        var YokoSumDict = new Dictionary<int, int>();
        for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
            int YokoSum = 0;
            for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
                YokoSum += BanArr[LoopX, LoopY];
            }
            YokoSumDict[LoopY] = YokoSum;
        }

        // 縦計を求める
        var TateSumDict = new Dictionary<int, int>();
        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            int TateSum = 0;
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                TateSum += BanArr[LoopX, LoopY];
            }
            TateSumDict[LoopX] = TateSum;
        }

        for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
            var AnswerList = new List<int>();
            for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
                int CurrSum = YokoSumDict[LoopY] + TateSumDict[LoopX];
                CurrSum -= BanArr[LoopX, LoopY];
                AnswerList.Add(CurrSum);
            }
            Console.WriteLine(IntEnumJoin(" ", AnswerList));
        }
    }

    // セパレータとInt型の列挙を引数として、結合したstringを返す
    static string IntEnumJoin(string pSeparater, IEnumerable<int> pEnum)
    {
        string[] StrArr = Array.ConvertAll(pEnum.ToArray(), pX => pX.ToString());
        return string.Join(pSeparater, StrArr);
    }

    ////////////////////////////////////////////////////////////////
    // IEnumerable<string>をintの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static int[,] CreateBanArr(IEnumerable<string> pStrEnum)
    {
        var StrList = pStrEnum.ToList();
        if (StrList.Count == 0) {
            return new int[0, 0];
        }

        int[] IntArr = { };
        Action<string> SplitAct = pStr =>
            IntArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        SplitAct(StrList[0]);

        int UB_X = IntArr.GetUpperBound(0);
        int UB_Y = StrList.Count - 1;

        int[,] WillReturn = new int[UB_X + 1, UB_Y + 1];

        for (int Y = 0; Y <= UB_Y; Y++) {
            SplitAct(StrList[Y]);
            for (int X = 0; X <= UB_X; X++) {
                WillReturn[X, Y] = IntArr[X];
            }
        }
        return WillReturn;
    }

    ////////////////////////////////////////////////////////////////
    // 2次元配列(int型)のデバッグ出力
    ////////////////////////////////////////////////////////////////
    static void PrintBan(int[,] pBanArr)
    {
        var sb = new System.Text.StringBuilder();
        for (int Y = 0; Y <= pBanArr.GetUpperBound(1); Y++) {
            for (int X = 0; X <= pBanArr.GetUpperBound(0); X++) {
                sb.Append(pBanArr[X, Y]);
            }
            sb.AppendLine();
        }
        Console.Write(sb.ToString());
    }
}


解説

横計と縦計を
前処理で求めてます。