用一系列数字填充整数数组(Java)。

huangapple go评论76阅读模式
英文:

Fill an integer array with a series of number that is a string (java)

问题

# 在下面写你的代码前面的英文说明部分

number = "110034043312132121220023020423340432"  # 但可能是任何转换为字符串的大数

print("以字符串形式输入长度: ")
input_str = input()  # abcdefg
length = len(input_str)  # 这意味着我会得到 6。

# 创建一个二维字符数组
rows = length
columns = len(number)
char_array = [[" " for _ in range(columns)] for _ in range(rows)]

# 填充字符数组
for i in range(rows):
    for j in range(columns):
        char_array[i][j] = number[j]

# 打印字符数组
for i in range(rows):
    for j in range(columns):
        print(char_array[i][j], end=" ")
    print()

请注意,这只是一个伪代码示例,因为您的原始代码是Java,而您要求使用Python翻译。实际上,数组的索引可能会略有不同。另外,您还需要导入Python的输入函数(input)和适当的打印语句。这只是一个简单示例,您可能需要根据实际情况进行适当的调整。

英文:

Im trying to write the following program, where i have a string that a number is generated possibly like 37 digits.
Is it possible to fill an array that i know the length but not the rows because the string can be anything.

String number;  //110034043312132121220023020423340432   but can be any big number that is converted to a string 

System.out.println("Give length in a form of a string: ");  
String input= scan.nextLine();    //abcdefg
int length = input.length();      //this means i get 6 back.

And save this in a character array where i know the length from input.lenght() and i dont know the rows

A B C D E F G
1 1 0 0 3 4 0
4 3 3 1 2 1 3
2 1 2 1 2 2 0
0 2 3 0 2 0 4
2 3 3 4 0 4 3
2 

I know the answer might be something stupid simple but please be coder noob friendly. Thanks

答案1

得分: 0

为了做到这一点,你可以考虑使用一个列表。

ArrayList<int[]> list = new ArrayList<int[]>();

在每一行中,你可以向列表中添加一个新的数组。
如果你希望最终的结果是一个二维数组,你可以将列表很容易地转换为一个数组。

英文:

To do this, you might be able to use a list instead.

ArrayList&lt;int[]&gt; list = new ArrayList&lt;int[]&gt;();

and in each line, you can add a new array into the list.
If you want the final result to be a 2d array, you can easily convert a list to an array.

答案2

得分: 0

public static char[][] createMatrix(String num, String title) {
    int totalRows = (int)Math.ceil((double)num.length() / title.length());
    char[][] matrix = new char[totalRows + 1][title.length()];
    
    for (int i = 0; i < matrix[0].length; i++)
        matrix[0][i] = title.charAt(i);
    
    for (int i = 0, row = 1, col = 0; i < num.length(); i++) {
        matrix[row][col++] = num.charAt(i);
    
        if (col == matrix[row].length) {
            row++;
            col = 0;
        }
    }
    
    return matrix;
}
英文:
public static char[][] createMatrix(String num, String title) {
    int totalRows = (int)Math.ceil((double)num.length() / title.length());
    char[][] matrix = new char[totalRows + 1][title.length()];

    for (int i = 0; i &lt; matrix[0].length; i++)
        matrix[0][i] = title.charAt(i);

    for (int i = 0, row = 1, col = 0; i &lt; num.length(); i++) {
        matrix[row][col++] = num.charAt(i);

        if (col == matrix[row].length) {
            row++;
            col = 0;
        }
    }

    return matrix;
}

答案3

得分: 0

解决方案可能如下
1. 使用 `input.length` 作为结果二维数组中列的最大数量
2. 计算结果二维数组中的行数 `nums / cols`,如果有余数则添加一行额外的行
3. 结果中的最后一行可能是不规则的包含少于 `cols` 个元素

String number = "110034043312132121220023020423340432"; // 但可以是任何转换为字符串的大数字

System.out.println("以字符串形式给出长度:");
String input = "ABCDEFG"; //scan.nextLine();    //abcdefg
int cols = input.length(); // 结果数组中的列数
int nums = number.length(); // 数字的总数

int rows = nums / cols + (nums % cols == 0 ? 0 : 1);
int[][] result = new int[rows][]; // 结果可能是不规则的

for (int i = 0; i < rows - 1; i++) {
    result[i] = new int[cols];
}
result[rows - 1] = new int[nums % cols == 0 ? cols : nums % cols];

for (int i = 0; i < nums; i++) {
    int r = i / cols;
    int c = i % cols;
    result[r][c] = number.charAt(i) - '0';
}

// 打印结果
System.out.println(input.replaceAll(".", "$0 ")); // 在列之间打印带有空格的标题
for (int[] row : result) {
    System.out.println(Arrays.toString(row)
                             .replaceAll("[^\\s\\d]", "") // 从输出中删除括号和逗号
    );
}

输出:

A B C D E F G 
1 1 0 0 3 4 0
4 3 3 1 2 1 3
2 1 2 1 2 2 0
0 2 3 0 2 0 4
2 3 3 4 0 4 3
2
英文:

The solution may be as follows:

  1. Use input.length as the max number of columns in the resulting 2D array
  2. Calculate the number of rows in the resulting 2D array nums / cols, adding one extra row if there's a remainder.
  3. The last row in the result may be jagged: contain less than cols elements.
String number = &quot;110034043312132121220023020423340432&quot;; //   but can be any big number that is converted to a string 

System.out.println(&quot;Give length in a form of a string: &quot;);  
String input= &quot;ABCDEFG&quot; ; //scan.nextLine();    //abcdefg
int cols = input.length(); // columns in the resulting array
int nums = number.length(); // total count of numbers

int rows = nums / cols + (nums % cols == 0 ? 0 : 1);
int[][] result = new int[rows][]; // the result may be jagged

for (int i = 0; i &lt; rows - 1; i++) {
    result[i] = new int[cols];
}
result[rows - 1] = new int[nums % cols == 0 ? cols : nums % cols];

for (int i = 0; i &lt; nums; i++) {
    int r = i / cols;
    int c = i % cols;
    result[r][c] = number.charAt(i) - &#39;0&#39;;
}

// print the result
System.out.println(input.replaceAll(&quot;.&quot;, &quot;$0 &quot;)); // print header with spaces between columns
for (int[] row : result) {
    System.out.println(Arrays.toString(row)
                             .replaceAll(&quot;[^\\s\\d]&quot;, &quot;&quot;) // remove brackets and comma from output
    );
}

Output:

A B C D E F G 
1 1 0 0 3 4 0
4 3 3 1 2 1 3
2 1 2 1 2 2 0
0 2 3 0 2 0 4
2 3 3 4 0 4 3
2

huangapple
  • 本文由 发表于 2020年10月24日 06:43:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64508149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定