华为机试 题目2 - 字符串过滤

来源:互联网 发布:西安儿童编程培训机构 编辑:程序博客网 时间:2024/06/05 09:23
/** * 2014年 校园招聘机试 第一题(60分、100分、160分) *  一、题目描述(60分): * 通过键盘输入一串小写字母(a~z)组成的字符串。请编写一个字符串过滤程序,若字符串中出现多个相同的字符,将非首次出现的字符过滤掉。 * 比如字符串“abacacde”过滤结果为“abcde”。 要求实现函数:void stringFilter(const char *pInputStr, * long lInputLen, char *pOutputStr); 【输入】 pInputStr: 输入字符串 lInputLen: 输入字符串长度 * 【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长; 【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出 示例 * 输入:“deefd” 输出:“def” 输入:“afafafaf” 输出:“af” 输入:“pppppppp” 输出:“p” * main函数已经隐藏,这里保留给用户的测试入口,在这里测试你的实现函数,可以调用printf打印输出 * 当前你可以使用其他方法测试,只要保证最终程序能正确执行即可,该函数实现可以任意修改,但是不要改变函数原型。一定要保证编译运行不受影响。 *  * @author snail * 耗时:18分钟 */public class Test201401 {private static String inputString = "afafafaf";private static final int inputLen = inputString.length();private static StringBuffer outputChars = new StringBuffer();public static void main(String args[]) {int n = 0;for (int i = 0; i < inputLen; i++) {n = 0;for (int j = 0; j < i; j++) {if (inputString.charAt(i) == inputString.charAt(j)) {n++;}}if (n == 0) {outputChars.append(inputString.charAt(i));}}System.out.print(outputChars.toString());}}

0 0
原创粉丝点击