转贴道面试题...大家用不同语言做做看...
面试失败,叫编一个程序,但在给定时间没编出来,那题目出来分享下,希望高手可以做出来题目:
编写一程序输出一个由若干单词构成的字符串中的最长单词及其长度,若字符串中有多个单词满足条件须将这些单词全部输出。(注:程序书写要规范)
例:输入字符串为“The indictment said the defendants had collected geographical data indicating thousands of people would be killed in the chemical blast”
输出应为:
最长单词:geographical
单词长度:12
在CSDN的java版转来的...大家看看...
package test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test1 {
private int length;
private String[] s;
private List list;
public void find(String str){
length = 0;
s = str.split(\" \");
list = new ArrayList();
for ( int i=0; i < s.length; i++ ){
if( s.length() > length ){
length = s.length();
list.clear();
list.add(s);
}
else if( s.length() == length ){
length = s.length();
list.add(s);
}
}
}
public void print(){
Iterator itor = list.iterator();
while( itor.hasNext() ){
String s = (String)itor.next();
System.out.println(\"最长单词:\"+s);
System.out.println(\"单词长度:\"+length);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Test1 app = new Test1();
app.find(\"a bb cses s fess\");
app.print();
}
}
java写的一个.
[ 本帖最后由 hjack 于 2006-1-13 16:32 编辑 ] package test;
public class CountString {
private static String find(String s) {
String newString[] = s.split(\" \");
int maxLength = 0;
String maxLengthString = null;
for (int i = 0; i < newString.length; i++) {
if (newString.length() > maxLength) {
maxLength = newString.length();
maxLengthString = newString;
}
}
return maxLengthString + \",\" + maxLength;
}
private static void print(String s) {
String newString[] = s.split(\",\");
System.out.println(\"最长单词:\" + newString);
System.out.println(\"单词长度:\" + newString);
}
public static void main(String[] args) {
String s = \"The indictment said the defendants had collected geographical data indicating thousands of people would be killed in the chemical blast\";
print(find(s));
return;
}
}
...被先贴出来了...我这个没用collection接口... 这是用JAVASCRIPT来写的
<html><head>
<SCRIPT LANGUAGE=\"JavaScript\">
<!--
function f()
{
var str=document.all.text1.value;
document.all.text2.value=\"\";
var s=str.split(\" \");
var len=s.length;
var t=s;
for(i=0;i<len;i++)if(t.length<s.length)t=s;
var L=t.length;
document.all.text2.value+=\"最长单词是: \";
//题目没说相同单词不要重复输出,所以省事一些
for(i=0;i<len;i++){
if(s.length==L)
document.all.text2.value+=s+\" \";
}
document.all.text2.value+=\" 单词长度为:\"+L;
}
//-->
</SCRIPT>
</head>
<body><center>
<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>
<textarea cols=\"100\" rows=\"10\" id=\"text2\"></textarea><br>
<input type=\"button\" value=\"OK\" onclick=\"f()\">
</center>
</body>
</html>
//Flash ActionScript 1.0
var temp=0;
var str = \"The indictment said the defendants had collected geographical data indicating thousands of people would be killed in the chemical blast\";
var breakStr = new Array();//to store the word
var longWord = new Array();//to store the longest word
breakStr=str.split(\" \"); //break the string
for(var i=0;i<breakStr.length;i++){
if(breakStr.length>temp){
longWord=new Array();
longWord.push(breakStr);
temp=breakStr.length;
}else if(breakStr.length==temp){
longWord.push(breakStr);
}
}
trace(temp);
trace(longWord);
//Flash ActionScript 2.0
var temp:Number = 0;
var str:String = \"The indictment said the defendants had collected geographical data indicating thousands of people would be killed in the chemical blast\";
var breakStr:Array = new Array(); //to store the word
var longWord:Array = new Array(); //to store the longest word
breakStr = str.split(\" \"); //break the string
for (var i:Number = 0; i<breakStr.length; i++) {
if (breakStr.length>temp) {
longWord = new Array();
longWord.push(breakStr);
temp = breakStr.length;
} else if (breakStr.length == temp) {
longWord.push(breakStr);
}
}
trace(temp);
trace(longWord);
OUTPUT:
12
geographical
[ 本帖最后由 黯然销魂 于 2006-1-16 17:34 编辑 ] 记得以前考二级C语言的上机题目中背过类似题目,用指针。。。。 #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;
} 好变态吖~~~~
望住D甘长嘅程序就想晕......... 手痒,直接打入一个试试,没有输出最长的字符串
int maxlen(char *word)
{
int len;
len=0;
while(*word)
{
if(word==\' \')
len=0;
else
len++;
word++;
}
return len;
} 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!=\' \') printf(\"%c\",word);
printf(\"\\n\");
j=0;
} //if
word++;
} //while
}
//没有调试过,仅供参考 应该可以直接扫描。。 强。。。。。。。。。。。。。。。。。 第三楼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 编辑 ] <%
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> 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 编辑 ] 呵呵.c#确实是优美的语言.java的优势在于思想. 赞同楼上的说法。支持!
页:
[1]