java编程题(一)

一, 现在输入n个数字,以逗号,分开;然后可选择升或者降序排序;按提交键就在另一页面显示按什么 排序,结果为,  ,

 提供reset

public class exp1 {
	String answer = null;
	String[] str = null;


	public static void main(String[] args) {
		
		String tempstr="";
		int[] tempint;
		
		exp1 ex =new exp1();
		System.out.println("请输入n个数字,用逗号隔开,按enter确定");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try{
			ex.answer = br.readLine();
		}catch(Exception e){
			System.out.println("IO error");
		}
		
		ex.str = ex.filerString(ex.answer);
		tempint = ex.strArrayToInt(ex.str);
	
		
		while(true){
			System.out.println("请输入命令:asc:升序,dec:降序,重置:reset;exit:复位");
			BufferedReader brr = new BufferedReader(new InputStreamReader(System.in));
			
			try {
				tempstr = brr.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(tempstr.equals("asc")){
				Arrays.sort(tempint);
				printIntArray(tempint,true);
			}else if(tempstr.equals("dec")){
				Arrays.sort(tempint);
				printIntArray(tempint,false);
			}else if(tempstr.equals("reset")){
				tempint = ex.strArrayToInt(ex.str);
				printIntArray(tempint,true);
			}else if(tempstr.equals("exit")){
				break;
			}
		}
	}


	private static void printIntArray(int[] tempint, boolean asc) {
		// TODO Auto-generated method stub
		if (asc) {
			for (int i = 0; i < tempint.length - 1; i++) {
				System.out.print(tempint[i] + ",");
			}
			System.out.println(tempint[tempint.length - 1]);
		} else {
			for (int i = tempint.length - 1; i >= 1; i--) {
				System.out.print(tempint[i] + ",");
			}
			System.out.println(tempint[0]);
		}
	}


	private int[] strArrayToInt(String[] str2) {
		// TODO Auto-generated method stub
		int[] temp = new int[str2.length];
		for (int i = 0; i < str2.length; i++) {
			temp[i] = Integer.parseInt(str2[i]);
		}
		return temp;
	}


	private String[] filerString(String str) {


		String[] strarray = null;
		int count = 0;


		StringTokenizer st = new StringTokenizer(str, ",");
		strarray = new String[st.countTokens()];
		while (st.hasMoreTokens()) {
			strarray[count] = st.nextToken();
			count++;
		}
		return strarray;
	}
}

二,编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,
 如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,应该输出为"我ABC"而不是"我ABC+汉的半个"。 

/*
 写代码前,先了解下两种编码:
 ucs2就是标准的unicode编码, 它是某国际组织设计的一种文字符号编码表,包括了世界上绝大多数文字和符号,
 包括中文,每个字符使用2字节编码,因此叫ucs2;


 UTF8是对ucs2的一种压缩编码, 它根据字符使用频率的不同,把高频字用8位来表示,
 低频字用16位或32位表示,这样一来一段文字的数据总体长度比较小,便于传输。


 因此需要将其转换为unicode编码,
 byte[] bytes = s.getBytes("Unicode");  由于上面生成的字节数组中前两个字节是标志位,
 bytes[0] = -2,bytes[1] = -1,因此,要从第三个字节开始扫描,对于一个英文或数字字符,
 UCS2编码的第二个字节是相应的ASCII,第一个字节是0,如a的UCS2编码是0  97,
 而汉字两个字节都不为0,因此,可以利于UCS2编码的这个规则来计算实际的字节数,该方法的实现代码如下:


 */

public class exp3 {

	public static String getFilterSubString(String str, int length)
			throws UnsupportedEncodingException {
		if (str.length() <= length) {
			return str;
		}

		byte[] bytes = str.getBytes("Unicode");
		int count = 0;  	//用于记录实际的字符长度
		int subCount = 0; //用于记录substring的长度,因为substring中的汉字被看与字母一样的单位;

		for (int i = 2; i < bytes.length; i = i + 2) {
			if (bytes[i] != 0) {
				if (count + 2 > length) {
					break;
				} else {
					count = count + 2;
					subCount++;
				}
			} else {
				if (count + 1 > length) {
					break;
				} else {
					count = count + 1;
					subCount++;
				}
			}
		}
		return str.substring(0, subCount);
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		System.out.println(exp3.getFilterSubString("我ABC", 4));
		System.out.println(exp3.getFilterSubString("我ABC汉DEF", 6));
		System.out.println(exp3.getFilterSubString("我ABCD", 4));
		System.out.println(exp3.getFilterSubString("我ABC汉DEF我ABC汉DEF我ABC汉DEF",12));
	}

}

三,腾讯的一道笔试题

1.  列表中有100个人,从第1个开始数数,当数到7或者7的倍数时,则从列表中将该对象移除,数到末尾后又开始从第一个循环开始数直到删除至最后一个人 !

public static void main(String[] args) {
		LinkedList<Integer> li = new LinkedList<Integer>();
		for(int i = 1;i<=100;i++){
			li.add(i);
		}
		new test().FindIt(li,7);
	}
	
	public void FindIt(LinkedList li,int countSum){
		if(countSum<=0){
			return;
		}
		int i=0;
		int count=0;
		
		while(li.size()>0){
			for(i=0;i<countSum-1;i++){
			//这道题有一个非常值得注意的地方,就是remove后,那么List的小标都会往前移动一位,
			//所以为什么是li.size()-1,将要移除的那一位不统计在内,就不会进入这个误区了
				if(count<li.size()-1){
					count++;
				}else{
					count = 0;
				}
			}
			
			System.out.print(li.remove(count)+" ");
		}
		
	}
}

运行结果如下:

7 14 21 28 35 42 49 56 63 70 77 84 91 98 5 1322 30 38 46 54 62 71 79 87 95 3 12 23 32 41 51 60 69 80 89 99 9 19 31 43 53 6575 86 97 10 24 36 48 61 74 88 1 16 29 45 59 76 92 6 25 40 58 78 94 15 34 55 7396 18 44 67 90 17 47 72 2 33 66 100 27 68 8 52 93 39 85 50 11 82 64 57 81 4 3783 26 20









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值