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

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
...全文
5218 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)
作者:July、阿财。 时间:二零一一年十月十三日。 ------------------------------ 无私分享造就开源的辉煌。 今是二零一一年十月十三日,明日14日即是本人刚好开博一周年。在一周年之际,特此分享出微软面试 全部100题答案的完整版,以作为对本博客所有读者的回馈。 一年之前的10月14日,一个名叫July 的人在一个叫csdn 的论坛上开帖分享微软等公司数据结构+算法 面试100题,自此,与上千网友一起,一起思考,一起解答这些面试题目,最终成就了一个名为:结构之法 算法之道的编程面试与算法研究并重的博客,如今,此博客影响力逐步渗透到海外,及至到整个互联网。 在此之前,由于本人笨拙,这微软面试100题的答案只整理到了前60题(第1-60题答案可到本人资源下 载处下载:http://v_july_v.download.csdn.net/),故此,常有朋友留言或来信询问后面40题的答案。只是 因个人认为:一、答案只是作为一个参考,不可太过依赖;二、常常因一些事情耽搁(如在整理最新的今年 九月、十月份的面试题:九月腾讯,创新工场,淘宝等公司最新面试十三题、十月百度,阿里巴巴,迅雷搜狗 最新面试十一题);三、个人正在针对那100题一题一题的写文章,多种思路,不断优化,即成程序员编程 艺术系列。自此,后面40题的答案迟迟未得整理。且个人已经整理的前60题的答案,在我看来,是有诸多问 题与弊端的,甚至很多答案都是错误的。 互联网总是能给人带来惊喜。前几日,一位现居美国加州的名叫阿财的朋友发来一封邮件,并把他自己 的全部100题的答案一并发予给我,自此,便似遇见了知己。十分感谢。 任何东西只有分享出来才更显其价值。本只需贴出后面40题的答案,因为前60题的答案本人早已整理上 传至网上,但多一种思路多一种参考亦未尝不可。特此,把阿财的答案再稍加整理番,然后把全部100题的答 案现今都贴出来。若有任何问题,欢迎不吝指正。谢谢。 上千上万的人都关注过此100题,且大都都各自贡献了自己的思路,或回复于微软100题维护地址上,或 回复于本博客内,人数众多,无法一一标明,特此向他们诸位表示敬意和感谢。谢谢大家,诸君的努力足以影 响整个互联网,咱们已经迎来一个分享互利的新时代。 感谢诸君,请享用.....

62,614

社区成员

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

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