Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

複数の整数の合計が21になる組み合わせの算出

More than 1 year has passed since last update.

2~4つの整数の合計が特定の数になる数値の組み合わせを算出します。
用途はごくごく限られると思いますが、手計算で行うと地味に時間がかかるので書いてみました。
カウンタの数とwhile文のネストを変えれば変数の数の変化にも対応できます。

main.kt
fun main(args: Array<String>) {
        //元数値作成
        val aon= arrayListOf<Int>()

        var i=0
        while (i<=20){
            aon.add(20-i)
            i+=1
        }


        //組み合わせのカウンタ
        var one = 0
        var two = 0
        var thr = 0
        var fur = 0



        //格納用
        val answer_21 = arrayListOf<String>()
        val answer_20 = arrayListOf<String>()
        val answer_19 = arrayListOf<String>()



        //組み合わせ調査開始
        while(one <aon.count()){
            two = one

            while (two <aon.count()){
                thr =two

                while (thr <aon.count()){
                    fur = thr

                    while (fur<aon.count()){



                        if((aon[one]+aon[two]+aon[thr]+aon[fur])==21){

                            answer_21.add(aon[one].toString()+"_"+aon[two].toString()+"_"+aon[thr].toString()+"_"+aon[fur].toString())

                        }

                        if((aon[one]+aon[two]+aon[thr]+aon[fur])==20){

                            answer_20.add(aon[one].toString()+"_"+aon[two].toString()+"_"+aon[thr].toString()+"_"+aon[fur].toString())

                        }

                        if((aon[one]+aon[two]+aon[thr]+aon[fur])==19){

                            answer_19.add(aon[one].toString()+"_"+aon[two].toString()+"_"+aon[thr].toString()+"_"+aon[fur].toString())

                        }


                        fur +=1
                    }


                 thr +=1
                }

              two+=1
            }

            one+=1
        }


        //正解の出力
        print("21になる組み合わせは・・・")
        println(answer_21)

        print("20になる組み合わせは・・・")
        println(answer_20)

        print("19になる組み合わせは・・・")
        println(answer_19)


}


21になる組み合わせは・・・[20_1_0_0, 19_2_0_0, 19_1_1_0, 18_3_0_0, 18_2_1_0, 18_1_1_1, 17_4_0_0, 17_3_1_0, 17_2_2_0, 17_2_1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4_0_0, 15_3_1_0, 15_2_2_0, 15_2_1_1, 14_5_0_0, 14_4_1_0, 14_3_2_0, 14_3_1_1, 14_2_2_1, 13_6_0_0, 13_5_1_0, 13_4_2_0, 13_4_1_1, 13_3_3_0, 13_3_2_1, 13_2_2_2, 12_7_0_0, 12_6_1_0, 12_5_2_0, 12_5_1_1, 12_4_3_0, 12_4_2_1, 12_3_3_1, 12_3_2_2, 11_8_0_0, 11_7_1_0, 11_6_2_0, 11_6_1_1, 11_5_3_0, 11_5_2_1, 11_4_4_0, 11_4_3_1, 11_4_2_2, 11_3_3_2, 10_9_0_0, 10_8_1_0, 10_7_2_0, 10_7_1_1, 10_6_3_0, 10_6_2_1, 10_5_4_0, 10_5_3_1, 10_5_2_2, 10_4_4_1, 10_4_3_2, 10_3_3_3, 9_9_1_0, 9_8_2_0, 9_8_1_1, 9_7_3_0, 9_7_2_1, 9_6_4_0, 9_6_3_1, 9_6_2_2, 9_5_5_0, 9_5_4_1, 9_5_3_2, 9_4_4_2, 9_4_3_3, 8_8_3_0, 8_8_2_1, 8_7_4_0, 8_7_3_1, 8_7_2_2, 8_6_5_0, 8_6_4_1, 8_6_3_2, 8_5_5_1, 8_5_4_2, 8_5_3_3, 8_4_4_3, 7_7_5_0, 7_7_4_1, 7_7_3_2, 7_6_6_0, 7_6_5_1, 7_6_4_2, 7_6_3_3, 7_5_5_2, 7_5_4_3, 7_4_4_4, 6_6_6_1, 6_6_5_2, 6_6_4_3, 6_5_5_3, 6_5_4_4, 5_5_5_4]
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away