对于给定的输入给出以下输出的程序


Eg 1: Input: a1b10
       Output: abbbbbbbbbb
Eg: 2: Input: b3c6d15
          Output: bbbccccccddddddddddddddd
The number varies from 1 to 99.

我尝试过的:

爪哇
static void printCharSeq(char c, int num) {
		while (num > 1) {
			System.out.print(c);
			num--;
		}
	}

void convert(String str) {
		char c[] = str.toCharArray();
		for (int i = 0; i < c.length; i++) {
			int val = 0;
			if ((c[i] >= '0' && c[i] <= '9')) {
				// val=c[i]-48;
				try {
					if (c[i + 1] >= '0' && c[i + 1] <= '9') {
						String a1 = String.valueOf(c[i] - 48);
						String a2 = String.valueOf(c[i + 1] - 48);
						val = Integer.parseInt(a1 + a2);
						// System.out.println(val);
						printCharSeq(c[i - 1], val);
						i++;
					} else {
						val = c[i] - 48;
						printCharSeq(c[i - 1], val);
					}
				} catch (ArrayIndexOutOfBoundsException ex) {
					val = c[i] - 48;
					printCharSeq(c[i - 1], val);
				}
			} else {
				System.out.print(c[i]);
				// printCharSeq(c[i], val);
			}
		}
	}
Here This logic works perfectly but when i try to 
input : v99t100cvrx2
Output : vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvttttttttttcvrxx

it prints t 10 times too. i don't know ether it is correct or wrong. please help me to find it out the logic is correct for different inputs.

解决方案1

引用:

它也打印 t 10 次。 我不知道这是正确还是错误。

我认为这是正确的,因为您还指出:

引用:

数量从 1 到 99 不等。

让我们猜测该代码处理只有 2 位数字的数字。
如果您想要超过 2 位数字,则必须相应地更改代码。
我不会使用处理 1 位数字的代码和处理 2 位数字的代码,而是使用循环。

Eg 1: Input: a1b10
       Output: abbbbbbbbbb

该编码是 RLE 游程编码
游程编码 – 维基百科[^]

解决方案2

为了将数字字符序列转换为整数值,您需要一个像这样的简单循环:

Set value = 0
For each character:
    if character is a number:
        set tempval = character - '0'
        multiply value by 10
        add tempval to value
    else:
        print the current character value times
// continue with the remaining processing.

解决方案3

下面的代码是给定数字的字符序列的完整代码,条件是限制在1-99之间,则不超过限制,如果超过则取数字的前两位
例如,如果它的100=10次打印相应的字符,这个程序是基于数组的位置,在你这样做之前,你必须考虑它,否则它会破坏整个代码

爪哇
import java.util.Scanner;
class CharSequence
{
static void PrintSeq(char c, int num) 
{
		while (num > 1) 
		{
			System.out.print(c);
			num--;
		}
}

static void ToConvert(String str) 
   {
		char c[] = str.toCharArray();
		
		for (int i = 0; i < c.length; i++) 
		{
			int val = 0;
			
			if ( c[i] >= '0' && c[i] <= '9' ) 
			{
				try 
				{
					if ( c[i + 1] >= '0' && c[i + 1] <= '9' ) 
					{
					//Used to print more than 9 values
					String a1 = String.valueOf(c[i] - 48);
					String a2 = String.valueOf(c[i + 1] - 48);
						
						val = Integer.parseInt(a1 + a2);
						
						PrintSeq(c[i - 1], val);
						
						i++;
					}
					else 
					{
						//Used to print 0-9 values
						val = c[i] - 48;
						PrintSeq(c[i - 1], val);
					}
				} 
				catch (ArrayIndexOutOfBoundsException ex) 
				{
					val = c[i] - 48;
					PrintSeq(c[i - 1], val);
				}				
			} 
			else 
			{
				System.out.print(c[i]);				
			}
		}
	}
	public static void main(String args[]) throws Exception
	{
		Scanner s=new Scanner(System.in);
		
		String wrd=s.nextLine();
		ToConvert(wrd);
	}
}

解决方案8

public static void main(String[] args) {
        String str="a20b13c11";
        String val="0";
        char letter = 0;
        for(int i=0;i<str.length();i++)
        {
            if(Character.isAlphabetic((str.charAt(i))) ){
                for(int j=0;j<Integer.parseInt(val);j++)
                {
                    System.out.print(letter);
                }
                val="0";
               letter=str.charAt(i);
            }
            else {

                val=val+str.charAt(i);
                if(i==str.length()-1) {
                    for (int j = 0; j < Integer.parseInt(val); j++) {
                        System.out.print(letter);
                    }
                }
                    
                }
            }



        }

コメント

タイトルとURLをコピーしました