2014届百田校招笔试

          这次百田的笔试共有三道编程题,60分钟完卷。感觉发挥十分槽糕。第二题事后反思自己的思维方式有误,丢弃掉原本数学的知识反而去想一些递归什么的。学习编程和算法要有这理科生的心态,而不是一味死记硬背。好了不多说。下面是3道大题。

    题目1:有n位同学(n是偶数),编号从1到n。现在需要把n位同学分配到2张桌子吃饭,有m组约束,要求a[i]不得与b[i]同桌。a[],b[]的长度都是m。如果有多种解决方案,返回其中一种一桌的编号。如果没有结果,返回空。

           

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
	public int[] arrange(int n, int m, int[] a, int[] b) {
		int[] l1 = new int[n / 2];
		int[] l2 = new int[n / 2];
		int l1size = 0;
		int l2size = 0;
		for (int i = 0; i < m; i++) {
			if (i == 0) {
				l1[0] = a[0];
				l2[0] = b[0];
				l1size++;
				l2size++;
			} else if (!findInArray(a[i], l1size, l1)
					&& !findInArray(b[i], l2size, l2)
					&& !findInArray(a[i], l2size, l2)
					&& !findInArray(b[i], l1size, l1)) {
				l1[l1size] = a[i];
				l2[l2size] = b[i];
				l1size++;
				l2size++;
			} else if (findInArray(a[i], l1size, l1)
					&& !findInArray(b[i], l2size, l2)
					&& !findInArray(b[i], l1size, l1)) {
				l2[l2size] = b[i];
				l2size++;
			} else if (!findInArray(a[i], l1size, l1)
					&& !findInArray(a[i], l2size, l2)
					&& findInArray(b[i], l1size, l1)) {
				l2[l2size] = a[i];
				l2size++;
			} else if (!findInArray(b[i], l1size, l1)
					&& !findInArray(b[i], l2size, l2)
					&& findInArray(a[i], l2size, l2)) {
				l1[l1size] = b[i];
				l1size++;
			} else if (!findInArray(a[i], l1size, l1)
					&& findInArray(b[i], l2size, l2)
					&& !findInArray(a[i], l2size, l2)) {
				l1[l1size] = a[i];
				l1size++;
			} else if ((findInArray(a[i], l1size, l1)
					&& findInArray(b[i], l2size, l2)
					&& !findInArray(a[i], l2size, l2) && !findInArray(b[i],
						l1size, l1))
					|| (findInArray(b[i], l1size, l1)
							&& findInArray(a[i], l2size, l2)
							&& !findInArray(b[i], l2size, l2) && !findInArray(
								a[i], l1size, l1))) {

			} else if ((findInArray(a[i], l1size, l1) && findInArray(b[i],
					l1size, l1))
					|| (findInArray(a[i], l2size, l2) && findInArray(b[i],
							l2size, l2))) {
				if ((findInNum(a[i], i, a) + findInNum(a[i], i, b)) >= 2
						&& (findInNum(b[i], i, a) + findInNum(b[i], i, b)) >= 2)
					return null;
				else if ((findInNum(a[i], i, a) + findInNum(a[i], i, b)) == 1
						&& (findInNum(b[i], i, a) + findInNum(b[i], i, b)) >= 2) {
					int t = 0;
					if (findInArray(a[i], i, a)) {
						for (int j = 0; j < i; j++) {
							if (a[j] == a[i])
								t = b[j];
						}
						for (int k = 0; k < l1size; k++) {
							if (a[i] == l1[k]) {
								l1[k] = t;
							}
						}
						for (int k = 0; k < l2size; k++) {
							if (t == l2[k]) {
								l1[k] = a[i];
							}
						}
					} else {
						for (int j = 0; j < i; j++) {
							if (b[j] == a[i])
								t = a[j];
						}
						for (int k = 0; k < l1size; k++) {
							if (a[i] == l1[k]) {
								l1[k] = t;
							}
						}
						for (int k = 0; k < l2size; k++) {
							if (t == l2[k]) {
								l1[k] = a[i];
							}
						}

					}

				} else if ((findInNum(b[i], i, a) + findInNum(b[i], i, b)) == 1
						&& (findInNum(a[i], i, a) + findInNum(a[i], i, b)) >= 2) {
					int t = 0;
					if (findInArray(b[i], i, a)) {
						for (int j = 0; j < i; j++) {
							if (a[j] == b[i])
								t = a[j];
						}
						for (int k = 0; k < l1size; k++) {
							if (b[i] == l1[k]) {
								l1[k] = t;
							}
						}
						for (int k = 0; k < l2size; k++) {
							if (t == l2[k]) {
								l1[k] = b[i];
							}
						}
					} else {
						for (int j = 0; j < i; j++) {
							if (b[j] == b[i])
								t = a[j];
						}
						for (int k = 0; k < l1size; k++) {
							if (b[i] == l1[k]) {
								l1[k] = t;
							}
						}
						for (int k = 0; k < l2size; k++) {
							if (t == l2[k]) {
								l1[k] = b[i];
							}
						}

					}
				}
			}
		}

		if (l1size < l1.length || l2size < l2.length) {
			for (int j = 1; j <= n; j++) {
				if (!findInArray(j, l1size, l1) && !findInArray(j, l2size, l2)) {
					if (l1size < l1.length) {
						l1[l1size] = j;
						l1size++;

					} else if (l2size < l2.length) {
						l2[l2size] = j;
						l2size++;
					}
				}
			}
		}
		for (int i = 0; i < l1.length; i++) {
			System.out.println(l1[i]);
		}
		for (int i = 0; i < l2.length; i++) {
			System.out.println(l2[i]);
		}
		return l1;
	}

	private boolean findInArray(int m, int j, int[] a) {
		for (int i = 0; i < j; i++) {
			if (a[i] == m) {
				return true;
			}
		}
		return false;
	}

	private int findInNum(int m, int j, int[] a) {
		int result = 0;
		for (int i = 0; i < j; i++) {
			if (a[i] == m) {
				++result;
			}
		}
		return result;
	}

	public static void main(String[] args) {
		int n = 0, m = 0;
		Scanner cin = new Scanner(new BufferedInputStream(System.in));
		if (cin.hasNextInt()) {
			n = cin.nextInt();
		} else {
			System.out.println("输入不对");
		}
		if (cin.hasNextInt()) {
			m = cin.nextInt();
		} else {
			System.out.println("输入不对");
		}
		int k = 0;
		int j = 0;
		int[] result = new int[n / 2];
		int[] a = new int[m];
		int[] b = new int[m];
		while (k < m && cin.hasNextInt()) {
			a[k] = cin.nextInt();
			k++;
		}
		while (j < m && cin.hasNextInt()) {
			b[j] = cin.nextInt();
			j++;
		}
		result = new Main().arrange(n, m, a, b);
	}
}

运行结果截图


题目2

       已知  f(n)=f(n-1)+f(n-2),如果知道f(0)的值为a,n的个数和f(n)的值,求f(1)的值。

      分别输入 a,n,f(n)的值输入f(1).

     例如输入  1   3  11 输出 5.

  代码

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
	public int findB(int a, int n, int f) {
		int anum = 0;
		int bnumpre = 0;
		int bnum = 1;
		for (int i = 2; i <= n; i++) {
			anum = bnum;
			bnum += bnumpre;
			bnumpre = anum;
		}
		return (f - a * anum) / bnum;
	}

	public static void main(String[] args) {
		Scanner cin = new Scanner(new BufferedInputStream(System.in));
		int a = 0;
		int n = 0;
		int f = 0;
		if (cin.hasNextInt()) {
			a = cin.nextInt();
		}
		if (cin.hasNextInt()) {
			n = cin.nextInt();
		}
		if (cin.hasNextInt()) {
			f = cin.nextInt();
		}
		System.out.println(new Main().findB(a, n, f));
	}
}

运行结果:


 题目3

      输入16进制字符串如“3A”输出“111010”

     代码

import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
	public String hxToBin(String s) {
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			if (i == 0) {
				result.append(trimZero(chatToString(s.charAt(i))));
			} else {
				result.append(chatToString(s.charAt(i)));
			}
		}
		return result.toString();
	}

	private String trimZero(String s) {
		String result = null;
		result = s.replaceFirst("^0+", "");
		return result;
	}

	private String chatToString(char a) {
		switch (a) {
		case '0':
			return "0000";
		case '1':
			return "0001";
		case '2':
			return "0010";
		case '3':
			return "0011";
		case '4':
			return "0100";
		case '5':
			return "0101";
		case '6':
			return "0110";
		case '7':
			return "0111";
		case '8':
			return "1000";
		case '9':
			return "1001";
		case 'A':
			return "1010";
		case 'B':
			return "1011";
		case 'C':
			return "1100";
		case 'D':
			return "1101";
		case 'E':
			return "1110";
		case 'F':
			return "1111";
		default:
			return null;
		}

	}

	public static void main(String[] args) {
		Scanner cin = new Scanner(new BufferedInputStream(System.in));
		String s = null;
		if (cin.hasNext()) {
			s = cin.next();
		}
		System.out.println(new Main().hxToBin(s));
	}
}
运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值