How to print out the number of a specific digit along with the digit itself in form "nxw." n is the frequency of the number, w is the number itself.
So, for example, if the user's input was 1 1 1. The output would be 3x1.
If the user's input was 1 1 1 at the first line and 7 7 1 1 0 at the second line. The output would be 3x1.2x7.2x1.1x0. with no spaces.
Note:
loop ends with a dot.
numbers don't have to be in a specific order
user can input as many digits as they want.
So for example, Input can be 1 1 1 at the first line 7 7 1 1 0 at the second ... etc.
This is my code so far. But I know that it's not true.
import java.util.*;
public class LaufLaengenKodierung {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int freq = 0;
int oldNum = 0;
int num = 0;
boolean first = true;
while(sc.hasNextInt()) {
int i = sc.nextInt();
if(i == oldNum) {
freq++;
num = i;
} else if(i != oldNum) {
freq = 1;
oldNum = i;
num = i;
if(first) {
first = false;
num = i;
freq = 1;
}
}
}
System.out.print(freq + "x" + num + ".");
sc.close();
}
}
Existing code needs to be slightly refactored to print the frequency and integer value as soon as a sub-sequence of the same values ends.
Tests:
Output:
Update If separate digits need to be counted only (not the integer numbers), e.g. input
11 11 11
should be converted to6x1.
instead of3x11.
as shown above, the method should be refactored to process the digits inside numbers:Output for tests:
"11 11 11", "112 223", "1 1 1\n7 7 1 1 0", "0 0 0"
:Online demo of both methods
printRLE
andprintRLEDigits