gpt4 book ai didi

java - 代码厨师 : Holes in the Text

转载 作者:太空宇宙 更新时间:2023-11-04 13:03:10 24 4
gpt4 key购买 nike

我正在研究以下CodeChef question :

Chef wrote some text on a piece of paper and now he wants to know how many holes are in the text. What is a hole? If you think of the paper as the plane and a letter as a curve on the plane, then each letter divides the plane into regions. For example letters "A", "D", "O", "P", "R" divide the plane into two regions so we say these letters each have one hole. Similarly, letter "B" has two holes and letters such as "C", "E", "F", "K" have no holes. We say that the number of holes in the text is equal to the total number of holes in the letters of the text. Help Chef to determine how many holes are in the text.

Input

The first line contains a single integer T <= 40, the number of test cases. T test cases follow. The only line of each test case contains a non-empty text composed only of uppercase letters of English alphabet. The length of the text is less then 100. There are no any spaces in the input.

Output

For each test case, output a single line containing the number of holes in the corresponding text.

Example

Input:
2
CODECHEF
DRINKEATCODE

Output:
2
5

我正在提交以下代码,但法官一直显示错误答案(WA)。有什么建议吗?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class test {
static short[] srr=new short[41];

public static short getHoles(String st){
short holes=0;
for (int i = 0; i < st.length(); i++) {
char c= st.charAt(i);
if(c=='A') holes++;

if(c=='D') holes++;

if(c=='P') holes++;

if(c=='O') holes++;

if(c=='R') holes++;

if(c=='Q') holes++;

if(c=='B'){
holes++;
holes++;
}
}

return holes;
}

public static void main(String[] args) throws IOException {
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
short cases;
String stt= s.readLine();
cases=Short.parseShort(stt);
String temp;
short t;
for (int i = 0; i < cases; i++) {
temp=s.readLine();
t = getHoles(temp);
srr[i]=t;
}

for (int i = 0; i< 45; i++) {
if(srr[i]==0) break;
System.out.println(srr[i]);
}

}
}

最佳答案

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Letter {

public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
InputStreamReader is= new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(is);
int n= Integer.parseInt(br.readLine());
for(int i=0;i<n;i++) {
int holes=0;
String s= br.readLine();
char[] ch= s.toCharArray();
for(int j=0;j<ch.length;j++) {

if(ch[j]=='A'||ch[j]=='D'||ch[j]=='O'||ch[j]=='Q'||ch[j]=='P'||ch[j]=='R') {
holes= holes+1;
}
else if(ch[j]=='B') {
holes=holes+2;
}
else {
holes=holes+0;
}
}
System.out.println(holes);
}
}
}

关于java - 代码厨师 : Holes in the Text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34745395/

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