当前位置: 代码迷 >> J2SE >> 一道JAVA笔试题解决办法
  详细解决方案

一道JAVA笔试题解决办法

热度:30   发布时间:2016-04-23 20:45:39.0
一道JAVA笔试题
当初去面试,做了一套题,其中一道题如下:

有一个对象 StringBuilder sentence = new StringBuilder("This is an apple"); 
要求只使用 char c,int p1,int p2这三个变量,且不再开辟更多内存的情况下,输出 apple an is This

俺不会做,所以没面上,有没有人帮做出来啊??
------解决方案--------------------
胡乱写了一个,要求应该可以达到,不过前提是
1、原句中哪里有空格,输出就哪里有空格,并且原句几个输出就几个
2、每个单词长度短(短指在char范围内,不会有长度几万的单词)


StringBuilder sentence = new StringBuilder("This is an apple");
char c;
int p1, p2;

p1 = sentence.length();

while (p1 > 0) {
p1--;
p2 = p1;

while (p1 >= 0 && sentence.charAt(p1) != ' ') {
p1--;
}

c = (char) (p2 - p1);
p2 = p1 + 1;

while (p1 + c >= p2) {
System.out.print(sentence.charAt(p2));
p2++;
}

if (p1 >= 0) {
System.out.print(' ');
}
}


------解决方案--------------------
public class Test {
public static void main(String[] args) {
StringBuilder sentence = new StringBuilder("This is an apple");

int p1 = sentence.length() - 1;
int p2 = 1;
char c;

while (p1 >= 0) {
c = sentence.charAt(p1);
while (c != ' ' && p1 > 0) {
c = sentence.charAt(--p1);
}
System.out.print(c);
c = sentence.charAt(++p1);

while (c != ' ' && p1 < sentence.length()) {
c = sentence.charAt(p1++);
System.out.print(c);
++p2;
}

p1 = p1 - p2 - 1;
p2 = 1;
}
}
}

------解决方案--------------------
用递归谢了一个

public class Test001 {
static char c = ' ';
static StringBuilder sentence = new StringBuilder("This is an apple");
static int p1 = sentence.length();
static int p2 = 0;

public static void main(String[] args) {
do{
p1 = getIndex(sentence ,p1,p2,c);
} while( p1 != -10 );

}

public static int getIndex(StringBuilder sentence ,int p1,int p2,char c){
if( sentence.substring(0, p1).lastIndexOf(c) > 0 ){
p2 = sentence.substring(0, p1).lastIndexOf(c);
System.out.print(sentence.substring(0, p1).substring(p2+1)+c);
}else{
System.out.print(sentence.substring(0, p1));
p2 = -10;
}
return p2;
}
}

------解决方案--------------------
写个不对的。
 char c;int p1;int p2;
       System.out.println(sentence.toString().split("\\s+")[3]+" "+sentence.toString().split("\\s")[2]+" "
        +  sentence.toString().split("\\s+")[1]+" "+sentence.toString().split("\\s")[0]);

------解决方案--------------------
char c=' ';
int p1=0,p2;
StringBuilder sentence = new StringBuilder("This is an apple");
p2=sentence.toString().length();
while((p1=sentence.toString().lastIndexOf(" ",p2-1))!=-1){
System.out.print(sentence.toString().substring(p1+1,p2)+c);
p2=sentence.toString().lastIndexOf(" ",p1);
if(sentence.toString().lastIndexOf(" ",p2-1)==-1){
System.out.print(sentence.toString().substring(0,p2));
}
}

------解决方案--------------------
我来贴一个没有技术含量的

p2 = sentence.length();
while(true){
p1 = 0;
for(int j=0;j<p2;j++){
c = sentence.charAt(j);
if(c ==' '){
p1 = j;
}
}
if(p1!=0){
System.out.print(sentence.substring(p1+1, p2)+" ");
}else{
System.out.print(sentence.substring(0, p2));
break;
}
p2 = p1;
}

------解决方案--------------------
先反向搜索空格,找到后再正向输出,因为只允许使用那三个变量嘛所以就用了点投机取巧的方式c = (char) p1;哈哈,反正效果是达到了。

StringBuilder sentence = new StringBuilder("This is an apple");

char c;
int p1, p2;

p1 = p2 = sentence.length() - 1;

while (p1 >= 0) {
c = sentence.charAt(p1);
if (c == ' ' 
------解决方案--------------------
 p1 == 0) {

if (p1 == 0) {
  相关解决方案