简体   繁体   English

用 Java 计数

[英]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:我有两个给定的字符串: String a = "111"String b = "132" ,对于这两个 String 我想实现这个计数顺序:

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:另一个例子是,如果我有两个这样的给定字符串:String a = "1111" 和 String b = "1223",我希望得到这个结果:

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" .这些也可以是更长的字符串,如String a = "0100110"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)我正在等待来自用户的这些字符串,条件是字符串a中的每个字符都应该小于或等于字符串b的相同字符位置(字符串 a = "11" 和字符串 b = "00" <= 不允许)

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 .调用较小的数字x和较大的数字y If you calculate y mod 10 ( y % 10 ), you will find the value of the least significant digit, call this n .如果您计算y mod 10 ( y % 10 ),您将找到最低有效数字的值,称为n Similarly, calculate the least significant digit of x , call it m Then, create a temporary variable i which is equal to x initially.同样,计算x的最低有效位,称为m然后,创建一个临时变量i ,它最初等于x Loop until that number is equal to y.循环直到该数字等于 y。

In the body of the loop, first, print i .在循环体中,首先,打印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.然后,如果i的最低有效位(同样,由i % 10计算),称其为o ,小于n ,则将i加一。 Otherwise, if o == n , increase i by 10 - n + m .否则,如果o == n ,则将i增加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 .自然地,如果o > n的情况出现了,那么就会出现问题(即来自用户的无效输入),因为保证x所有数字都小于或等于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)+"");
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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