3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

POH6 女子高生プログラマーの大バトル〜コボール文明の逆襲〜 C#解答例

Last updated at Posted at 2015-09-20

https://paiza.jp/poh/joshibato
今更ですが解いてみました。

回答をブログ等に書いていただいても問題ありません。

との事ですので、考え方の整理のためにまとめてみます。
ネタバレ申し訳ございません。

No1 霧島京子 すごろく問題

https://paiza.jp/poh/joshibato/kirishima
すごろくで、出目によってゴールできるかを判定する問題でした。

1.すごろくのマス目総数を取得する
2.マス目の指示を配列にセットする
3.試行回数をループ終了の条件にする
4.1回目の移動後、number変数にマス目の位置をセットする(配列は0始まり、マス目は1始まり)
5.number変数がマス目総数以上であれば結果判定に進む
6.number変数がマス目総総数より少なければ、マス目の指示に従って現在位置を移動する(訪れたマス目の番号を記録しておく)
7.1度訪れたマスに再び訪れた場合、ループを中断して結果判定に進む
8.マス目の指示が0であれば、ループを中断して結果判定に進む
9.現在位置が1未満、またはマス目総数以上であればループを中断して結果判定に進む それ以外は6.に戻る
10.結果判定:現在位置がマス目総数と同じであれば"Yes"と表示、それ以外は全て"No"と表示する

answer.cs
using System;
using System.Collections.Generic;
namespace EmptyProjects
{
    class paiza
    {
        static void Main()
        {
            int ArrayCount = int.Parse(Console.ReadLine());
            var Array = Console.ReadLine().Split(' ');
            int TryCount = int.Parse(Console.ReadLine());

            for (int i = 0; i < TryCount; i++)
            {
                int number = int.Parse(Console.ReadLine()) + 1;
                if (number < ArrayCount)
                {
                    List<int> prenumber = new List<int>();
                    do
                    {
                        if (prenumber.Contains(number)) break;
                        prenumber.Add(number);
                        if (int.Parse(Array[number - 1]) == 0) break;
                        else number = number + int.Parse(Array[number - 1]);
                      
                    } while ((number > 1 && number < ArrayCount));
                }

                if (number == ArrayCount) Console.WriteLine("Yes");
                else Console.WriteLine("No");
            }
        }
    }
}

No2 六村リオ コーヒー問題

https://paiza.jp/poh/joshibato/rio
コーヒーにお湯とコーヒー粉末を足して、味見で飲んで、残ったコーヒーの濃度を問う問題でした。
味見で量が減る部分の計算式を思いつかなかったので、問題文にあった計算式を丸写しです。。。
計算にはdecimal型の変数を使いましたが、doubleでも問題ないと思います。
操作種別をswitchで分岐して、足す(お湯orコーヒー)・引くの操作を行い、結果を表示しています。

answer.cs
using System;
namespace EmptyProjects
{
    class paiza
    {
        static void Main()
        {
            int trycount = int.Parse(Console.ReadLine());
            decimal hotwater = 0;
            decimal coffee = 0;

            for (int i = 0; i < trycount; i++)
            {
                var action = Console.ReadLine().Split(' ');
                decimal val = decimal.Parse(action[1]);
                switch (action[0])
                {
                    case "1":
                        hotwater += val;
                        break;
                    case "2":
                        coffee += val;
                        break;
                    case "3":
                        decimal temphw = hotwater;
                        decimal tempcf = coffee;
                        hotwater -= val * temphw / (temphw + tempcf);
                        coffee -= val * tempcf / (temphw + tempcf);
                        break;
                }
            }

            Console.WriteLine((int)(coffee / (hotwater + coffee) * 100));
        }
    }
}

No3 緑川つばめ 報酬問題

https://paiza.jp/poh/joshibato/tsubame
報酬として2桁の数値が与えられたとき、元の数値に10の位と1の位を加算した数値を返すという問題でした。
書いたコードは4行。
解く順番が逆だったかも知れませんね。。。
変に勘ぐって、条件を何回も見返してしまいました。

answer.cs
using System;
namespace EmptyProjects
{
    class paiza
    {
        static void Main()
        {
            int number = int.Parse(Console.ReadLine());
            int tens = number / 10;
            int singles = number % 10;
            Console.WriteLine(number + tens + singles);
        }
    }
}

paizaは、業務プログラムの開発ではあまり見かけない要件が問題になっていて、私には新鮮です。
今回の例題のように、かなり簡単な問題も多いため、ちょっとだけ頭をひねって解答を思いついたときはスカッとしますね。
慣れてきたら処理時間の高速化やコードゴルフなんかに手を出すと、基礎の理解がどんどん深まるのでおすすめです。
それではまた。

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?