-2

I'd better express this with a example, I'm trying to write a program, say I have a function like this:

static List<Integer> int2List(int l, int base)

the input and output should be

l=0, base=3, return {1}
l=1, base=3, return {2}
l=2, base=3, return {3}
l=3, base=3, return {1,1}
l=4, base=3, return {1,2}
l=5, base=3, return {1,3}
l=6, base=3, return {2,1}
l=7, base=3, return {2,2}
l=8, base=3, return {2,3}
l=9, base=3, return {3,1}
l=10, base=3, return {3,2}
l=11, base=3, return {3,3}
l=12, base=3, return {1,1,1}
l=13, base=3, return {1,1,2}
l=14, base=3, return {1,1,3}

... It's quite like recursively mod but there are some condition I can't figure out.

Thanks

Thanks for all your comments and I've updated the question content and the signature of the method, I wanted to display the content of a list and I forgot to add comma between elements.

So here's my code, it's not correct yet but I think you can get some sense from it. It's also quite like the Long.toString(int i, int radix), the differences are first I need to add those leading zeros, and second I need to do a "+1" from every element in the list, but doing that not gives me correct either...

    static List<Integer> int2List(int l, int base) {
//        String s = Long.toString(l,base);
        List<Integer> list = new ArrayList<Integer>();

        int k = l % base;
        int j = l / base;
        int m = 0;
        list.add((int) (k + 1));
        while (j > base) {
            k = j % base;
            list.add((int) (k));
            if (j == base)
                break;
            j = j / base;
        }
        if (j == base) {
            list.add((int) (j));
        } else {
            if (j > 0) {
                list.add((int) (j));
            }
        }
        Collections.reverse(list);
        return list;
    }

I'm aware of the mistakes I've made in the first version, I now fix my mistakes and update the question to be more specific, can you remove the "not worth reading" please, I'll really appreciate..

===================

Hmm, the method is supposed to be used in a loop, such that it will output all possible sequences composed by number in range [1,base], and the l can be seen as an index.

Li Wang
  • 13
  • 4
  • your example does not make much sense. Why do you want to get a single value list and not just return the value? Can you explain what your method is intended to do? – Michael Lihs Jan 24 '17 at 21:49
  • why does it return list? – maszter Jan 24 '17 at 21:49
  • from what you showed, it looks like the function increases both the input number and base by 1 then returns the representation of the modified base. – Shiping Jan 24 '17 at 21:51
  • I think you need to do your homework, try to write some code and get back with a more specific question. Good luck! – JonDoe297 Jan 24 '17 at 21:52

2 Answers2

2

How about this?

static List<Integer> int2List(int l, int base)
{
    List<Integer> list = new ArrayList<Integer>();
    int n;
    l++;
    while(l > 0)
    {
       n = (l-1) % base;
       list.add(0, (int) n+1);
       l = (l-n)/base; 
    }
    return list;
}
Shiping
  • 1,203
  • 2
  • 11
  • 21
0

I think your method signature is wrong. The function seems to convert a numerical value into the Base representation of it.

This would mean, that the return value would have to contain String.

For what reason you are using a List<...> there is unclear to me.

Ray
  • 686
  • 3
  • 6