工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 2127|回复: 16

转贴道面试题...大家用不同语言做做看...

[复制链接]
发表于 2006-1-13 14:52 | 显示全部楼层 |阅读模式
面试失败,叫编一个程序,但在给定时间没编出来,那题目出来分享下,希望高手可以做出来
题目:
编写一程序输出一个由若干单词构成的字符串中的最长单词及其长度,若字符串中有多个单词满足条件须将这些单词全部输出。(注:程序书写要规范)
例:输入字符串为“The indictment said the defendants had collected geographical data indicating thousands of people would be killed in the chemical blast”
输出应为:
最长单词:geographical
单词长度:12

在CSDN的java版转来的...大家看看...
发表于 2006-1-13 16:30 | 显示全部楼层

  1. package test;

  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;

  5. public class Test1 {

  6.         private int length;
  7.         private String[] s;
  8.         private List list;
  9.        
  10.         public void find(String str){
  11.                 length = 0;
  12.                 s = str.split(" ");
  13.                 list = new ArrayList();
  14.                 for ( int i=0; i < s.length; i++ ){
  15.                         if( s[i].length() > length ){
  16.                                 length = s[i].length();
  17.                                 list.clear();
  18.                                 list.add(s[i]);
  19.                         }
  20.                         else if( s[i].length() == length ){
  21.                                 length = s[i].length();
  22.                                 list.add(s[i]);
  23.                         }
  24.                 }
  25.         }
  26.        
  27.         public void print(){
  28.                 Iterator itor = list.iterator();
  29.                 while( itor.hasNext() ){
  30.                         String s = (String)itor.next();
  31.                         System.out.println("最长单词:"+s);
  32.                         System.out.println("单词长度:"+length);
  33.                 }
  34.                
  35.         }
  36.         /**
  37.          * @param args
  38.          */
  39.         public static void main(String[] args) {
  40.                 // TODO 自动生成方法存根
  41.                 Test1 app = new Test1();
  42.                 app.find("a bb cses s fess");
  43.                 app.print();
  44.         }

  45. }
复制代码

java写的一个.

[ 本帖最后由 hjack 于 2006-1-13 16:32 编辑 ]
回复

使用道具 举报

 楼主| 发表于 2006-1-13 18:41 | 显示全部楼层
  1. package test;

  2. public class CountString {

  3.         private static String find(String s) {
  4.                 String newString[] = s.split(" ");
  5.                 int maxLength = 0;
  6.                 String maxLengthString = null;
  7.                 for (int i = 0; i < newString.length; i++) {
  8.                         if (newString[i].length() > maxLength) {
  9.                                 maxLength = newString[i].length();
  10.                                 maxLengthString = newString[i];
  11.                         }
  12.                 }

  13.                 return maxLengthString + "," + maxLength;
  14.         }

  15.         private static void print(String s) {
  16.                 String newString[] = s.split(",");
  17.                 System.out.println("最长单词:" + newString[0]);
  18.                 System.out.println("单词长度:" + newString[1]);
  19.         }

  20.         public static void main(String[] args) {
  21.                 String s = "The indictment said the defendants had collected geographical data indicating thousands of people would be killed in the chemical blast";
  22.                 print(find(s));
  23.                 return;
  24.         }
  25. }
复制代码



...被先贴出来了...我这个没用collection接口...
回复

使用道具 举报

发表于 2006-1-13 23:50 | 显示全部楼层
这是用JAVASCRIPT来写的

  1. <html><head>
  2. <SCRIPT LANGUAGE="JavaScript">
  3. <!--
  4. function f()
  5. {
  6.         var str=document.all.text1.value;
  7.         document.all.text2.value="";
  8.         var s=str.split(" ");
  9.         var len=s.length;
  10.         var t=s[0];
  11.         for(i=0;i<len;i++)if(t.length<s[i].length)t=s[i];
  12.         var L=t.length;
  13.         document.all.text2.value+="最长单词是: ";
  14.         //题目没说相同单词不要重复输出,所以省事一些
  15.         for(i=0;i<len;i++){
  16.                 if(s[i].length==L)
  17.                 document.all.text2.value+=s[i]+" ";
  18.                 }
  19.         document.all.text2.value+=" 单词长度为:"+L;
  20. }
  21. //-->
  22. </SCRIPT>
  23. </head>
  24. <body><center>
  25. <textarea cols="100" rows="10" id="text1">The indictment said the defendants had collected geographical data indicating thousands of people would be killed in the chemical blast</textarea>
  26. <textarea cols="100" rows="10" id="text2"></textarea><br>
  27. <input type="button" value="OK" onclick="f()">
  28. </center>
  29. </body>
  30. </html>
复制代码
回复

使用道具 举报

发表于 2006-1-16 16:25 | 显示全部楼层

  1. //Flash ActionScript 1.0
  2. var temp=0;
  3. var str = "The indictment said the defendants had collected geographical data indicating thousands of people would be killed in the chemical blast";
  4. var breakStr = new Array();  //to store the word
  5. var longWord = new Array();  //to store the longest word
  6. breakStr=str.split(" "); //break the string
  7. for(var i=0;i<breakStr.length;i++){
  8.         if(breakStr[i].length>temp){
  9.                 longWord=new Array();
  10.                 longWord.push(breakStr[i]);
  11.                 temp=breakStr[i].length;
  12.         }else if(breakStr[i].length==temp){
  13.                 longWord.push(breakStr[i]);
  14.         }
  15. }
  16. trace(temp);
  17. trace(longWord);
复制代码


  1. //Flash ActionScript 2.0
  2. var temp:Number = 0;
  3. var str:String = "The indictment said the defendants had collected geographical data indicating thousands of people would be killed in the chemical blast";
  4. var breakStr:Array = new Array(); //to store the word
  5. var longWord:Array = new Array(); //to store the longest word
  6. breakStr = str.split(" "); //break the string
  7. for (var i:Number = 0; i<breakStr.length; i++) {
  8.         if (breakStr[i].length>temp) {
  9.                 longWord = new Array();
  10.                 longWord.push(breakStr[i]);
  11.                 temp = breakStr[i].length;
  12.         } else if (breakStr[i].length == temp) {
  13.                 longWord.push(breakStr[i]);
  14.         }
  15. }
  16. trace(temp);
  17. trace(longWord);
复制代码



OUTPUT:
12
geographical


[ 本帖最后由 黯然销魂 于 2006-1-16 17:34 编辑 ]
回复

使用道具 举报

发表于 2006-1-16 22:50 | 显示全部楼层
记得以前考二级C语言的上机题目中背过类似题目,用指针。。。。
回复

使用道具 举报

发表于 2006-1-16 23:20 | 显示全部楼层
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

class CMaxSubStrs
{
public:

        CMaxSubStrs(const string& rStr);
        void PrintMaxSubStrs();

private:

        void GetMaxSubStrs(const string& rStr);

private:
        vector<string> mVecMaxStrs;
        int mMaxLen;

};

CMaxSubStrs::CMaxSubStrs(const string& rStr)
{
        mMaxLen = 0;
        GetMaxSubStrs(rStr);
}

void CMaxSubStrs::GetMaxSubStrs(const string& rStr)
{
        string                subStr;

        //利用string流分割字符串
        istringstream input_string(rStr.c_str());
       
        while(!input_string.eof())
        {
                input_string>>subStr;

                //如果遇到更大的子串则清空容器,容器存放新的最大子串
                if(mMaxLen < subStr.length())
                {
                        //记录最大子串长度
                        mMaxLen = subStr.length();
                        mVecMaxStrs.clear();
                        mVecMaxStrs.push_back(subStr);
                }
                else if(mMaxLen == subStr.length())
                {
                        mVecMaxStrs.push_back(subStr);
                }
        }
}

void CMaxSubStrs::PrintMaxSubStrs()
{
        vector<string>::iterator iter;
        vector<string>::iterator endIter = mVecMaxStrs.end();
        for(iter=mVecMaxStrs.begin(); iter!=endIter; iter++)
        {
                cout<<(*iter)<<endl;
        }

        cout<<\"len:\"<<mMaxLen<<endl;

}

int main(int argc, char* argv[])
{
        CMaxSubStrs test(\"The indictment said the defendants had \"
                                        \"collected geographical data indicating \"
                                        \"thousands of people would be killed in \"
                                        \"the chemical blast\");
        test.PrintMaxSubStrs();

        return 0;
}
回复

使用道具 举报

发表于 2006-1-17 14:52 | 显示全部楼层
好变态吖~~~~
望住D甘长嘅程序就想晕.........
回复

使用道具 举报

发表于 2006-1-24 19:35 | 显示全部楼层
手痒,直接打入一个试试,没有输出最长的字符串
int maxlen(char *word)
{
  int len;
  len=0;
  while(*word)
  {
    if(word==\' \')
      len=0;
   else
     len++;
    word++;
  }
return len;
}
回复

使用道具 举报

发表于 2006-1-24 19:44 | 显示全部楼层
int maxlen(char *word)
{
  int len;
  len=0;
  while(*word)
  {
    if(*word==\' \')
      len=0;
   else
     len++;
    word++;
  }
return len;
}
void xixi(char *word)
{
  int len,i,j,index;
  len=maxlen(word);
  index=0;i=j=0;
  while(word)
  {
    if(word==\' \')
      index=i+1;
    else
     j++;
   if(j==len)
   {
      while(word[index]!=\' \') printf(\"%c\",word[index++]);
      printf(\"\\n\");
      j=0;
   } //if
   word++;
  } //while
}

//没有调试过,仅供参考
回复

使用道具 举报

发表于 2006-1-26 12:49 | 显示全部楼层
应该可以直接扫描。。
回复

使用道具 举报

发表于 2006-1-30 02:08 | 显示全部楼层
强。。。。。。。。。。。。。。。。。
回复

使用道具 举报

发表于 2006-2-21 11:12 | 显示全部楼层
第三楼wool王 (Woden) 的程序有问题吧?
                for (int i = 0; i < newString.length; i++) {
                        if (newString.length() > maxLength) {
                                maxLength = newString.length();
                                maxLengthString = newString;
                        }
                }
感觉这部分是错误的。
               for (int i = 0; i < newString.length; i++) {
                       if (newString.length() = maxLength) {
                                maxLengthString =maxLengthString +\",\" +newString;
                        }
                        if (newString.length() > maxLength) {
                                maxLength = newString.length();
                                maxLengthString = newString;
                        }

                }

在最后输出时,再用SPLIT进行分解并输出

[ 本帖最后由 大鱼 于 2006-2-21 14:01 编辑 ]
回复

使用道具 举报

发表于 2006-2-21 14:02 | 显示全部楼层
<%
if request.QueryString(\"action\")=\"n\" then
i_strr=request.Form(\"str_n\")
shuchu(i_strr)
end if

function shuchu(i_str)
dim array_list,lenght_max
array_list=split(i_str,\" \")
lenght_max=0
str_max=\"\"
for i=0 to ubound(array_list)
  if len(array_list(i))=lenght_max then
     str_max=str_max&\",\"&array_list(i)
  end if
  if len(array_list(i))>lenght_max then
     str_max=array_list(i)
         lenght_max=len(array_list(i))
  end if
next

response.Write(\"最长单词:\")
response.Write(str_max)
response.Write(\"<br>长度为:\")
response.Write(lenght_max)
end function
%>
<html>
<title>
测试页面
</title>
<body>
<form action=\"?action=n\" method=\"post\" name=\"dd\">
<input type=\"text\" name=\"str_n\" size=\"30\" value=\"dddd kkkk bbb dddd\">
<input type=\"submit\" value=\"提交\">
</form>
</body>
</html>
回复

使用道具 举报

发表于 2006-2-21 14:41 | 显示全部楼层
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace yong
{
        /// <summary>
        /// test 的摘要说明。
        /// </summary>
        public class test : System.Web.UI.Page
        {
                protected System.Web.UI.WebControls.TextBox TextBox1;
                protected System.Web.UI.WebControls.Label Label1;
                protected System.Web.UI.WebControls.Button Button1;
       
                private void Page_Load(object sender, System.EventArgs e)
                {
                        // 在此处放置用户代码以初始化页面
                }

                #region Web 窗体设计器生成的代码
                override protected void OnInit(EventArgs e)
                {
                        //
                        // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
                        //
                        InitializeComponent();
                        base.OnInit(e);
                }
               
                /// <summary>
                /// 设计器支持所需的方法 - 不要使用代码编辑器修改
                /// 此方法的内容。
                /// </summary>
                private void InitializeComponent()
                {   
                        this.Button1.Click += new System.EventHandler(this.Button1_Click);
                        this.Load += new System.EventHandler(this.Page_Load);

                }
                #endregion

                private void Button1_Click(object sender, System.EventArgs e)
                {
                        string i_str=TextBox1.Text.ToString();
                        string s_char=\" \";
                        string[] str_i=i_str.Split(s_char.ToCharArray());
                        int lenght_max=0;
                        string str_max=\"\";
                        for (int i=0;i<str_i.Length;i++)
                        {
                                if(str_i.Length==lenght_max)
                                {
                     str_max=str_max+\",\"+str_i.ToString();
                                }
                                if(str_i.Length>lenght_max)
                                {
                                        str_max=str_i.ToString();
                                        lenght_max=str_i.Length;
                                }                       
                        }
                       
          Label1.Text=\"最长的字符为:\"+str_max.ToString()+\"<br>\"+\"长度为:\"+lenght_max.ToString();
                }
        }
}

到底还是。NET写程序舒服,一个字。爽!比JAVA和ASP等好用多了。

[ 本帖最后由 大鱼 于 2006-2-21 14:46 编辑 ]
回复

使用道具 举报

 楼主| 发表于 2006-2-22 12:30 | 显示全部楼层
呵呵.c#确实是优美的语言.java的优势在于思想.
回复

使用道具 举报

发表于 2006-2-22 13:37 | 显示全部楼层
赞同楼上的说法。支持!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-14 16:24

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表