<div style="font-size: 14px; color: rgb(73, 73, 73); font-family: simsun; line-height: 21px; background-color: rgb(226, 226, 226);">题一:题目描述(60分):</div><div style="font-size: 14px; color: rgb(73, 73, 73); font-family: simsun; line-height: 21px; background-color: rgb(226, 226, 226);">通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。</div><div style="font-size: 14px; color: rgb(73, 73, 73); font-family: simsun; line-height: 21px; background-color: rgb(226, 226, 226);">比如字符串“abacacde”过滤结果为“abcde”。</div><div style="font-size: 14px; color: rgb(73, 73, 73); font-family: simsun; line-height: 21px; background-color: rgb(226, 226, 226);">要求实现函数: <wbr></wbr></div><div style="font-size: 14px; color: rgb(73, 73, 73); font-family: simsun; line-height: 21px; background-color: rgb(226, 226, 226);">void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr);</div>
<pre name="code" class="java">public class StringFilter { private static void stringFilte(char[] pInputStr,long lInputLen,char[] pOutputStr) { int[] a=new int[26]; //初始化一个长度为26的int型数组,注:int型数组初始化时为0 int len=0; for(int i=0;i<lInputLen;i++) { int temp=0; temp=pInputStr[i]-'a'; if(a[temp]==0)<span style="white-space:pre"> </span>//当字符串第一次出现 { pOutputStr[len++]=pInputStr[i]; a[temp]=1;<span style="white-space:pre"> </span>//已出现过的字符串标记为1 } else { continue; } } } /** * @param args */ public static void main(String[] args) { char[] pInputStr=new char[]{'d','d','e','f','f','g','g','g'}; long lInputLen=pInputStr.length; char[] pOutputStr=new char[(int) lInputLen]; stringFilte(pInputStr,lInputLen,pOutputStr); for(int i=0;i<lInputLen;i++) { System.out.print(pOutputStr[i]); } } }
输出defg
题二:题目描述(40分):
通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串压缩程序,将字符串中连续出席的重复字母进行压缩,并输出压缩后的字符串。
压缩规则:
1. 仅压缩连续重复出现的字符。比如字符串"abcbc"由于无连续重复字符,压缩后的字符串还是"abcbc".
2. 压缩字段的格式为"字符重复的次数+字符"。例如:字符串"xxxyyyyyyz"压缩后就成为"3x6yz"
要求实现函数:
void stringZip(const char *pInputStr, long lInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例
输入:“cccddecc” 输出:“3c2de2c”
输入:“adef” 输出:“adef”
输入:“pppppppp” 输出:“8p”
public class StringZip { private static void stringZip(final char[] pInputStr, long lInputLen, char[] pOutputStr) { int i=0; int len=0; while(i<lInputLen) { int time=1; //字符出现的次数 char c=pInputStr[i]; i++; /*也可改为 char c=pInputStr[i++];*/ while(i<lInputLen && c==pInputStr[i]) { time++; i++; } if(time==1) { pOutputStr[len++]=c; }else if(time<=9) { pOutputStr[len++]=(char) (time+'0'); pOutputStr[len++]=c; } else if(time<=99) { pOutputStr[len++]=(char)(time/10+'0'); pOutputStr[len++]=(char)(time%10+'0'); pOutputStr[len++]=c; } } } /** * @param args */ public static void main(String[] args) { final char[] pInputStr={'c','c','c','d','d','e','f','f'}; long lInputLen=pInputStr.length; char[] pOutputStr=new char[(int)(lInputLen)]; stringZip(pInputStr,lInputLen,pOutputStr); for(int i=0;i<lInputLen;i++) { System.out.print(pOutputStr[i]); } }