java - java中的运行长度编码

标签 java run-length-encoding

关闭。这个问题需要debugging details .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

9 个月前关闭。




Improve this question




如何以“nxw”形式打印出特定数字的编号以及数字本身。 n 是数字的频率,w 是数字本身。
因此,例如,如果用户的输入是 1 1 1。输出将是 3x1。
如果用户的输入在第一行是 1 1 1,在第二行是 7 7 1 1 0。输出将为 3x1.2x7.2x1.1x0。没有空格。
笔记:

  • 循环以一个点结束。
  • 数字不必按特定顺序排列
  • 用户可以输入任意数量的数字。

  • 例如,输入可以是 1 1 1 在第一行 7 7 1 1 0 在第二行......等等。
    到目前为止,这是我的代码。但我知道这不是真的。
    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();
    }
            
    }
    

    最佳答案

    现有代码需要稍微重构以在相同值的子序列结束时立即打印频率和整数值。

    static void printRLE(String input) {
        Scanner sc = new Scanner(input);
        int freq = 0;
        int oldNum = 0;
        boolean first = true;
            
        while(sc.hasNextInt()) {
                
            int i = sc.nextInt();
            
            if (i != oldNum || first) {
                if (first)
                    first = false;
                else // integer value changed
                    System.out.printf("%dx%d.", freq, oldNum);
                oldNum = i;
                freq = 1;
            } else {
                freq++;
            }
        }
        if (!first)    
            System.out.printf("%dx%d.%n", freq, oldNum);
        else 
            System.out.println("No integer found"); // or print 0x0 if it's correct
        sc.close();    
    }
    
    测试:
    String[] tests = {
        "",
        "abc.",
        "11 11 11",
        "1 1 1\n7 7 1 1 0",
        "0 0 0",
    };    
    
    for (String test: tests) {
        System.out.println("test=[" + test + "]");
        printRLE(test);
        System.out.println("--------");
    }
    
    输出:
    test=[]
    No integer found
    --------
    test=[abc.]
    No integer found
    --------
    test=[11 11 11]
    3x11.
    --------
    test=[1 1 1
    7 7 1 1 0]
    3x1.2x7.2x1.1x0.
    --------
    test=[0 0 0]
    3x0.
    --------
    

    更新
    如果只需要计算单独的数字(而不是整数),例如输入 11 11 11应转换为 6x1.而不是 3x11.如上所示,应该重构该方法以处理数字中的数字:
    static void printRLEDigits(String input) {
        Scanner sc = new Scanner(input);
        int freq = 0;
        int oldNum = 0;
        boolean first = true;
            
        out: while(sc.hasNext()) {
                
            String s = sc.next(); // getting "number" delimited with whitespaces
            for (char c: s.toCharArray()) {
                if (!Character.isDigit(c)) {
                    break out;
                }
                int i = c - '0';
                if (i != oldNum || first) {
                    if (first)
                        first = false;
                    else // digit changed
                        System.out.printf("%dx%d.", freq, oldNum);
                    oldNum = i;
                    freq = 1;
                } else {
                    freq++;
                }
            }
        }
        if (!first)    
            System.out.printf("%dx%d.%n", freq, oldNum);
        else 
            System.out.println("No integer found");
        sc.close();    
    }
    
    测试输出:"11 11 11", "112 223", "1 1 1\n7 7 1 1 0", "0 0 0" :
    test=[11 11 11]
    6x1.
    --------
    test=[112 223]
    2x1.3x2.1x3.
    --------
    test=[1 1 1
    7 7 1 1 0]
    3x1.2x7.2x1.1x0.
    --------
    test=[0 0 0]
    3x0.
    --------
    
    Online demo of both methods printRLE and printRLEDigits

    关于java - java中的运行长度编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65061949/

    相关文章:

    c - 打印正确数量的相同字符时出现问题。C 中的 RLE

    c - 用 C 语言将文件压缩为运行长度代码的程序

    Java JMenuItem 在第二个menuItem之后添加边框

    java - 使用反射调用方法时出现 NoSuchMethodException

    java - 如何使用spring初始化、加载属性和处置我自己的连接池?

    c++ - 为什么我的 RLE 代码显示 std out of range for c++?

    java - IntelliJ Idea JDK路径

    Java:成对组合数组中的元素

    matlab - 给定数据表和出现次数,我可以创建基础数据集吗?

    embedded - 对于这种情况,什么是好的(解)压缩例程