今天去面试了,题目做不出来

laodabest 2006-01-03 02:09:30
面试失败,叫编一个程序,但在给定时间没编出来,那题目出来分享下,希望高手可以做出来
题目:
编写一程序输出一个由若干单词构成的字符串中的最长单词及其长度,若字符串中有多个单词满足条件须将这些单词全部输出。(注:程序书写要规范)
例:输入字符串为“The indictment said the defendants had collected geographical data indicating thousands of people would be killed in the chemical blast”
输出应为:
最长单词:geographical
单词长度:12
...全文
5243 81 打赏 收藏 转发到动态 举报
写回复
用AI写文章
81 条回复
切换为时间正序
请发表友善的回复…
发表回复
fofoo1982 2006-01-25
  • 打赏
  • 举报
回复
我觉得不需要在内存中存储字符串,只要记录最长的字符串在输入串(或文件)中的起始指针和长度就行.比较完和,再读输入串(或文件)输出.这样不要开辟太大的内存
thinkner 2006-01-23
  • 打赏
  • 举报
回复
d
starshx 2006-01-23
  • 打赏
  • 举报
回复
夸张,基础题。。。。。
os_rui 2006-01-22
  • 打赏
  • 举报
回复
import java.util.*;

public class GetWord{
public static void main(String args[]){
String scString="The indictment said the defendants asdfcdrdztga had collected geographical data indicating thousands of";

StringTokenizer token=new StringTokenizer( scString," ");
Vector result = new Vector();

int lengthTemp=0;
String strTemp="";
int lengthMax=0;

int nextIndex = 0;


while( token.hasMoreTokens() ){

strTemp = token.nextToken();
lengthTemp = strTemp.length();

if( lengthTemp>lengthMax ){//新串替换旧串
result.clear();
lengthMax = lengthTemp;
result.add( 0, strTemp );
continue;
}

if( lengthMax==lengthTemp ){//尾部追加新的字符串
nextIndex = result.size();
result.add( nextIndex, strTemp );
}

}//end while

for(int i=0; i<result.size(); i++ ){
System.out.println( result.elementAt( i ) );
}
}
}
liligugu 2006-01-21
  • 打赏
  • 举报
回复
先用split(" ");取出各个单词,然后在把每个单词执行trim( );在依次比较.length()
volareagi 2006-01-19
  • 打赏
  • 举报
回复
大家能不能对比下 那个编出来的既正确又简单 而且最重要的是比较规范化
martin_wang 2006-01-18
  • 打赏
  • 举报
回复
/**
* @author win2000
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class TestXML {


public static void main(String[] args) {
String word = "The indictment said the defendants asdfcdrdztga had collected geographical data indicating thousands of people would be killed in the chemical blast";
String[] list = word.split(" ");
int maxlength = -1;
ArrayList l = new ArrayList(list.length);

for (int i = 0; i < list.length - 1; i++) {

if (maxlength < list[i].length()) {
maxlength = list[i].length();

if (l.isEmpty()) {

} else {
l.clear();
}

l.add(new Integer(list[i].length()));
l.add(list[i]);
} else if (maxlength == list[i].length()) {
maxlength = list[i].length();
l.add(new Integer(list[i].length()));
l.add(list[i]);
}

}

for (int n = 0; n < l.size(); n++) {
System.out.println(l.get(n));
System.out.println(l.get(n + 1));
n++;

}
}
}
yufanmaster 2006-01-17
  • 打赏
  • 举报
回复
学习学习,尤其是Wasingmon(独自等待)给的代码挺不错的
Harbin_SAKURA 2006-01-16
  • 打赏
  • 举报
回复
public class Test
{
public static void main(String args[])
{
String string = "The indictment said the defendants had collected geographical data indicating thousands of people would be killed in the chemical blast";
String[] sql = string.split(" ");
int maxlength = 0;
String maxlengthString = "";
for(int i=0;i<sql.length;i++)
{
if(sql[i].length()>maxlength)
{
maxlengthString = sql[i];
maxlength = sql[i].length();
}
}
System.out.println("×î´ó³¤¶ÈµÄ×Ö·û´®ÊÇ:"+maxlengthString);
System.out.println("Æä³¤¶ÈÊÇ"+maxlength);
}
};
fengever 2006-01-16
  • 打赏
  • 举报
回复
import java.util.StringTokenizer;

public class spli {
public static void main(String args[]){
String str = "The indictment said the defendants had collected geographical data indicating thousands of people would be killed in the chemical blast ";
StringTokenizer st = new StringTokenizer(str," ");
int len = 0;
String tem = "";
String res = "";
while(st.hasMoreTokens()){
tem=st.nextToken();
if(tem.length()>len){
res = tem;
len = tem.length();
}else if(tem.length()==len)
res +=","+tem;
}
System.out.println(res);
System.out.print(len);
}

}
xboxjacky 2006-01-14
  • 打赏
  • 举报
回复
xuexi
tjblyy 2006-01-13
  • 打赏
  • 举报
回复
我参考了一下上面各位大家的代码,然后自己改了改之后,能够顺利运行并得出正确的结果,特贴在这里,给大家分享一下,借花献佛:
import java.util.Vector;
import java.util.Iterator;
class MaxLenWord
{
private static Vector result=new Vector();
private static String str="";
public static Vector getMaxLenWord(String str)
{
int maxlenWordIndex=0;
String[] strArr=str.split(" ");
for(int i=1;i<strArr.length;i++)
{
if(strArr[i].length()>strArr[maxlenWordIndex].length())
{
maxlenWordIndex=i;
result.clear();
result.add(strArr[i]);

}
else if(strArr[i].length()==strAr[maxlenWordIndex].length())
{
maxlenWordIndex=i;
result.add(strArr[i]);

}
}
for(int i=0;i<result.size();i++)
{
System.out.println("最长长度的单词是:"+result.get(i));
}
return result;
}

public static int getMaxLen(Vector v)
{
Iterator it=v.iterator();
int n=0;
if(it.hasNext())
{
n=it.next().toString().length();

}
System.out.println("最大长度是:"+n);
return n;
}

public static void main(String []args)
{

str="The indictment said the defendants had collected"
+" geographical data indicating thousands of people"
+" would be killed in the chemical blast";
getMaxLenWord(str);
getMaxLen(getMaxLenWord(str));

}
}
xijiang007008 2006-01-12
  • 打赏
  • 举报
回复
真晕阿,看了那么多,都没看到JAVA的精华。
我来写个主要片断:
String[] result = "The indictment said the defendants had collected geographical data indicating thousands of people would be killed in the chemical blast".split("\\s");
for (int x=0; x<result.length; x++)
System.out.println(result[x]);

这是核心,别的代码就自己++++了。
Wasingmon 2006-01-11
  • 打赏
  • 举报
回复

public class StringTest {

/**
* @param args
*
* 输出一个由若干单词构成的字符串中的最长单词及其长度,
*若字符串中有多个单词满足条件须将这些单词全部输出。
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="The indictment said the defendants had collected geographical data indicating thousands of people would be killed in the chemical blast";
String []strArray=str.split(" ");
int max=0;
//取得最大单词的长度
for(int i=0;i<strArray.length;i++){
if(max<strArray[i].length()){
max=strArray[i].length();
}
}
//输出满足最大长度的单词
for(int i=0;i<strArray.length;i++){
if(max==strArray[i].length()){
System.out.println(strArray[i]);
System.out.println(strArray[i].length());
}
}


}

}
zhuiganzhe88 2006-01-11
  • 打赏
  • 举报
回复
哪个对啊 我测试了几个都有错误 浮躁那个测试是0啊
cyqxyz 2006-01-11
  • 打赏
  • 举报
回复
/*
* Test.java
* Copyright XXX
* */
package csdn;

/*
*输出一个由若干单词构成的字符串中的最长单词及其长度,
*若字符串中有多个单词满足条件须将这些单词全部输出。
* @version 1.26, 04/07/16
* @author cyqxyz
* */
public class Test {
/*
*输出一个由若干单词构成的字符串中的最长单词及其长度,
*若字符串中有多个单词满足条件须将这些单词全部输出。
* @param args Arrays of String
* */
public static void main(String[] args) {
String testString =
new String("The indictment said the defendants had collected " +
"geographical data indicating thousands of people would be killed " +
"in the chemical blast");
String split = " ";
String[] arrays = testString.split(split);
int maxLength = arrays[0].length();
int position = 0;
for(int i=1 ; i < arrays.length ; i++){
//record the position of the max length word
if( maxLength < arrays[i].length() ){
position = i;
}
//record the length of the max length word
maxLength = maxLength > arrays[i].length() ? maxLength : arrays[i].length();
}
System.out.println(maxLength);
System.out.println(arrays[position]);
}
}

嘿嘿,我写的!!!
guojian_ren 2006-01-10
  • 打赏
  • 举报
回复
I expect you'll pass the interview!
benq998 2006-01-09
  • 打赏
  • 举报
回复
这道题多做不出来,那还真应该在学学,这样面试1000%会失败。
kerosun 2006-01-09
  • 打赏
  • 举报
回复
mark
sTrawman2005 2006-01-08
  • 打赏
  • 举报
回复
我觉得这时大家应该鼓励搂主,把搂主题做好即可,动不动就简单啥的,说这有什么用应该好好想想如何改进这样会对大家更好
加载更多回复(61)

62,625

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧