为给定输入提供以下输出的程序 [英] A program to give the following output for the given input
本文介绍了为给定输入提供以下输出的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Eg 1: Input: a1b10
Output: abbbbbbbbbb
Eg: 2: Input: b3c6d15
Output: bbbccccccddddddddddddddd
The number varies from 1 to 99.
我尝试过:
What I have tried:
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.
推荐答案
引用:
它也打印了10次。我不知道以太它是对还是错。
it prints t 10 times too. i don't know ether it is correct or wrong.
我认为这是正确的,因为你还说:
I think it is correct because you also state that:
Quote:
数字从1到99不等。
让我们猜测代码只处理2位数字。
如果你想要超过2位数,你必须相应地改变代码。
而不是让代码处理1位数和代码来处理2位数,我会使用循环。
Lets guess that the code handle numbers with only 2 digits.
If you want more than 2 digits, you have to change the code accordingly.
Rather than having code to handle 1 digit and code to handle 2 digits, I would use a loop.
Eg 1: Input: a1b10
Output: abbbbbbbbbb
这个编码是RLE运行长度编码
行程编码 - 维基百科 [ ^ ]
为了将一系列数字字符转换为整数值,您需要一个简单的循环:
In order to convert a sequence of numeric characters to an integer value you need a simple loop like this:
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.
以下代码是给定数字的字符序列的完整代码,并且条件是限制在1-99之间且不超过限制,如果是,则需要数字的前两位数
例如,如果它的100 = 10次打印相应的字符,这个程序是基于数组的位置,在你做之前,你必须考虑它否则会破坏整个代码
The below code is the complete code of Character Sequence of a given Number and the condition given that the limit is between 1-99 it does not exceeds the limit,if it does it takes the first two digit of the number
For example, if its 100=10 times it prints the respective character,This program is based on the array's positions,before you do,you have to consider it otherwise it spoil the entire code
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);
}
}
这篇关于为给定输入提供以下输出的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文