algorithm to sum up a list of numbers for all combinations

前端 未结 15 2297
北荒
北荒 2020-12-04 22:34

I have a list of numbers and I want to add up all the different combinations. For example:

  • number as 1,4,7 and 13
  • the output would be:

15条回答
  •  天涯浪人
    2020-12-04 23:22

    public static void main(String[] args) {
            // this is an example number
            long number = 245L;
            int sum = 0;
    
            if (number > 0) {
                do {
                    int last = (int) (number % 10);
                    sum = (sum + last) % 9;
                } while ((number /= 10) > 0);
                System.err.println("s = " + (sum==0 ? 9:sum);
            } else {
                System.err.println("0");
            }
        }
    

提交回复
热议问题