方正的面试题目1

出自求职百科

跳转到: 导航, 搜索
输入:一个文件,一个特征字符串

输出:这个特征字符串在文件中出现的次数

可以用一个循环嵌套来累加一个计数器,如:
int count=0; //计数器
String arg; //文章
String str; //特征字符串
String temp; //arg的一部分
public int getCount(String arg,String str){
if(arg.indexOf(str)>0){
count++;

temp = arg.subString(arg.indexOf(str),arg.length());

getCount(temp,str);
}
}
我没有运行过,但思想是这样的,当然这只是其中一种,
大家会给更多的实现方法给你的。

将文章中的所有特征字符串替换为空
然后用原文章的长度减去替换后的文章的长度除以特征字符串的长度就是
int count=0; //计数器
String arg; //文章
String str; //特征字符串
String temp; //arg的一部分
public int getCount(String arg,String str){
temp = arg.replaceAll(str,"");
count = (arg.length()-temp.length())/str.length();
}

其实方法很多啦,.个人感觉sunshine5246(阳光)这个不错
我也做了一个
源程序如下:
class Test
{
public static void main(String[] args)
{
Find str=new Find("1111112323232323232323","232");
str.count();
}
}
class Find
{
String ss;
String reg;
public Find(String ss,String reg)
{
this.ss=ss;
this.reg=reg;
}
public void count()
{
int i=0;
int j=0;
int n=ss.indexOf(reg);
// System.out.println("count n1 is:"+n);
while( n!=-1&&j!=-1)
{

j=ss.indexOf(reg,reg.length()+n+j);
i++;
n=0;
}
System.out.println(reg+"'s count is:"+i);

}
}


kao ,代码太多了,天天学傻了
class Test
{
public staticvoid count(String ss,String reg)
{
int i=0;
int j=0;
int n=ss.indexOf(reg);
//System.out.println("count n1 is:"+n);
while( n!=-1&&j!=-1)
{
j=ss.indexOf(reg,reg.length()+n+j);
i++;
n=0;
}
System.out.println(reg+"'s count is:"+i);

}
public static void main(String[] args)
{
count("1111112323232323232323","32");
}
}

个人工具
公司索引
  • A   B   C   D   E   F   G
  • H   I   J   K   L   M   N
  • O    P
  •     Q    R    S    T
  • U    V    W    X    Y    Z
工具箱