我正在研究以下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
DRINKEATCODEOutput:
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/