gpt4 book ai didi

java,在大字符串输入的某个字符处计数的代码

转载 作者:行者123 更新时间:2023-12-04 15:11:43 26 4
gpt4 key购买 nike

我的问题是如果我有一个字符串 - 在字符串“#”和“.”中有两种类型的字符如果每次我向右移动 3 并向下移动,我想计算我登陆“#”的次数。例如,给定的字符串如下所示:

...#.#.......##....#.......#...
.#....#.##.#..#.......#.....##.
#..#...##.####..###.....#......
..#...##...#...#.#......#...#.#
.##.##.#...#.....#.##..##......
.#...#.#.##.###..#...#...#.....
.#..##..#....##.##....##....##.
..#...##....#..###........##...
.#..#..#.#....#.#...#.#......#.
.##.....#...#..#..#..#...###...
.#...#....#..#...........###...
.....#...........##.#......#...
.....#....##......##..#.#......
-1//stops the checking

接下来会发生什么,0 表示降落在 . X 表示它落在 # 上,这是每次发生这种情况时计数应该增加 1 的时间:

...#.#.......##....#.......#...
.#.o..#.##.#..#.......#.....##.
#..#..0##.####..###.....#......
..#...##.0.#...#.#......#...#.#
.##.##.#...#0....#.##..##......
.#...#.#.##.###0.#...#...#.....
.#..##..#....##.##0...##....##.
..#...##....#..###...o....##...
.#..#..#.#....#.#...#.#.0....#.
.##.....#...#..#..#..#...##X...
.#...#....#..#...........###..0//notice here next round it will exceed
.....#...........##.#......#.....0..#...........##.#......#...//line get doubled as if returning to beginning of the
.....#....##......##..#.#...........X....##......##..#.#......//same here
-1//to stop

所以在开始时,我移动到第三个索引并向下移动到下一行,我检查该字符是否为“#”,如果是,则计数++所以在这个例子中 - 它移动了三个,所以第一行移动到......然后向下移动到下一行,这将是索引 3,在这种情况下是.#。所以第三个是。等等。如果它落在 #count++ 上。这是我的代码,但它似乎不起作用-

public static Scanner reader = new Scanner(System.in);

public static void main(String[] args) {
String a = "";//starts empty
int cnt = 0, b = 0;//cnt++ if '#', b helps us move 3 to the right each time
a = reader.nextLine(); //gets the string
if (b + 3 > a.length() - 1) {//checks that the length when moving 3 doesn't exceed string length
b = 0;// if it does exceed then go all the way to the left of the string
}
b += 3;//move right 3
a = reader.nextLine(); //get next line from the input
while (a != "-1") {//until there is -1 (to stop getting input) it continues to check
char c = a.charAt(b -
1);//takes whats at place b(-1 is because first index is 0 and not1) and note this is three to the right and after it moves down a row!// is line 23
if (c == '#' && a != "") {//checks if string isn't empty and if the char equals #
cnt++;//if yes count++
}
if (b + 3 > a.length() - 1) {//explained above
b = 0;
}
b += 3;
a = reader.nextLine();
}


System.out.println(cnt);
}

我得到的错误如下:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 26
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
at java.base/java.lang.String.charAt(String.java:702)
at q.qq.main(Advent3.java:23)

帮助将不胜感激。让我知道是否有不清楚的地方或者如何改进:) 谢谢在聊天中讨论并尝试改进后的代码:

public class Advent3 {
public static Scanner reader= new Scanner(System.in);
public static void main(String[] args){
int b=4,cnt=0,line=1,s;
String str="";
char charsign;
str=reader.nextLine();
s=str.length();
str=reader.nextLine();

while(str!=""||str!="-1") {
line++;
s=str.length();
if(b%str.length()==0) {
charsign= str.charAt(10);
}
else {
charsign= str.charAt(b%str.length()-1);
}
if(charsign=='#') {
cnt++;
System.out.println(cnt);
}
b+=3;
str=reader.nextLine();
}
System.out.println(str);

System.out.println(cnt);
}
}

代码本身可以工作,我想我已经很接近了,但是它没有打印出正确的答案!我被卡住了,非常感谢您的帮助!

最佳答案

public static int count(Scanner scan) {
int res = 0;
int col = -1;
String str;
boolean check = false;

while (!"-1".equals(str = scan.nextLine())) {
if (col >= str.length())
col %= str.length() - 1;
if (check && str.charAt(col) == '#')
res++;

col += 3;
check = true;
}

return res;
}

演示:

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(count(scan)); // 5
}

输入:

...#.#.......##....#.......#...
.#....#.##.#..#.......#.....##.
#..#...##.####..###.....#......
..#...##...#...#.#......#...#.#
.##.##.#...#.....#.##..##......
.#...#.#.##.###..#...#...#.....
.#..##..#....##.##....##....##.
..#...##....#..###........##...
.#..#..#.#....#.#...#.#......#.
.##.....#...#..#..#..#...###...
.#...#....#..#...........###...
.....#...........##.#......#...
.....#....##......##..#.#......
-1

输出:

...#.#.......##....#.......#...
.#0...#.##.#..#.......#.....##.
#..#.0.##.####..###.....#......
..#...##0..#...#.#......#...#.#
.##.##.#...1.....#.##..##......
.#...#.#.##.##2..#...#...#.....
.#..##..#....##.#3....##....##.
..#...##....#..###..3.....##...
.#..#..#.#....#.#...#.#3.....#.
.##.....#...#..#..#..#...#4#...
.#...#....#..#...........###.4.
..4..#...........##.#......#...
.....5....##......##..#.#......
-1

5

关于java,在大字符串输入的某个字符处计数的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65121659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com