英文:
When the code is run it doesn't output anything
问题
这是codevita-2020问题中的一部分,名为“星座”。
我尝试使用Java解决这个问题。我已经构建了逻辑,但在尝试输入字符数组时遇到了困难。
问题陈述:
三个字符 { #, *, . } 代表了太空中的星星和星系的星座。每个星系由 # 字符界定。在给定星系中可以有一个或多个星星。星星只能呈元音字母的形状 { A, E, I, O, U }。元音字母的星星形状是一个3x3的块。星星不能重叠。点(.)字符表示空白。
给定一个由 { #, *, . } 字符组成的3xN矩阵,找出其中的星系和星星。
注意:请注意在下面的示例部分中如何表示元音字母A的3x3块。
示例1:
输入
18
-
. * # * * * # * * * # * * * . * .
-
. * # * . * # . * . # * * * * * *
-
-
-
* * * # * * * # * * * * . *
-
-
输出
U#O#I#EA
我的代码:
// 代码部分不翻译
它在运行时没有显示任何输出。
英文:
This was the part of a codevita-2020 problem called "constellations".
I tried solving the question using Java. I have built the logic but faced difficulty while trying to take input in a char array.
(There is a space between each line)
Problem statement :
Three characters { #, *, . } represents a constellation of stars and galaxies in space. Each galaxy is demarcated by # characters. There can be one or many stars in a given galaxy. Stars can only be in shape of vowels { A, E, I, O, U } . A collection of * in the shape of the vowels is a star. A star is contained in a 3x3 block. Stars cannot be overlapping. The dot(.) character denotes empty space.
Given 3xN matrix comprising of { #, *, . } character, find the galaxy and stars within them.
Note: Please pay attention to how vowel A is denoted in a 3x3 block in the examples section below.
Example 1
Input
18
* . * # * * * # * * * # * * * . * .
* . * # * . * # . * . # * * * * * *
* * * # * * * # * * * # * * * * . *
Output
U#O#I#EA
MY CODE:
package codevita;
//constellations - codevita
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.*;
public class constellations {
static public BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
static public PrintWriter out = new PrintWriter(System.out);
static List<String> list = new ArrayList<>();
static int main = 0;
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
StringBuilder result = new StringBuilder();
int n = Integer.parseInt(reader.readLine());
System.out.println();
String line[] = new String[3];
int x = line.length;
for(int j = 0 ; j < 3; j++) {
line[j] = sc.nextLine();
System.out.println();
}
char []line1 = line[0].toCharArray();
char []line2 = line[1].toCharArray();
char []line3 = line[2].toCharArray();
int count = 0,main = 0;
for(int k = 0; k < line1.length; k++){
if(line1[k] =='#'){
count++;
}
}
int split[] = new int [n];
for(int l = 0; l < line1.length; l++){
if(line1[l] =='#'){
split[l] = l;
}
}
// for A
for(int i = 2; i< n; i++){
// for A
if(line1[i] =='#') {
i++;
}
if(line1[i]== '.' && line1[i-1]== '*' &&line1[i-2]== '.' &&line2[i]== '*' && line2[i-1]== '*' &&line2[i-2]== '*' && line3[i]== '*' && line3[i-1]== '.' &&line3[i-2]== '*'){
result.append('A');
if(split[i] != 0){
result.append('#');
}
System.out.print(result.toString());
main++;
}
// for E
else if(line1[i]== '*' && line1[i-1]== '*' &&line1[i-2]== '*' &&line2[i]== '*' && line2[i-1]== '*' &&line2[i-2]== '*' && line3[i]== '*' && line3[i-1]== '*' &&line3[i-2]== '*'){
result.append('E');
if(split[i] != 0){
result.append('#');
}
main++;
}
// for I
else if(line1[i]== '*' && line1[i-1]== '*' &&line1[i-2]== '*' &&line2[i]== '.' && line2[i-1]== '*' &&line2[i-2]== '.' && line3[i]== '*' && line3[i-1]== '*' &&line3[i-2]== '*'){
result.append('I');
if(split[i] != 0){
result.append('#');
}
main++;
}
// for O
else if(line1[i]== '*' && line1[i-1]== '*' &&line1[i-2]== '*' &&line2[i]== '*' && line2[i-1]== '.' &&line2[i-2]== '*' && line3[i]== '*' && line3[i-1]== '*' &&line3[i-2]== '*'){
result.append('O');
if(split[i] != 0){
result.append('#');
}
main++;
}
// for U
else if(line1[i]== '*' && line1[i-1]== '.' &&line1[i-2]== '*' &&line2[i]== '*' && line2[i-1]== '.' &&line2[i-2]== '*' && line3[i]== '*' && line3[i-1]== '*' &&line3[i-2]== '*'){
result.append('U');
if(split[i] != 0){
result.append('#');
}
main++;
}
}
if(main >= (n-count)/x) {
out.println(result.toString());
}
sc.close();
out.close();
}
static int[] readArray(int n) throws IOException {
Scanner sc = new Scanner(System.in);
int[] a = new int[n];
String[] data = reader.readLine().split(" ");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(data[i]);
}
sc.close();
return a;
}
}
> It shows no output when I run this.
</details>
# 答案1
**得分**: 1
不可靠地同时通过`BufferedReader reader`和`Scanner sc`读取`System.in`。您必须决定只采用一种方式来读取输入。可能最简单的修复方法是将
Scanner sc = new Scanner(System.in);
更改为
Scanner sc = new Scanner(reader);
---
此外,您没有考虑字符之间的空格。您可以通过在早期阶段删除空格来修复此问题;将
line[j] = sc.nextLine();
更改为
line[j] = sc.nextLine().replace(" ", "");
---
另外,将`#`插入输出的逻辑是错误的。要修复这个问题,只需将
if(line1[i] =='#') {
i++;
}
更改为
if (line1[i] == '#') result.append('#');
请注意,这仍然不会在开头打印`#`,因为您只从`int i = 2;`开始循环。
<details>
<summary>英文:</summary>
It doesn't reliably work to read `System.in` both via `BufferedReader reader` and via `Scanner sc`. You have to decide on only one way to read the input. The probably simplest fix is to change
Scanner sc = new Scanner(System.in);
to
Scanner sc = new Scanner(reader);
---
Then, you didn't take the space between the characters into account. You can fix this by removing the spaces at an early stage; change
line[j] = sc.nextLine();
to
line[j] = sc.nextLine().replace(" ", "");
---
Also, the logic to insert `#` in the output is wrong. To fix, just change
if(line1[i] =='#') {
i++;
}
to
if (line1[i] == '#') result.append('#');
Note that this still won't print `#` at the beginning, because you start the loop only at `int i = 2;`.
</details>
# 答案2
**得分**: 0
Here is the translated code:
```java
int n = 18, x1, y1;
Scanner sc = new Scanner(System.in);
char x[][] = new char[3][n];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < n; j++) {
x[i][j] = sc.next().charAt(0);
}
}
for (int i = 0; i < n; i++) {
if (x[0][i] == '#' && x[1][i] == '#' && x[2][i] == '#') {
System.out.print("#");
} else if (x[0][i] == '.' && x[1][i] == '.' && x[2][i] == '.') {
} else {
char a, b, c, a1, b1, c1, a2, b2, c2;
x1 = i;
a = x[0][x1];
b = x[0][x1 + 1];
c = x[0][x1 + 2];
a1 = x[1][x1];
b1 = x[1][x1 + 1];
c1 = x[1][x1 + 2];
a2 = x[2][x1];
b2 = x[2][x1 + 1];
c2 = x[2][x1 + 2];
if (a == '.' && b == '*' && c == '.' && a1 == '*' && b1 == '*' && c1 == '*' && a2 == '*' && b2 == '.' && c2 == '*') {
System.out.print("A");
i = i + 2;
}
if (a == '*' && b == '*' && c == '*' && a1 == '*' && b1 == '*' && c1 == '*' && a2 == '*' && b2 == '*' && c2 == '*') {
System.out.print("E");
i = i + 2;
}
if (a == '*' && b == '*' && c == '*' && a1 == '.' && b1 == '*' && c1 == '.' && a2 == '*' && b2 == '*' && c2 == '*') {
System.out.print("I");
i = i + 2;
}
if (a == '*' && b == '*' && c == '*' && a1 == '*' && b1 == '.' && c1 == '*' && a2 == '*' && b2 == '*' && c2 == '*') {
System.out.print("O");
i = i + 2;
}
if (a == '*' && b == '.' && c == '*' && a1 == '*' && b1 == '.' && c1 == '*' && a2 == '*' && b2 == '*' && c2 == '*') {
System.out.print("U");
i = i + 2;
}
}
}
I have translated the code as requested.
英文:
int n=18,x1,y1;
Scanner sc=new Scanner(System.in);
char x[][]=new char[3][n];
for(int i=0;i<3;i++)
{
for(int j=0;j<n;j++)
{
x[i][j]=sc.next().charAt(0);
}
}
for(int i=0;i<n;i++)
{
if(x[0][i]=='#' && x[1][i]=='#' && x[2][i]=='#')
{
System.out.print("#");
}
else if(x[0][i]=='.' && x[1][i]=='.' && x[2][i]=='.')
{}
else
{
char a,b,c,a1,b1,c1,a2,b2,c2;
x1 = i;
a = x[0][x1];
b = x[0][x1+1];
c = x[0][x1+2];
a1 = x[1][x1];
b1 = x[1][x1+1];
c1 = x[1][x1+2];
a2 = x[2][x1];
b2 = x[2][x1+1];
c2 = x[2][x1+2];
if(a=='.' && b=='*' && c=='.' && a1=='*' && b1=='*' && c1=='*' && a2=='*' && b2=='.' && c2=='*')
{
System.out.print("A");
i = i + 2;
}
if(a=='*' && b=='*' && c=='*' && a1=='*' && b1=='*' && c1=='*' && a2=='*' && b2=='*' && c2=='*')
{
System.out.print("E");
i = i + 2;
}
if(a=='*' && b=='*' && c=='*' && a1=='.' && b1=='*' && c1=='.' && a2=='*' && b2=='*' && c2=='*')
{
System.out.print("I");
i = i + 2;
}
if(a=='*' && b=='*' && c=='*' && a1=='*' && b1=='.' && c1=='*' && a2=='*' && b2=='*' && c2=='*')
{
System.out.print("O");
i = i + 2;
}
if(a=='*' && b=='.' && c=='*' && a1=='*' && b1=='.' && c1=='*' && a2=='*' && b2=='*' && c2=='*')
{
System.out.print("U");
i = i + 2;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论