LoginSignup
1

More than 5 years have passed since last update.

キウイジュース2

Last updated at Posted at 2014-06-20

 さてさて、ここから2週目です。ここからは中々進歩しない俺の成長記録付けみたいな事をするので、結構途方も無いかもしれないです(笑)いつも見守ってくださる方に感謝の気持ちを捧げながらコードを書き上げたいと思います。とりあえずは、全探索ってイメージが直ぐに浮かぶ様なコードが書ければいいのかな・・・?

public class Main {

    int[] thePouring(int[] capacities, int[] bottles, int[] fromId, int[] toId) {
        for(int c = 0; c < fromId.length; c++) {
            if(capacities[toId[c]] < bottles[fromId[c]] + bottles[toId[c]]) {
                bottles[fromId[c]] -= capacities[toId[c]] - bottles[toId[c]];
                bottles[toId[c]] = capacities[toId[c]];
            } else {
                bottles[toId[c]] += bottles[fromId[c]];
                bottles[fromId[c]] = 0;
            }
        }
        return(bottles);
    }


    void doIt() {
        int[] capacities = {700000, 800000, 900000, 1000000};
        int[] bottles = {478478,478478,478478,478478};
        int[] fromId = {2,3,2,0,1};
        int[] toId = {0,1,1,3,2};
        int[] ans = thePouring(capacities, bottles, fromId, toId);
        for(int r = 0; r < ans.length; r++) {
            System.out.print(ans[r] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Main().doIt();
    }

}

 いやー見事にきったないコードですね〜。でも、最初書いたのよりはまだ効率が良い気がする。だって、インクリメント方式だったから処理回数多いもんアレ。こっち繰り返し+分岐だからまだ全探索っぽい雰囲気保ってる・・・と信じてる。

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
1