LoginSignup
0
0

More than 3 years have passed since last update.

カレンダーの表示 (paizaランク B 相当)

Last updated at Posted at 2020-03-21

問題文

解説を視る前に書いたコード(エラーになるが原因は分かっていない)

    class Program
    {
        static int[] NextDay(int year,int month, int day)
        {
            day++;
            if(day > LastDay(year, month))
            {
                day = 1;
                month++;
            }
            if(month > 12)
            {
                month = 1;
                year++;
            }
            return new int[] { year, month, day };
        }
        //1800年1月1日からの日数を計算するメソッド
        static int CountDay (int year,int month,int day)
        {
            int y = 1800, m = 1, d = 1;
            int count = 0;
            //400年分の日数
            int daysOf400years = 365 * 400 + (400 / 4) - (400 / 100) + (400 + 400);
            //400年の倍数飛ぶ
            count += ((year - 1800) / 400) * daysOf400years;
            y = year - (year - 1800) % 400;
            while(!(y == year && m == month && d == day))
            {
                count++;
                int[] nextDay = NextDay(y, m, d);
                y = nextDay[0];
                m = nextDay[1];
                d = nextDay[2];
            }
            return count;
        }
        static int LastDay(int year, int month)
        {
            switch (month)
            {
                case 2:
                    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
                    {
                        return 29;
                    }
                    else
                    {
                        return 28;
                    }
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
                default:
                    return 31;
            }
        }
        static int WhatDay(int year, int month, int day)
        {
            int dayOfweek = 3; //1800年1月1日は水曜日
            int count = CountDay(year, month, day);
            return (dayOfweek + count) % 7;
        }
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Trim().Split(' ');
            int y = int.Parse(input[0]);
            int m = int.Parse(input[1]);
            //y年m月1日の曜日を求める
            int dayOfWeek = WhatDay(y, m,1);
            //日→0、月→1、・・・・ 土→6
            //y年m月は何日か
            int lastDay = LastDay(y, m);
            for(int i = 0; i < dayOfWeek; i++)
            {
                Console.Write("   ");
            }

            for(int i = 1; i < 10; i++)
            {
                Console.Write(" {0}", i);
                //日から金なら後ろに半角スペース、土なら改行
                if(dayOfWeek != 6)
                {
                    Console.Write(" ");
                }
                else
                {
                    Console.WriteLine();
                }
                dayOfWeek = (dayOfWeek + 1) % 7;
            }
            for(int i = 10;i < lastDay+1; i++)
            {
                Console.Write("{0}", i);
                //日から金なら後ろに半角スペース、土なら改行
                if (dayOfWeek != 6)
                {
                    Console.Write(" ");
                }
                else
                {
                    Console.WriteLine();
                }
                dayOfWeek = (dayOfWeek + 1) % 7;
            }
            for(int i = dayOfWeek; i < 7; i++)
            {
                Console.Write("  ");
                if (i != 6)
                {
                    Console.Write(" ");
                }
                else
                {
                    Console.WriteLine();
                }
            }
        }
    }
YouTubeの解説動画を視て書いたもの

メモ string.Formatメソッドで桁数を指定する方法[

参考ページ


string.Format("値は{0,5}です。", 10);

string.Format("値は{0,-5}です。", "ABC");

{}の中の変数を指定する番号の後に、カンマで区切って指定する。

プラスで右寄せマイナスで左寄せ


    class Program
    {
        static int[] NextDay(int year, int month, int day)
        {
            day++;
            if (day > LastDay(year, month))
            {
                day = 1;
                month++;
            }
            if (month > 12)
            {
                month = 1;
                year++;
            }
            return new int[] { year, month, day };
        }

        static int LastDay(int year, int month)
        {
            switch (month)
            {
                case 2:
                    if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
                    {
                        return 29;
                    }
                    else
                    {
                        return 28;
                    }
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
                default:
                    return 31;
            }
        }
        static int DayOfWeek(int year, int month, int day)
        {
            int y = 1800, m = 1, d = 1, dow = 3;
            while(!(y == year && m == month && d == day))
            {
                int[] date = NextDay(y, m, d);
                y = date[0];
                m = date[1];
                d = date[2];
                dow = (dow + 1) % 7;
            }
            return dow;
        }
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Trim().Split(' ');
            int y = int.Parse(input[0]);
            int m = int.Parse(input[1]);
            string[][] cal = new string[6][];
            for(int i = 0; i < 6; i++)
            {
                cal[i] = new string[7];
                for(int j = 0; j < 7; j++)
                {
                    cal[i][j] = "  ";
                }
            }
            //calを更新する
            int line = 0, column = DayOfWeek(y, m, 1);
            int day = 1;
            while(day <= LastDay(y, m))
            {
                cal[line][column] = string.Format("{0,2}", day); //プラスで右寄せ、マイナスで左寄せ
                day++;
                line += (column + 1) / 7; 
                column = (column + 1) % 7;
            }
            DisplayCalendar(cal);
        }
        static void DisplayCalendar(string[][] cal)
        {
            for(int i = 0;i < cal.Length;i++)
            {
                Console.Write(cal[i][0]);
                for(int j = 1; j < cal[i].Length; j++)
                {
                    Console.Write(" {0}", cal[i][j]);
                }
                Console.WriteLine();
            }
        }
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0