アイズオンラインジャッジメント(AOJ)のプログラムコード置き場(C#) [ITP1_7]

こんにちは。シジミです。

この記事はAOJで回答したコードを随時置いていきます。

構成としまして、以下のように三弾構成にしています。

環境はvisual sutadio 2019、言語はC#です。

問題:ITP1_7_A

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

namespace ITP_1_7A
{

   class Program
   {
       static void Main(string[] args)
       {

           while(true)
           {
               var line = Console.ReadLine().Trim().Split(' ').Select(s => int.Parse(s)).ToArray();

               int a = line[0];
               int b = line[1];
               int c = line[2];
               string str ="";

               if (a == -1 && b == -1 && c == -1) break;

               if (a == -1 || b == -1||a+b<30) str = "F";
               else if (a + b >= 80) str = "A";
               else if (65<=a + b &&a+b< 80) str = "B";
               else if (50<=a + b &&a+b< 65||c>=50) str = "C";
               else if (30<=a + b &&a+b< 50&&c<50) str = "D";

               Console.WriteLine(str);
           }

           return;
       }

   }
}

問題:ITP1_7_B

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

namespace ITP_1_7B
{
   class Program
   {
       static void Main(string[] args)
       {
           int n = 0;
           while (true)
           {
               var line = Console.ReadLine().Trim().Split(' ').Select(s => int.Parse(s)).ToArray();

               int a = line[0];
               int b = line[1];

               if (a == 0 && b == 0) break;

               for(int i = 1; i < a - 1;i++)
               {
                   for (int j = i+1; j < a; j++)
                   {
                       for (int k = j+1; k <= a; k++)
                       {
                           if (i+j+k==b)
                           {
                               n++;
                           }
                       }
                   }
               }

           Console.WriteLine(n);
               n = 0;
           }
           return;
       }
   }
}

問題:ITP1_7_C

解説:2次元配列array を作成。これに対して入力を1つずつ各要素に足し上げ、最後に全部書き出すといった流れになっています。
 注意点としては、出力時に各行の最後の列のみ改行させること。

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

namespace ITP_1_7C
{
   class Program
   {
       static void Main(string[] args)
       {
           var N = Console.ReadLine().Trim().Split(' ').Select(s => int.Parse(s)).ToArray();

           int a = N[0] + 1;
           int b = N[1] + 1;

           int[,] array = new int[a, b];
           for (int i = 0; i < N[0]; i++)
           {
               var line = Console.ReadLine().Trim().Split(' ').Select(s => int.Parse(s)).ToArray();

               for (int j = 0; j < line.Length; j++)
               {
                   array[i, j] = line[j];
                   array[N[0], j] += line[j];
                   array[i, N[1]] += line[j];
                   array[N[0], N[1]] += line[j];
               }
           }


           for (int i = 0; i <= N[0]; i++)
           {
               for (int j = 0; j <= N[1]; j++)
               {
                   if (j == N[1]) Console.WriteLine("{0}", array[i, j]);
                   else Console.Write("{0} ", array[i, j]);
               }
           }
           return;
       }
   }
}

問題:ITP1_7_D

解説:注意点として、配列をint型にすると 100x100 を入れようとするとwrong answer になるので、long型で宣言しておきましょう。

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

namespace ITP_1_7D
{
   class Program
   {
       static void Main(string[] args)
       {
           var N = Console.ReadLine().Trim().Split(' ').Select(s => int.Parse(s)).ToArray();

           int n = N[0];
           int m = N[1];
           int l = N[2];

           long[,] arrayA = new long[n, m];
           long[,] arrayB = new long[m, l];
           long[,] arrayC = new long[n, l];
           for (int i = 0; i < n+m; i++)
           {
               var line = Console.ReadLine().Trim().Split(' ').Select(s => int.Parse(s)).ToArray();

               if (i < n)
               {
                   for (int j = 0; j < m; j++)
                   {
                       arrayA[i, j] = line[j];
                   }
               }
               else
               {
                   for (int j = 0; j < l; j++)
                   {
                       arrayB[i - n, j] = line[j];
                   }
               }
           }

           for (int i = 0; i < n; i++)
           {
               for (int j = 0; j < l; j++)
               {
                   for (int k = 0; k < m; k++)
                   {
                       arrayC[i, j] += arrayA[i, k] * arrayB[k, j];
                   }
               }
           }

           for (int i = 0; i < n; i++)
           {
               for (int j = 0; j < l; j++)
               {
                   if (j == l - 1) Console.WriteLine("{0}", arrayC[i, j]);
                   else Console.Write("{0} ", arrayC[i, j]);
               }
           }

           return;
       }

   }
}

この記事が気に入ったらサポートをしてみませんか?