以下の内容はhttps://anton0825.hatenablog.com/entry/20110209/1297593161より取得しました。


SRM495 Div2 Easy

シミュレーションするだけですが、indexをforループの外で宣言しているのを忘れて初期化忘れの不具合が出てしまいました。

using System;
using System.Collections.Generic;
using System.Text;
 
 
public class CarrotBoxesEasy
{
    public int theIndex(int[] carrots, int K)
    {
        int index = 0;
        for (int k = 0; k < K; k++)
        {
            index = 0; //これを忘れた
            for (int i = 1; i < carrots.Length; i++)
                if (carrots[index] < carrots[i])
                    index = i;
            carrots[index]--;
        }
        return index;
    }
}

下記のようにforループの中でindexを返すようにしたほうがいいかもしれません。

using System;
using System.Collections.Generic;
using System.Text;
 
 
public class CarrotBoxesEasy
{
    public int theIndex(int[] carrots, int K)
    {
        for (int k = 0; k < K; k++)
        {
            int index = 0;
            for (int i = 1; i < carrots.Length; i++)
                if (carrots[index] < carrots[i])
                    index = i;
            carrots[index]--;
            if(k.Equals(K - 1)) return index;
        }
        throw new Exception("error");
    }
}



以上の内容はhttps://anton0825.hatenablog.com/entry/20110209/1297593161より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14