简体   繁体   中英

Counting with Java

I have two given Strings: String a = "111" and String b = "132" , for these two String I want to achieve this count order:

111
112
121
122
131
132

Other example is if I have two given String like this: String a = "1111" and String b = "1223", I expect this result:

1111
1112
1113
1121
1122
1123
1211
1212
1213
1221
1222
1223

These can be also longer strings like String a = "0100110" and String b = "01101120" .

I'm waiting these Strings from user in condition that every character in String a should be lower or equal than the same character position in String b (String a = "11" and String b = "00" <= not allowed)

This is a recursive method till now but it doesn't work very well because it generates number twice or more depending on the input:

public void expand(String l,String h){
for(int i=l.length()-1; i>=0; i--)
{
    sb = new StringBuffer(l);               
    if(charToDigit(l.charAt(i)) < charToDigit(h.charAt(i))) {           
        sb.replace(i, i+1, inc(sb.charAt(i)));  
        expand(sb.toString(),h);
        System.out.println(sb.toString());
    } 
}
}

Call the smaller number x and the larger number y . If you calculate y mod 10 ( y % 10 ), you will find the value of the least significant digit, call this n . Similarly, calculate the least significant digit of x , call it m Then, create a temporary variable i which is equal to x initially. Loop until that number is equal to y.

In the body of the loop, first, print i . Then, if the least significant digit of i (again, calculated by i % 10 ), call it o , is less than n , increment i by one. Otherwise, if o == n , increase i by 10 - n + m . Naturally, if it is ever the case that o > n , something went wrong (ie invalid input from the user), since the guarantee was that all digits of x are less than or equal to the corresponding digits in y .

So, in pseudocode:

x = smaller number
y = larger number
n = y % 10
m = x % 10
i = x

while (i <= y):
    print i
    o = i % 10
    if (o < n):
        i += 1
    else if (o == n):
        i += 10 - n + m

Here is my solution

static String l="000";
static String h="232";
static ArrayList<String> combinations = new ArrayList<String>(); 
static int stringLength= l.length();

for(int i=0; i<rulelength; i++)
{
    combinations.add((charToDigit(h.charAt(i)) - charToDigit(l.charAt(i))+1)+"");
}
int number = 1;
for(int i=0; i<combinations.size(); i++)
{
    number*=Integer.parseInt(combinations.get(i));
}
int change = Integer.parseInt(combinations.get(combinations.size()-1));
expand(l, h, change, number);

public static void expand(String l, String h, int change, int comb)
{ 
    StringBuffer sb = new StringBuffer(l);
    int pos = stringLength-1;
    int tmpPos = pos;
    for(int i=1; i<=comb; i++)
    {
        System.out.println(sb.toString());
        sb.replace(pos, pos+1, inc(sb.charAt(pos)));
        if((i % change)==0) {   
            for(int j=stringLength-1; j>0; j--)
            {
                if(charToDigit(sb.charAt(j)) >= (Integer.parseInt(combinations.get(j))-1)) 
                    tmpPos = j-1;                   
                else
                    break;
            }
            sb.replace(tmpPos, tmpPos+1, inc(sb.charAt(tmpPos)));
            for(int j=stringLength-1; j>tmpPos; j--)
            {
                sb.replace(j, j+1, l.charAt(j)+"");
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM